Mercurial > mplayer.hg
annotate libmpcodecs/vd_ffmpeg.c @ 6281:cd1cde2f3cc0
updated to english version
author | jonas |
---|---|
date | Mon, 03 Jun 2002 15:53:24 +0000 |
parents | f49ec39ab0c6 |
children | e97686ab386b |
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; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
58 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
59 struct config lavc_decode_opts_conf[]={ |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
60 #if LIBAVCODEC_BUILD >= 4611 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
61 {"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
|
62 {"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
|
63 #endif |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
64 {NULL, NULL, 0, 0, 0, 0, NULL} |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
65 }; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
66 |
4952 | 67 // to set/get/query special features/parameters |
68 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
|
69 vd_ffmpeg_ctx *ctx = sh->context; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
70 switch(cmd){ |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
71 case VDCTRL_QUERY_FORMAT: |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
72 if( (*((int*)arg)) == IMGFMT_YV12 ) return CONTROL_TRUE; |
5707 | 73 if( (*((int*)arg)) == IMGFMT_IYUV ) return CONTROL_TRUE; |
74 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
|
75 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
|
76 return CONTROL_FALSE; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
77 } |
4952 | 78 return CONTROL_UNKNOWN; |
79 } | |
80 | |
81 // init driver | |
82 static int init(sh_video_t *sh){ | |
5280 | 83 AVCodecContext *avctx; |
84 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
|
85 AVCodec *lavc_codec; |
4952 | 86 |
87 if(!avcodec_inited){ | |
88 avcodec_init(); | |
89 avcodec_register_all(); | |
90 avcodec_inited=1; | |
91 } | |
5280 | 92 |
93 ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx)); | |
94 if (!ctx) | |
95 return(0); | |
96 memset(ctx, 0, sizeof(vd_ffmpeg_ctx)); | |
4952 | 97 |
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
|
98 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
|
99 if(!lavc_codec){ |
4952 | 100 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh->codec->dll); |
101 return 0; | |
102 } | |
5482 | 103 |
5494
1c45b1484ffb
i just fixed b-frames & slices in libavcodec :) ... iam too tired for benchmarks now ...
michael
parents:
5482
diff
changeset
|
104 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
|
105 ctx->do_slices=1; |
4952 | 106 |
5280 | 107 ctx->avctx = malloc(sizeof(AVCodecContext)); |
108 memset(ctx->avctx, 0, sizeof(AVCodecContext)); | |
109 avctx = ctx->avctx; | |
4952 | 110 |
5280 | 111 avctx->width = sh->disp_w; |
112 avctx->height= sh->disp_h; | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
113 #if LIBAVCODEC_BUILD >= 4611 |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
114 avctx->workaround_bugs= lavc_param_workaround_bugs; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
115 avctx->error_resilience= lavc_param_error_resilience; |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
116 #endif |
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
117 |
5280 | 118 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"libavcodec.size: %d x %d\n",avctx->width,avctx->height); |
4952 | 119 if (sh->format == mmioFOURCC('R', 'V', '1', '3')) |
5280 | 120 avctx->sub_id = 3; |
5939 | 121 #if LIBAVCODEC_BUILD >= 4605 |
122 /* AVRn stores huffman table in AVI header */ | |
123 /* Pegasus MJPEG stores it also in AVI header, but it uses the common | |
124 MJPG fourcc :( */ | |
5940 | 125 if (sh->bih && (sh->bih->biSize != sizeof(BITMAPINFOHEADER)) && |
5939 | 126 (sh->format == mmioFOURCC('A','V','R','n') || |
127 sh->format == mmioFOURCC('M','J','P','G'))) | |
128 { | |
129 avctx->flags |= CODEC_FLAG_EXTERN_HUFF; | |
130 avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER); | |
131 avctx->extradata = malloc(avctx->extradata_size); | |
132 memcpy(avctx->extradata, sh->bih+sizeof(BITMAPINFOHEADER), | |
133 avctx->extradata_size); | |
134 | |
135 #if 0 | |
136 { | |
137 int x; | |
138 uint8_t *p = avctx->extradata; | |
139 | |
140 for (x=0; x<avctx->extradata_size; x++) | |
141 printf("[%x] ", p[x]); | |
142 printf("\n"); | |
143 } | |
144 #endif | |
145 } | |
146 #endif | |
4952 | 147 /* 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
|
148 if (avcodec_open(avctx, lavc_codec) < 0) { |
4952 | 149 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantOpenCodec); |
150 return 0; | |
151 } | |
152 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: libavcodec init OK!\n"); | |
5482 | 153 ctx->last_aspect=-3; |
5510 | 154 return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12); |
4952 | 155 } |
156 | |
157 // uninit driver | |
158 static void uninit(sh_video_t *sh){ | |
5280 | 159 vd_ffmpeg_ctx *ctx = sh->context; |
160 AVCodecContext *avctx = ctx->avctx; | |
161 | |
162 if (avcodec_close(avctx) < 0) | |
4952 | 163 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantCloseCodec); |
5939 | 164 |
165 #if LIBAVCODEC_BUILD >= 4605 | |
166 if (avctx->extradata_size) | |
167 free(avctx->extradata); | |
168 #endif | |
169 | |
5280 | 170 if (avctx) |
171 free(avctx); | |
172 if (ctx) | |
173 free(ctx); | |
4952 | 174 } |
175 | |
5482 | 176 #include "libvo/video_out.h" // FIXME!!! |
177 | |
178 static void draw_slice(struct AVCodecContext *s, | |
179 UINT8 **src, int linesize, | |
180 int y, int width, int height){ | |
181 vo_functions_t * output = s->opaque; | |
182 int stride[3]; | |
183 | |
184 stride[0]=linesize; | |
185 stride[1]=stride[2]=stride[0]/2; | |
186 | |
187 output->draw_slice (src, stride, width, height, 0, y); | |
188 | |
189 } | |
4952 | 190 |
191 // decode a frame | |
192 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){ | |
193 int got_picture=0; | |
194 int ret; | |
195 AVPicture lavc_picture; | |
5280 | 196 vd_ffmpeg_ctx *ctx = sh->context; |
197 AVCodecContext *avctx = ctx->avctx; | |
5482 | 198 mp_image_t* mpi=NULL; |
4952 | 199 |
200 if(len<=0) return NULL; // skipped frame | |
201 | |
5613 | 202 avctx->draw_horiz_band=NULL; |
203 if(ctx->vo_inited && !ctx->convert && !(flags&3)){ | |
5482 | 204 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE | |
205 (ctx->do_slices?MP_IMGFLAG_DRAW_CALLBACK:0), | |
206 sh->disp_w, sh->disp_h); | |
207 if(mpi && mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){ | |
208 // vd core likes slices! | |
209 avctx->draw_horiz_band=draw_slice; | |
210 avctx->opaque=sh->video_out; | |
5613 | 211 } |
5482 | 212 } |
5875 | 213 |
214 #if LIBAVCODEC_BUILD > 4603 | |
215 avctx->hurry_up=(flags&3)?((flags&2)?2:1):0; | |
216 #endif | |
217 | |
5280 | 218 ret = avcodec_decode_video(avctx, &lavc_picture, |
4952 | 219 &got_picture, data, len); |
220 if(ret<0) mp_msg(MSGT_DECVIDEO,MSGL_WARN, "Error while decoding frame!\n"); | |
221 if(!got_picture) return NULL; // skipped image | |
6265
f49ec39ab0c6
workaround bugs & error resilience ffmpeg decoder options
michael
parents:
5940
diff
changeset
|
222 |
5482 | 223 if (avctx->aspect_ratio_info != ctx->last_aspect || |
224 avctx->width != sh->disp_w || | |
225 avctx->height != sh->disp_h || | |
226 !ctx->vo_inited) | |
5280 | 227 { |
228 ctx->last_aspect = avctx->aspect_ratio_info; | |
229 switch(avctx->aspect_ratio_info) | |
230 { | |
231 case FF_ASPECT_4_3_625: | |
232 case FF_ASPECT_4_3_525: | |
233 sh->aspect = 4.0/3.0; | |
234 break; | |
235 case FF_ASPECT_16_9_625: | |
236 case FF_ASPECT_16_9_525: | |
237 sh->aspect = 16.0/9.0; | |
238 break; | |
239 case FF_ASPECT_SQUARE: | |
240 sh->aspect = 0.0; | |
241 break; | |
242 } | |
5482 | 243 sh->disp_w = avctx->width; |
244 sh->disp_h = avctx->height; | |
245 ctx->vo_inited=1; | |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
246 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
|
247 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
|
248 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
|
249 return NULL; |
5592
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
250 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
|
251 } |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
252 |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
253 if(!mpi && ctx->convert){ |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
254 // do yuv422p -> yuy2 conversion: |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
255 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
|
256 avctx->width, avctx->height); |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
257 if(!mpi) return NULL; |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
258 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
|
259 mpi->planes[0],avctx->width,avctx->height, |
b545d56314d2
yuy2 support, fixed bug of dropping frame after config_vo calls
arpi
parents:
5517
diff
changeset
|
260 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
|
261 return mpi; |
5280 | 262 } |
4952 | 263 |
5482 | 264 if(!mpi) |
4952 | 265 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE, |
5280 | 266 avctx->width, avctx->height); |
4952 | 267 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
|
268 printf("couldn't allocate image for codec\n"); |
4952 | 269 return NULL; |
270 } | |
271 | |
272 mpi->planes[0]=lavc_picture.data[0]; | |
273 mpi->planes[1]=lavc_picture.data[1]; | |
274 mpi->planes[2]=lavc_picture.data[2]; | |
275 mpi->stride[0]=lavc_picture.linesize[0]; | |
276 mpi->stride[1]=lavc_picture.linesize[1]; | |
277 mpi->stride[2]=lavc_picture.linesize[2]; | |
278 | |
5280 | 279 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
|
280 // we have 422p but user wants yv12 (420p) |
4952 | 281 mpi->stride[1]*=2; |
282 mpi->stride[2]*=2; | |
283 } | |
284 | |
5517 | 285 #ifdef FF_POSTPROCESS |
286 mpi->qscale=&quant_store[0][0]; | |
287 mpi->qstride=MBC+1; | |
288 #endif | |
289 | |
4952 | 290 return mpi; |
291 } | |
292 | |
293 #endif | |
294 |