Mercurial > mplayer.hg
annotate libmpcodecs/vd_ffmpeg.c @ 6505:e3a166f8e81f
imported from xine, generalized, created header file, all tables moved to
svq1_cb.h (was: svq1_codebooks.h)
TODO: use libavcodec's MC and bit parsing routines, maybe VLC too,
finally move the whole decoder to libavcodec
author | arpi |
---|---|
date | Sat, 22 Jun 2002 23:07:41 +0000 |
parents | e97686ab386b |
children | 3284abe2d73f |
rev | line source |
---|---|
4952 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <assert.h> | |
4 | |
5 #include "config.h" | |
6 #include "mp_msg.h" | |
7 #include "help_mp.h" | |
8 | |
9 #ifdef USE_LIBAVCODEC | |
10 | |
11 #include "bswap.h" | |
12 | |
13 #include "vd_internal.h" | |
14 | |
15 static vd_info_t info = { | |
16 "FFmpeg's libavcodec codec family", | |
17 "ffmpeg", | |
18 VFM_FFMPEG, | |
19 "A'rpi", | |
20 "http://ffmpeg.sf.net", | |
21 "native codecs" | |
22 }; | |
23 | |
24 LIBVD_EXTERN(ffmpeg) | |
25 | |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
26 #include "../postproc/rgb2rgb.h" |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
27 |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
28 |
4952 | 29 #ifdef USE_LIBAVCODEC_SO |
30 #include <libffmpeg/avcodec.h> | |
31 #else | |
32 #include "libavcodec/avcodec.h" | |
33 #endif | |
34 | |
35 int avcodec_inited=0; | |
36 | |
5517 | 37 #ifdef FF_POSTPROCESS |
38 int quant_store[MBR+1][MBC+1]; | |
39 #endif | |
40 | |
5280 | 41 typedef struct { |
42 AVCodecContext *avctx; | |
5482 | 43 int last_aspect; |
44 int do_slices; | |
45 int vo_inited; | |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
46 int convert; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
47 int yuy2_support; |
5280 | 48 } vd_ffmpeg_ctx; |
49 | |
4952 | 50 //#ifdef FF_POSTPROCESS |
51 //unsigned int lavc_pp=0; | |
52 //#endif | |
53 | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
54 #include "cfgparser.h" |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
55 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
56 static int lavc_param_workaround_bugs=0; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
57 static int lavc_param_error_resilience=0; |
6355 | 58 static int lavc_param_gray=0; |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
59 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
60 struct config lavc_decode_opts_conf[]={ |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
61 #if LIBAVCODEC_BUILD >= 4611 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
62 {"bug", &lavc_param_workaround_bugs, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL}, |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
63 {"ver", &lavc_param_error_resilience, CONF_TYPE_INT, CONF_RANGE, -1, 99, NULL}, |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
64 #endif |
6355 | 65 #if LIBAVCODEC_BUILD >= 4614 |
66 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, | |
67 #endif | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
68 {NULL, NULL, 0, 0, 0, 0, NULL} |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
69 }; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
70 |
4952 | 71 // to set/get/query special features/parameters |
72 static int control(sh_video_t *sh,int cmd,void* arg,...){ | |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
73 vd_ffmpeg_ctx *ctx = sh->context; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
74 switch(cmd){ |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
75 case VDCTRL_QUERY_FORMAT: |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
76 if( (*((int*)arg)) == IMGFMT_YV12 ) return CONTROL_TRUE; |
5707 | 77 if( (*((int*)arg)) == IMGFMT_IYUV ) return CONTROL_TRUE; |
78 if( (*((int*)arg)) == IMGFMT_I420 ) return CONTROL_TRUE; | |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
79 if( (*((int*)arg)) == IMGFMT_YUY2 && ctx->yuy2_support ) return CONTROL_TRUE; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
80 return CONTROL_FALSE; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
81 } |
4952 | 82 return CONTROL_UNKNOWN; |
83 } | |
84 | |
85 // init driver | |
86 static int init(sh_video_t *sh){ | |
5280 | 87 AVCodecContext *avctx; |
88 vd_ffmpeg_ctx *ctx; | |
5457
f248c9e86423
config vo only if aspect really changed and width&&height isn't changed (if w||h changes, we set it later)
alex
parents:
5331
diff
changeset
|
89 AVCodec *lavc_codec; |
4952 | 90 |
91 if(!avcodec_inited){ | |
92 avcodec_init(); | |
93 avcodec_register_all(); | |
94 avcodec_inited=1; | |
95 } | |
5280 | 96 |
97 ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx)); | |
98 if (!ctx) | |
99 return(0); | |
100 memset(ctx, 0, sizeof(vd_ffmpeg_ctx)); | |
4952 | 101 |
5457
f248c9e86423
config vo only if aspect really changed and width&&height isn't changed (if w||h changes, we set it later)
alex
parents:
5331
diff
changeset
|
102 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh->codec->dll); |
f248c9e86423
config vo only if aspect really changed and width&&height isn't changed (if w||h changes, we set it later)
alex
parents:
5331
diff
changeset
|
103 if(!lavc_codec){ |
4952 | 104 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh->codec->dll); |
105 return 0; | |
106 } | |
5482 | 107 |
5494
1c45b1484ffb
i just fixed b-frames & slices in libavcodec :) ... iam too tired for benchmarks now ...
michael
parents:
5482
diff
changeset
|
108 if(vd_use_slices && lavc_codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND) |
1c45b1484ffb
i just fixed b-frames & slices in libavcodec :) ... iam too tired for benchmarks now ...
michael
parents:
5482
diff
changeset
|
109 ctx->do_slices=1; |
4952 | 110 |
5280 | 111 ctx->avctx = malloc(sizeof(AVCodecContext)); |
112 memset(ctx->avctx, 0, sizeof(AVCodecContext)); | |
113 avctx = ctx->avctx; | |
4952 | 114 |
5280 | 115 avctx->width = sh->disp_w; |
116 avctx->height= sh->disp_h; | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
117 #if LIBAVCODEC_BUILD >= 4611 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
118 avctx->workaround_bugs= lavc_param_workaround_bugs; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
119 avctx->error_resilience= lavc_param_error_resilience; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
120 #endif |
6355 | 121 #if LIBAVCODEC_BUILD >= 4614 |
122 if(lavc_param_gray) avctx->flags|= CODEC_FLAG_GRAY; | |
123 #endif | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
124 |
5280 | 125 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"libavcodec.size: %d x %d\n",avctx->width,avctx->height); |
4952 | 126 if (sh->format == mmioFOURCC('R', 'V', '1', '3')) |
5280 | 127 avctx->sub_id = 3; |
5939 | 128 #if LIBAVCODEC_BUILD >= 4605 |
129 /* AVRn stores huffman table in AVI header */ | |
130 /* Pegasus MJPEG stores it also in AVI header, but it uses the common | |
131 MJPG fourcc :( */ | |
5940 | 132 if (sh->bih && (sh->bih->biSize != sizeof(BITMAPINFOHEADER)) && |
5939 | 133 (sh->format == mmioFOURCC('A','V','R','n') || |
134 sh->format == mmioFOURCC('M','J','P','G'))) | |
135 { | |
136 avctx->flags |= CODEC_FLAG_EXTERN_HUFF; | |
137 avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER); | |
138 avctx->extradata = malloc(avctx->extradata_size); | |
139 memcpy(avctx->extradata, sh->bih+sizeof(BITMAPINFOHEADER), | |
140 avctx->extradata_size); | |
141 | |
142 #if 0 | |
143 { | |
144 int x; | |
145 uint8_t *p = avctx->extradata; | |
146 | |
147 for (x=0; x<avctx->extradata_size; x++) | |
148 printf("[%x] ", p[x]); | |
149 printf("\n"); | |
150 } | |
151 #endif | |
152 } | |
153 #endif | |
4952 | 154 /* open it */ |
5457
f248c9e86423
config vo only if aspect really changed and width&&height isn't changed (if w||h changes, we set it later)
alex
parents:
5331
diff
changeset
|
155 if (avcodec_open(avctx, lavc_codec) < 0) { |
4952 | 156 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantOpenCodec); |
157 return 0; | |
158 } | |
159 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: libavcodec init OK!\n"); | |
5482 | 160 ctx->last_aspect=-3; |
5510 | 161 return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12); |
4952 | 162 } |
163 | |
164 // uninit driver | |
165 static void uninit(sh_video_t *sh){ | |
5280 | 166 vd_ffmpeg_ctx *ctx = sh->context; |
167 AVCodecContext *avctx = ctx->avctx; | |
168 | |
169 if (avcodec_close(avctx) < 0) | |
4952 | 170 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantCloseCodec); |
5939 | 171 |
172 #if LIBAVCODEC_BUILD >= 4605 | |
173 if (avctx->extradata_size) | |
174 free(avctx->extradata); | |
175 #endif | |
176 | |
5280 | 177 if (avctx) |
178 free(avctx); | |
179 if (ctx) | |
180 free(ctx); | |
4952 | 181 } |
182 | |
5482 | 183 #include "libvo/video_out.h" // FIXME!!! |
184 | |
185 static void draw_slice(struct AVCodecContext *s, | |
186 UINT8 **src, int linesize, | |
187 int y, int width, int height){ | |
188 vo_functions_t * output = s->opaque; | |
189 int stride[3]; | |
190 | |
191 stride[0]=linesize; | |
192 stride[1]=stride[2]=stride[0]/2; | |
193 | |
194 output->draw_slice (src, stride, width, height, 0, y); | |
195 | |
196 } | |
4952 | 197 |
198 // decode a frame | |
199 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){ | |
200 int got_picture=0; | |
201 int ret; | |
202 AVPicture lavc_picture; | |
5280 | 203 vd_ffmpeg_ctx *ctx = sh->context; |
204 AVCodecContext *avctx = ctx->avctx; | |
5482 | 205 mp_image_t* mpi=NULL; |
4952 | 206 |
207 if(len<=0) return NULL; // skipped frame | |
208 | |
5613 | 209 avctx->draw_horiz_band=NULL; |
210 if(ctx->vo_inited && !ctx->convert && !(flags&3)){ | |
5482 | 211 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE | |
212 (ctx->do_slices?MP_IMGFLAG_DRAW_CALLBACK:0), | |
213 sh->disp_w, sh->disp_h); | |
214 if(mpi && mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){ | |
215 // vd core likes slices! | |
216 avctx->draw_horiz_band=draw_slice; | |
217 avctx->opaque=sh->video_out; | |
5613 | 218 } |
5482 | 219 } |
5875 | 220 |
221 #if LIBAVCODEC_BUILD > 4603 | |
222 avctx->hurry_up=(flags&3)?((flags&2)?2:1):0; | |
223 #endif | |
224 | |
5280 | 225 ret = avcodec_decode_video(avctx, &lavc_picture, |
4952 | 226 &got_picture, data, len); |
227 if(ret<0) mp_msg(MSGT_DECVIDEO,MSGL_WARN, "Error while decoding frame!\n"); | |
228 if(!got_picture) return NULL; // skipped image | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
229 |
5482 | 230 if (avctx->aspect_ratio_info != ctx->last_aspect || |
231 avctx->width != sh->disp_w || | |
232 avctx->height != sh->disp_h || | |
233 !ctx->vo_inited) | |
5280 | 234 { |
235 ctx->last_aspect = avctx->aspect_ratio_info; | |
236 switch(avctx->aspect_ratio_info) | |
237 { | |
238 case FF_ASPECT_4_3_625: | |
239 case FF_ASPECT_4_3_525: | |
240 sh->aspect = 4.0/3.0; | |
241 break; | |
242 case FF_ASPECT_16_9_625: | |
243 case FF_ASPECT_16_9_525: | |
244 sh->aspect = 16.0/9.0; | |
245 break; | |
246 case FF_ASPECT_SQUARE: | |
247 sh->aspect = 0.0; | |
248 break; | |
249 } | |
5482 | 250 sh->disp_w = avctx->width; |
251 sh->disp_h = avctx->height; | |
252 ctx->vo_inited=1; | |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
253 ctx->yuy2_support=(avctx->pix_fmt==PIX_FMT_YUV422P); |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
254 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h, |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
255 ctx->yuy2_support ? IMGFMT_YUY2 : IMGFMT_YV12)) |
5457
f248c9e86423
config vo only if aspect really changed and width&&height isn't changed (if w||h changes, we set it later)
alex
parents:
5331
diff
changeset
|
256 return NULL; |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
257 ctx->convert=(sh->codec->outfmt[sh->outfmtidx]==IMGFMT_YUY2); |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
258 } |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
259 |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
260 if(!mpi && ctx->convert){ |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
261 // do yuv422p -> yuy2 conversion: |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
262 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
263 avctx->width, avctx->height); |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
264 if(!mpi) return NULL; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
265 yuv422ptoyuy2(lavc_picture.data[0],lavc_picture.data[1],lavc_picture.data[2], |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
266 mpi->planes[0],avctx->width,avctx->height, |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
267 lavc_picture.linesize[0],lavc_picture.linesize[1],mpi->stride[0]); |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
268 return mpi; |
5280 | 269 } |
4952 | 270 |
5482 | 271 if(!mpi) |
4952 | 272 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE, |
5280 | 273 avctx->width, avctx->height); |
4952 | 274 if(!mpi){ // temporary! |
5207
d337cc4ab0ee
config vo if resolution changed (after decoded image read the dimensions out of lavc context)
alex
parents:
5124
diff
changeset
|
275 printf("couldn't allocate image for codec\n"); |
4952 | 276 return NULL; |
277 } | |
278 | |
279 mpi->planes[0]=lavc_picture.data[0]; | |
280 mpi->planes[1]=lavc_picture.data[1]; | |
281 mpi->planes[2]=lavc_picture.data[2]; | |
282 mpi->stride[0]=lavc_picture.linesize[0]; | |
283 mpi->stride[1]=lavc_picture.linesize[1]; | |
284 mpi->stride[2]=lavc_picture.linesize[2]; | |
285 | |
5280 | 286 if(avctx->pix_fmt==PIX_FMT_YUV422P){ |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
287 // we have 422p but user wants yv12 (420p) |
4952 | 288 mpi->stride[1]*=2; |
289 mpi->stride[2]*=2; | |
290 } | |
291 | |
5517 | 292 #ifdef FF_POSTPROCESS |
293 mpi->qscale=&quant_store[0][0]; | |
294 mpi->qstride=MBC+1; | |
295 #endif | |
296 | |
4952 | 297 return mpi; |
298 } | |
299 | |
300 #endif | |
301 |