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