Mercurial > mplayer.hg
annotate libmpcodecs/vd_libmpeg2.c @ 26923:cbd9c19ce4d8
Properly free memory allocate by liba52.
| author | reimar |
|---|---|
| date | Sun, 01 Jun 2008 16:40:56 +0000 |
| parents | 05841cbb86aa |
| children | 84723050e997 |
| 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; |
| 26594 | 81 #ifdef ARCH_ALPHA |
| 82 accel |= MPEG2_ACCEL_ALPHA; | |
| 83 #elif ARCH_ARM | |
| 84 accel |= MPEG2_ACCEL_ARM; | |
| 85 #endif | |
| 86 #ifdef HAVE_IWMMXT | |
| 87 accel |= MPEG2_ACCEL_ARM_IWMMXT; | |
| 88 #elif HAVE_MVI | |
| 89 accel |= MPEG2_ACCEL_ALPHA_MVI; | |
| 90 #elif HAVE_VIS | |
| 13117 | 91 accel |= MPEG2_ACCEL_SPARC_VIS; |
| 9859 | 92 #endif |
| 93 mpeg2_accel(accel); | |
| 5465 | 94 |
| 9859 | 95 mpeg2dec = mpeg2_init (); |
| 96 | |
| 97 if(!mpeg2dec) return 0; | |
| 98 | |
| 99 mpeg2_custom_fbuf(mpeg2dec,1); // enable DR1 | |
| 18301 | 100 |
| 101 context = calloc(1, sizeof(vd_libmpeg2_ctx_t)); | |
| 102 context->mpeg2dec = mpeg2dec; | |
| 103 sh->context = context; | |
| 9859 | 104 |
| 13112 | 105 mpeg2dec->pending_buffer = 0; |
| 106 mpeg2dec->pending_length = 0; | |
| 107 | |
| 9859 | 108 return 1; |
| 4998 | 109 } |
| 110 | |
| 111 // uninit driver | |
| 112 static void uninit(sh_video_t *sh){ | |
| 18301 | 113 int i; |
| 114 vd_libmpeg2_ctx_t *context = sh->context; | |
| 115 mpeg2dec_t * mpeg2dec = context->mpeg2dec; | |
| 13112 | 116 if (mpeg2dec->pending_buffer) free(mpeg2dec->pending_buffer); |
| 14012 | 117 mpeg2dec->decoder.convert=NULL; |
| 118 mpeg2dec->decoder.convert_id=NULL; | |
| 9859 | 119 mpeg2_close (mpeg2dec); |
| 18301 | 120 for (i=0; i < 3; i++) |
| 121 free(context->quant_store[i]); | |
| 122 free(sh->context); | |
| 4998 | 123 } |
| 124 | |
| 13112 | 125 static void draw_slice (void * _sh, uint8_t * const * src, unsigned int y){ |
| 9859 | 126 sh_video_t* sh = (sh_video_t*) _sh; |
| 18301 | 127 vd_libmpeg2_ctx_t *context = sh->context; |
| 128 mpeg2dec_t* mpeg2dec = context->mpeg2dec; | |
| 9859 | 129 const mpeg2_info_t * info = mpeg2_info (mpeg2dec); |
| 130 int stride[3]; | |
| 5465 | 131 |
| 13112 | 132 // printf("draw_slice() y=%d \n",y); |
| 5465 | 133 |
| 9859 | 134 stride[0]=mpeg2dec->decoder.stride; |
| 135 stride[1]=stride[2]=mpeg2dec->decoder.uv_stride; | |
| 5465 | 136 |
| 9859 | 137 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
|
138 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
|
139 (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
|
140 info->sequence->picture_height-y, |
| 9859 | 141 0, y); |
| 4998 | 142 } |
| 143 | |
| 5465 | 144 // decode a frame |
| 145 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){ | |
| 18301 | 146 vd_libmpeg2_ctx_t *context = sh->context; |
| 147 mpeg2dec_t * mpeg2dec = context->mpeg2dec; | |
| 9859 | 148 const mpeg2_info_t * info = mpeg2_info (mpeg2dec); |
| 149 int drop_frame, framedrop=flags&3; | |
| 150 | |
| 13995 | 151 // MPlayer registers its own draw_slice callback, prevent libmpeg2 from freeing the context |
| 14012 | 152 mpeg2dec->decoder.convert=NULL; |
| 153 mpeg2dec->decoder.convert_id=NULL; | |
| 13150 | 154 |
|
11080
26f1b3ad4a77
skip null frames in mpeg files, patch by Zoltan Hidvegi <mplayer@hzoli.2y.net>
attila
parents:
10663
diff
changeset
|
155 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
|
156 |
| 9859 | 157 // append extra 'end of frame' code: |
| 158 ((char*)data+len)[0]=0; | |
| 159 ((char*)data+len)[1]=0; | |
| 160 ((char*)data+len)[2]=1; | |
| 161 ((char*)data+len)[3]=0xff; | |
| 162 len+=4; | |
| 5465 | 163 |
| 13112 | 164 if (mpeg2dec->pending_length) { |
| 165 mpeg2_buffer (mpeg2dec, mpeg2dec->pending_buffer, mpeg2dec->pending_buffer + mpeg2dec->pending_length); | |
| 166 } else { | |
| 23916 | 167 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len); |
| 13112 | 168 } |
| 9859 | 169 |
| 170 while(1){ | |
| 171 int state=mpeg2_parse (mpeg2dec); | |
| 13995 | 172 int type, use_callback; |
| 173 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
|
174 unsigned long pw, ph; |
|
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
175 int imgfmt; |
| 13995 | 176 |
| 9859 | 177 switch(state){ |
| 12932 | 178 case STATE_BUFFER: |
| 13112 | 179 if (mpeg2dec->pending_length) { |
| 13152 | 180 // just finished the pending data, continue with processing of the passed buffer |
| 13112 | 181 mpeg2dec->pending_length = 0; |
| 23916 | 182 mpeg2_buffer (mpeg2dec, data, (uint8_t *)data+len); |
| 13112 | 183 } else { |
| 184 // parsing of the passed buffer finished, return. | |
| 185 return 0; | |
| 186 } | |
| 187 break; | |
| 9859 | 188 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
|
189 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
|
190 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
|
191 if(ph) sh->aspect = (float) pw / (float) ph; |
| 25962 | 192 // video parameters initialized/changed, (re)init libvo: |
| 13995 | 193 if (info->sequence->width >> 1 == info->sequence->chroma_width && |
| 194 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
|
195 imgfmt = IMGFMT_YV12; |
| 13995 | 196 } else if (info->sequence->width >> 1 == info->sequence->chroma_width && |
| 197 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
|
198 imgfmt = IMGFMT_422P; |
| 13995 | 199 } else return 0; |
|
25977
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
200 if (imgfmt == context->imgfmt && |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
201 info->sequence->picture_width == context->width && |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
202 info->sequence->picture_height == context->height && |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
203 sh->aspect == context->aspect) |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
204 break; |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
205 if(!mpcodecs_config_vo(sh, |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
206 info->sequence->picture_width, |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
207 info->sequence->picture_height, imgfmt)) |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
208 return 0; |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
209 context->imgfmt = imgfmt; |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
210 context->width = info->sequence->picture_width; |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
211 context->height = info->sequence->picture_height; |
|
e3be2de66969
Avoid reinit of vo with the exactly same parameters over and over.
reimar
parents:
25962
diff
changeset
|
212 context->aspect = sh->aspect; |
| 9859 | 213 break; |
| 13995 | 214 case STATE_PICTURE: |
| 215 type=info->current_picture->flags&PIC_MASK_CODING_TYPE; | |
| 9859 | 216 |
| 217 drop_frame = framedrop && (mpeg2dec->decoder.coding_type == B_TYPE); | |
| 218 drop_frame |= framedrop>=2; // hard drop | |
| 219 if (drop_frame) { | |
| 220 mpeg2_skip(mpeg2dec, 1); | |
| 221 //printf("Dropping Frame ...\n"); | |
| 222 break; | |
| 223 } | |
| 224 mpeg2_skip(mpeg2dec, 0); //mpeg2skip skips frames until set again to 0 | |
| 225 | |
| 14012 | 226 use_callback = (!framedrop && vd_use_slices && |
| 227 (info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) ? | |
| 228 MP_IMGFLAG_DRAW_CALLBACK:0; | |
| 13995 | 229 |
| 9859 | 230 // get_buffer "callback": |
| 13995 | 231 mpi_new=mpcodecs_get_image(sh,MP_IMGTYPE_IPB, |
| 14012 | 232 (type==PIC_FLAG_CODING_TYPE_B) ? |
| 233 use_callback : (MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE), | |
| 14075 | 234 info->sequence->width, |
| 235 info->sequence->height); | |
| 13995 | 236 |
| 13112 | 237 if(!mpi_new) return 0; // VO ERROR!!!!!!!! |
| 238 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
|
239 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
|
240 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
|
241 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
|
242 if (info->current_picture->flags&PIC_FLAG_TOP_FIELD_FIRST) |
| 13112 | 243 mpi_new->fields |= MP_IMGFIELD_TOP_FIRST; |
| 244 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
|
245 if (info->current_picture->flags&PIC_FLAG_REPEAT_FIRST_FIELD) |
| 13112 | 246 mpi_new->fields |= MP_IMGFIELD_REPEAT_FIRST; |
| 247 else mpi_new->fields &= ~MP_IMGFIELD_REPEAT_FIRST; | |
| 248 mpi_new->fields |= MP_IMGFIELD_ORDERED; | |
| 20639 | 249 if (!(info->current_picture->flags&PIC_FLAG_PROGRESSIVE_FRAME)) |
| 250 mpi_new->fields |= MP_IMGFIELD_INTERLACED; | |
| 9859 | 251 |
| 12935 | 252 #ifdef MPEG12_POSTPROC |
| 18301 | 253 mpi_new->qstride=info->sequence->width>>4; |
| 254 { | |
| 255 char **p = &context->quant_store[type==PIC_FLAG_CODING_TYPE_B ? | |
| 256 2 : (context->quant_store_idx ^= 1)]; | |
| 257 *p = realloc(*p, mpi_new->qstride*(info->sequence->height>>4)); | |
| 258 mpi_new->qscale = *p; | |
| 12935 | 259 } |
| 13112 | 260 mpeg2dec->decoder.quant_store=mpi_new->qscale; |
| 261 mpeg2dec->decoder.quant_stride=mpi_new->qstride; | |
| 262 mpi_new->pict_type=type; // 1->I, 2->P, 3->B | |
| 263 mpi_new->qscale_type= 1; | |
| 12935 | 264 #endif |
| 265 | |
| 14012 | 266 if (mpi_new->flags&MP_IMGFLAG_DRAW_CALLBACK |
| 267 && !(mpi_new->flags&MP_IMGFLAG_DIRECT)) { | |
| 268 // nice, filter/vo likes draw_callback :) | |
| 269 mpeg2dec->decoder.convert=draw_slice; | |
| 270 mpeg2dec->decoder.convert_id=sh; | |
| 271 } else { | |
| 272 mpeg2dec->decoder.convert=NULL; | |
| 273 mpeg2dec->decoder.convert_id=NULL; | |
| 13995 | 274 } |
| 275 | |
| 9859 | 276 break; |
| 277 case STATE_SLICE: | |
| 278 case STATE_END: | |
| 12932 | 279 case STATE_INVALID_END: |
| 9859 | 280 // decoding done: |
| 13112 | 281 if(info->display_fbuf) { |
| 13152 | 282 mp_image_t* mpi = info->display_fbuf->id; |
| 283 if (mpeg2dec->pending_length == 0) { | |
| 13995 | 284 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start; |
| 285 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length); | |
| 286 memcpy(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length); | |
| 13152 | 287 } else { |
| 288 // still some data in the pending buffer, shouldn't happen | |
| 289 mpeg2dec->pending_length = mpeg2dec->buf_end - mpeg2dec->buf_start; | |
| 290 memmove(mpeg2dec->pending_buffer, mpeg2dec->buf_start, mpeg2dec->pending_length); | |
| 291 mpeg2dec->pending_buffer = realloc(mpeg2dec->pending_buffer, mpeg2dec->pending_length + len); | |
| 292 memcpy(mpeg2dec->pending_buffer+mpeg2dec->pending_length, data, len); | |
| 293 mpeg2dec->pending_length += len; | |
| 294 } | |
| 295 // fprintf(stderr, "pending = %d\n", mpeg2dec->pending_length); | |
| 13112 | 296 return mpi; |
| 297 } | |
|
7957
31fd09cc9ba2
passing picture_type (might be usefull for postprocessing)
michael
parents:
7756
diff
changeset
|
298 } |
|
31fd09cc9ba2
passing picture_type (might be usefull for postprocessing)
michael
parents:
7756
diff
changeset
|
299 } |
| 5465 | 300 } |
