Mercurial > mplayer.hg
annotate libmpcodecs/vd_libmpeg2.c @ 26561:359bf9284e8b
10l: Add missing \ for line continuation.
author | diego |
---|---|
date | Tue, 29 Apr 2008 08:05:44 +0000 |
parents | 2506f1b0bdbe |
children | 05841cbb86aa |
rev | line source |
---|---|
4998 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 | |
4 #include "config.h" | |
8026
b465ba5897a3
usage of libmpeg2, liba52, mp3lib & svq1 can be disabled
arpi
parents:
7957
diff
changeset
|
5 |
4998 | 6 #include "mp_msg.h" |
7 | |
8 #include "vd_internal.h" | |
9 | |
9859 | 10 //#undef MPEG12_POSTPROC |
11 | |
4998 | 12 static vd_info_t info = |
13 { | |
12932 | 14 "MPEG 1/2 Video decoder libmpeg2-v0.4.0b", |
4998 | 15 "libmpeg2", |
9859 | 16 "A'rpi & Fabian Franz", |
4998 | 17 "Aaron & Walken", |
18 "native" | |
19 }; | |
20 | |
21 LIBVD_EXTERN(libmpeg2) | |
22 | |
9859 | 23 //#include "libvo/video_out.h" // FIXME!!! |
5465 | 24 |
4998 | 25 #include "libmpeg2/mpeg2.h" |
12932 | 26 #include "libmpeg2/attributes.h" |
4998 | 27 #include "libmpeg2/mpeg2_internal.h" |
28 | |
17012 | 29 #include "cpudetect.h" |
5465 | 30 |
18301 | 31 typedef struct { |
32 mpeg2dec_t *mpeg2dec; | |
33 int quant_store_idx; | |
34 char *quant_store[3]; | |
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
35 int imgfmt; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
36 int width; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
37 int height; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
38 double aspect; |
18301 | 39 } vd_libmpeg2_ctx_t; |
40 | |
4998 | 41 // to set/get/query special features/parameters |
42 static int control(sh_video_t *sh,int cmd,void* arg,...){ | |
18301 | 43 vd_libmpeg2_ctx_t *context = sh->context; |
44 mpeg2dec_t * mpeg2dec = context->mpeg2dec; | |
14012 | 45 const mpeg2_info_t * info = mpeg2_info (mpeg2dec); |
13995 | 46 |
47 switch(cmd) { | |
48 case VDCTRL_QUERY_FORMAT: | |
14012 | 49 if (info->sequence->width >> 1 == info->sequence->chroma_width && |
50 info->sequence->height >> 1 == info->sequence->chroma_height && | |
51 (*((int*)arg)) == IMGFMT_YV12) | |
13995 | 52 return CONTROL_TRUE; |
14012 | 53 if (info->sequence->width >> 1 == info->sequence->chroma_width && |
54 info->sequence->height == info->sequence->chroma_height && | |
55 (*((int*)arg)) == IMGFMT_422P) | |
13995 | 56 return CONTROL_TRUE; |
57 return CONTROL_FALSE; | |
58 } | |
59 | |
4998 | 60 return CONTROL_UNKNOWN; |
61 } | |
62 | |
63 // init driver | |
64 static int init(sh_video_t *sh){ | |
18301 | 65 vd_libmpeg2_ctx_t *context; |
9859 | 66 mpeg2dec_t * mpeg2dec; |
13995 | 67 // const mpeg2_info_t * info; |
9859 | 68 int accel; |
5465 | 69 |
9859 | 70 accel = 0; |
71 if(gCpuCaps.hasMMX) | |
72 accel |= MPEG2_ACCEL_X86_MMX; | |
73 if(gCpuCaps.hasMMX2) | |
74 accel |= MPEG2_ACCEL_X86_MMXEXT; | |
75 if(gCpuCaps.has3DNow) | |
76 accel |= MPEG2_ACCEL_X86_3DNOW; | |
26393
2506f1b0bdbe
Backport SSE2-optimized IDCT routines from upstream libmpeg2.
diego
parents:
25977
diff
changeset
|
77 if(gCpuCaps.hasSSE2) |
2506f1b0bdbe
Backport SSE2-optimized IDCT routines from upstream libmpeg2.
diego
parents:
25977
diff
changeset
|
78 accel |= MPEG2_ACCEL_X86_SSE2; |
10267
d953763cc555
libmpeg2-altivec patch by Magnus Damm <damm@opensource.se>:
arpi
parents:
10250
diff
changeset
|
79 if(gCpuCaps.hasAltiVec) |
d953763cc555
libmpeg2-altivec patch by Magnus Damm <damm@opensource.se>:
arpi
parents:
10250
diff
changeset
|
80 accel |= MPEG2_ACCEL_PPC_ALTIVEC; |
13117 | 81 #ifdef HAVE_VIS |
82 accel |= MPEG2_ACCEL_SPARC_VIS; | |
9859 | 83 #endif |
84 mpeg2_accel(accel); | |
5465 | 85 |
9859 | 86 mpeg2dec = mpeg2_init (); |
87 | |
88 if(!mpeg2dec) return 0; | |
89 | |
90 mpeg2_custom_fbuf(mpeg2dec,1); // enable DR1 | |
18301 | 91 |
92 context = calloc(1, sizeof(vd_libmpeg2_ctx_t)); | |
93 context->mpeg2dec = mpeg2dec; | |
94 sh->context = context; | |
9859 | 95 |
13112 | 96 mpeg2dec->pending_buffer = 0; |
97 mpeg2dec->pending_length = 0; | |
98 | |
9859 | 99 return 1; |
4998 | 100 } |
101 | |
102 // uninit driver | |
103 static void uninit(sh_video_t *sh){ | |
18301 | 104 int i; |
105 vd_libmpeg2_ctx_t *context = sh->context; | |
106 mpeg2dec_t * mpeg2dec = context->mpeg2dec; | |
13112 | 107 if (mpeg2dec->pending_buffer) free(mpeg2dec->pending_buffer); |
14012 | 108 mpeg2dec->decoder.convert=NULL; |
109 mpeg2dec->decoder.convert_id=NULL; | |
9859 | 110 mpeg2_close (mpeg2dec); |
18301 | 111 for (i=0; i < 3; i++) |
112 free(context->quant_store[i]); | |
113 free(sh->context); | |
4998 | 114 } |
115 | |
13112 | 116 static void draw_slice (void * _sh, uint8_t * const * src, unsigned int y){ |
9859 | 117 sh_video_t* sh = (sh_video_t*) _sh; |
18301 | 118 vd_libmpeg2_ctx_t *context = sh->context; |
119 mpeg2dec_t* mpeg2dec = context->mpeg2dec; | |
9859 | 120 const mpeg2_info_t * info = mpeg2_info (mpeg2dec); |
121 int stride[3]; | |
5465 | 122 |
13112 | 123 // printf("draw_slice() y=%d \n",y); |
5465 | 124 |
9859 | 125 stride[0]=mpeg2dec->decoder.stride; |
126 stride[1]=stride[2]=mpeg2dec->decoder.uv_stride; | |
5465 | 127 |
9859 | 128 mpcodecs_draw_slice(sh, (uint8_t **)src, |
12572
7d681d8ebab8
display height may be a lot smaller or larger than picture height, sample provided by winnicki
iive
parents:
11080
diff
changeset
|
129 stride, info->sequence->picture_width, |
7d681d8ebab8
display height may be a lot smaller or larger than picture height, sample provided by winnicki
iive
parents:
11080
diff
changeset
|
130 (y+16<=info->sequence->picture_height) ? 16 : |
7d681d8ebab8
display height may be a lot smaller or larger than picture height, sample provided by winnicki
iive
parents:
11080
diff
changeset
|
131 info->sequence->picture_height-y, |
9859 | 132 0, y); |
4998 | 133 } |
134 | |
5465 | 135 // decode a frame |
136 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){ | |
18301 | 137 vd_libmpeg2_ctx_t *context = sh->context; |
138 mpeg2dec_t * mpeg2dec = context->mpeg2dec; | |
9859 | 139 const mpeg2_info_t * info = mpeg2_info (mpeg2dec); |
140 int drop_frame, framedrop=flags&3; | |
141 | |
13995 | 142 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context |
14012 | 143 mpeg2dec->decoder.convert=NULL; |
144 mpeg2dec->decoder.convert_id=NULL; | |
13150 | 145 |
11080
26f1b3ad4a77
skip null frames in mpeg files, patch by Zoltan Hidvegi <mplayer@hzoli.2y.net>
attila
parents:
10663
diff
changeset
|
146 if(len<=0) return NULL; // skipped null frame |
26f1b3ad4a77
skip null frames in mpeg files, patch by Zoltan Hidvegi <mplayer@hzoli.2y.net>
attila
parents:
10663
diff
changeset
|
147 |
9859 | 148 // append extra 'end of frame' code: |
149 ((char*)data+len)[0]=0; | |
150 ((char*)data+len)[1]=0; | |
151 ((char*)data+len)[2]=1; | |
152 ((char*)data+len)[3]=0xff; | |
153 len+=4; | |
5465 | 154 |
13112 | 155 if (mpeg2dec->pending_length) { |
156 mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length); | |
157 } else { | |
23916 | 158 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len); |
13112 | 159 } |
9859 | 160 |
161 while(1){ | |
162 int state=mpeg2_parse (mpeg2dec); | |
13995 | 163 int type, use_callback; |
164 mp_image_t* mpi_new; | |
18428
22805699b7b1
moved code to set aspect ratio for mpeg12 away from video.c and into decoder files; A/R changes work correctly with -vc mpeg12
nicodvb
parents:
18301
diff
changeset
|
165 unsigned long pw, ph; |
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
166 int imgfmt; |
13995 | 167 |
9859 | 168 switch(state){ |
12932 | 169 case STATE_BUFFER: |
13112 | 170 if (mpeg2dec->pending_length) { |
13152 | 171 // just finished the pending data, continue with processing of the passed buffer |
13112 | 172 mpeg2dec->pending_length = 0; |
23916 | 173 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len); |
13112 | 174 } else { |
175 // parsing of the passed buffer finished, return. | |
176 return 0; | |
177 } | |
178 break; | |
9859 | 179 case STATE_SEQUENCE: |
18428
22805699b7b1
moved code to set aspect ratio for mpeg12 away from video.c and into decoder files; A/R changes work correctly with -vc mpeg12
nicodvb
parents:
18301
diff
changeset
|
180 pw = info->sequence->display_width * info->sequence->pixel_width; |
22805699b7b1
moved code to set aspect ratio for mpeg12 away from video.c and into decoder files; A/R changes work correctly with -vc mpeg12
nicodvb
parents:
18301
diff
changeset
|
181 ph = info->sequence->display_height * info->sequence->pixel_height; |
22805699b7b1
moved code to set aspect ratio for mpeg12 away from video.c and into decoder files; A/R changes work correctly with -vc mpeg12
nicodvb
parents:
18301
diff
changeset
|
182 if(ph) sh->aspect = (float) pw / (float) ph; |
25962 | 183 // video parameters initialized/changed, (re)init libvo: |
13995 | 184 if (info->sequence->width >> 1 == info->sequence->chroma_width && |
185 info->sequence->height >> 1 == info->sequence->chroma_height) { | |
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
186 imgfmt = IMGFMT_YV12; |
13995 | 187 } else if (info->sequence->width >> 1 == info->sequence->chroma_width && |
188 info->sequence->height == info->sequence->chroma_height) { | |
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
189 imgfmt = IMGFMT_422P; |
13995 | 190 } else return 0; |
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
191 if (imgfmt == context->imgfmt && |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
192 info->sequence->picture_width == context->width && |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
193 info->sequence->picture_height == context->height && |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
194 sh->aspect == context->aspect) |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
195 break; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
196 if(!mpcodecs_config_vo(sh, |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
197 info->sequence->picture_width, |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
198 info->sequence->picture_height, imgfmt)) |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
199 return 0; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
200 context->imgfmt = imgfmt; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
201 context->width = info->sequence->picture_width; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
202 context->height = info->sequence->picture_height; |
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
203 context->aspect = sh->aspect; |
9859 | 204 break; |
13995 | 205 case STATE_PICTURE: |
206 type=info->current_picture->flags&PIC_MASK_CODING_TYPE; | |
9859 | 207 |
208 drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE); | |
209 drop_frame |= framedrop>=2; // hard drop | |
210 if (drop_frame) { | |
211 mpeg2_skip(mpeg2dec, 1); | |
212 //printf("Dropping Frame ...\n"); | |
213 break; | |
214 } | |
215 mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0 | |
216 | |
14012 | 217 use_callback = (!framedrop && vd_use_slices && |
218 (info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ? | |
219 MP_IMGFLAG_DRAW_CALLBACK:0; | |
13995 | 220 |
9859 | 221 // get_buffer "callback": |
13995 | 222 mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB, |
14012 | 223 (type==PIC_FLAG_CODING_TYPE_B) ? |
224 use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE), | |
14075 | 225 info->sequence->width, |
226 info->sequence->height); | |
13995 | 227 |
13112 | 228 if(!mpi_new) return 0; // VO ERROR!!!!!!!! |
229 mpeg2_set_buf(mpeg2dec, mpi_new->planes, mpi_new); | |
20591
ca1d5d9fe51c
Set mpi stride, mostly fixes http://samples.mplayerhq.hu/MPEG2/res_change_ffmpeg_aspect.ts
reimar
parents:
18771
diff
changeset
|
230 mpi_new->stride[0] = info->sequence->width; |
ca1d5d9fe51c
Set mpi stride, mostly fixes http://samples.mplayerhq.hu/MPEG2/res_change_ffmpeg_aspect.ts
reimar
parents:
18771
diff
changeset
|
231 mpi_new->stride[1] = info->sequence->chroma_width; |
ca1d5d9fe51c
Set mpi stride, mostly fixes http://samples.mplayerhq.hu/MPEG2/res_change_ffmpeg_aspect.ts
reimar
parents:
18771
diff
changeset
|
232 mpi_new->stride[2] = info->sequence->chroma_width; |
10510
73b3e4336cd4
Add mpeg2_flags to mp_image_t, copy flags in vd_libmpeg2.c,
ranma
parents:
10267
diff
changeset
|
233 if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST) |
13112 | 234 mpi_new->fields |= MP_IMGFIELD_TOP_FIRST; |
235 else mpi_new->fields &= ~MP_IMGFIELD_TOP_FIRST; | |
10510
73b3e4336cd4
Add mpeg2_flags to mp_image_t, copy flags in vd_libmpeg2.c,
ranma
parents:
10267
diff
changeset
|
236 if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD) |
13112 | 237 mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST; |
238 else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST; | |
239 mpi_new->fields |= MP_IMGFIELD_ORDERED; | |
20639 | 240 if (!(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) |
241 mpi_new->fields |= MP_IMGFIELD_INTERLACED; | |
9859 | 242 |
12935 | 243 #ifdef MPEG12_POSTPROC |
18301 | 244 mpi_new->qstride=info->sequence->width>>4; |
245 { | |
246 char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ? | |
247 2 : (context->quant_store_idx ^= 1)]; | |
248 *p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4)); | |
249 mpi_new->qscale = *p; | |
12935 | 250 } |
13112 | 251 mpeg2dec->decoder.quant_store=mpi_new->qscale; |
252 mpeg2dec->decoder.quant_stride=mpi_new->qstride; | |
253 mpi_new->pict_type=type; // 1->I, 2->P, 3->B | |
254 mpi_new->qscale_type= 1; | |
12935 | 255 #endif |
256 | |
14012 | 257 if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK |
258 && !(mpi_new->flags&MP_IMGFLAG_DIRECT)) { | |
259 // nice, filter/vo likes draw_callback :) | |
260 mpeg2dec->decoder.convert=draw_slice; | |
261 mpeg2dec->decoder.convert_id=sh; | |
262 } else { | |
263 mpeg2dec->decoder.convert=NULL; | |
264 mpeg2dec->decoder.convert_id=NULL; | |
13995 | 265 } |
266 | |
9859 | 267 break; |
268 case STATE_SLICE: | |
269 case STATE_END: | |
12932 | 270 case STATE_INVALID_END: |
9859 | 271 // decoding done: |
13112 | 272 if(info->display_fbuf) { |
13152 | 273 mp_image_t* mpi = info->display_fbuf->id; |
274 if (mpeg2dec->pending_length == 0) { | |
13995 | 275 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start; |
276 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length); | |
277 memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length); | |
13152 | 278 } else { |
279 // still some data in the pending buffer, shouldn't happen | |
280 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start; | |
281 memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length); | |
282 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len); | |
283 memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len); | |
284 mpeg2dec->pending_length += len; | |
285 } | |
286 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length); | |
13112 | 287 return mpi; |
288 } | |
7957
31fd09cc9ba2
passing picture_type (might be usefull for postprocessing)
michael
parents:
7756
diff
changeset
|
289 } |
31fd09cc9ba2
passing picture_type (might be usefull for postprocessing)
michael
parents:
7756
diff
changeset
|
290 } |
5465 | 291 } |