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