fix
[mplayer/glamo.git] / libmpcodecs / vd_ffmpeg.c
blob45404e29f761e294d888ec8a81dc625ceea46191
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <time.h>
6 #include "config.h"
7 #include "mp_msg.h"
8 #include "help_mp.h"
10 #ifdef USE_LIBAVCODEC
12 #include "bswap.h"
14 #include "vd_internal.h"
16 static vd_info_t info = {
17 "FFmpeg's libavcodec codec family",
18 "ffmpeg",
19 "A'rpi",
20 "A'rpi, Michael, Alex",
21 "native codecs (http://ffmpeg.sf.net/)"
24 LIBVD_EXTERN(ffmpeg)
26 #ifdef USE_LIBAVCODEC_SO
27 #include <ffmpeg/avcodec.h>
28 #else
29 #include "libavcodec/avcodec.h"
30 #endif
32 #if LIBAVCODEC_BUILD < 4641
33 #error we do not support libavcodec prior to build 4641, get the latest libavcodec CVS
34 #endif
36 #if LIBAVCODEC_BUILD < 4645
37 #warning your version of libavcodec is old, u might want to get a newer one
38 #endif
40 #if LIBAVCODEC_BUILD < 4645
41 #define AVFrame AVVideoFrame
42 #define coded_frame coded_picture
43 #endif
45 #if LIBAVCODEC_BUILD < 4654
46 #define PIX_FMT_RGB24 PIX_FMT_BGR24
47 #define PIX_FMT_RGBA32 PIX_FMT_BGRA32
48 #endif
50 #if LIBAVCODEC_BUILD < 4672
51 #undef HAVE_XVMC
52 #endif
54 #ifdef HAVE_XVMC
55 #include "xvmc_render.h"
56 #endif
58 int avcodec_inited=0;
60 typedef struct {
61 AVCodecContext *avctx;
62 AVFrame *pic;
63 float last_aspect;
64 int do_slices;
65 int do_dr1;
66 int vo_inited;
67 int best_csp;
68 int b_age;
69 int ip_age[2];
70 int qp_stat[32];
71 double qp_sum;
72 double inv_qp_sum;
73 int ip_count;
74 int b_count;
75 } vd_ffmpeg_ctx;
77 //#ifdef FF_POSTPROCESS
78 //unsigned int lavc_pp=0;
79 //#endif
81 #include "m_option.h"
83 static int get_buffer(AVCodecContext *avctx, AVFrame *pic);
84 static void release_buffer(AVCodecContext *avctx, AVFrame *pic);
85 static enum PixelFormat get_format(struct AVCodecContext * avctx,
86 const enum PixelFormat * pix_fmt);
88 #ifdef HAVE_XVMC
89 static int mc_get_buffer(AVCodecContext *avctx, AVFrame *pic);
90 static void mc_release_buffer(AVCodecContext *avctx, AVFrame *pic);
91 static void mc_render_slice(struct AVCodecContext *s,
92 AVFrame *src, int offset[4],
93 int y, int type, int height);
94 #endif
96 static int lavc_param_workaround_bugs= FF_BUG_AUTODETECT;
97 static int lavc_param_error_resilience=2;
98 static int lavc_param_error_concealment=3;
99 static int lavc_param_gray=0;
100 static int lavc_param_vstats=0;
101 static int lavc_param_idct_algo=0;
102 static int lavc_param_debug=0;
103 static int lavc_param_vismv=0;
104 static int lavc_param_skip_top=0;
105 static int lavc_param_skip_bottom=0;
106 static int lavc_param_fast=0;
107 static int lavc_param_lowres=0;
108 static char *lavc_param_lowres_str=NULL;
109 #if LIBAVCODEC_BUILD >= 4758
110 static char *lavc_param_skip_loop_filter_str = NULL;
111 static char *lavc_param_skip_idct_str = NULL;
112 static char *lavc_param_skip_frame_str = NULL;
113 #endif
114 static int lavc_param_threads=1;
116 m_option_t lavc_decode_opts_conf[]={
117 {"bug", &lavc_param_workaround_bugs, CONF_TYPE_INT, CONF_RANGE, -1, 999999, NULL},
118 {"er", &lavc_param_error_resilience, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL},
119 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
120 {"idct", &lavc_param_idct_algo, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL},
121 {"ec", &lavc_param_error_concealment, CONF_TYPE_INT, CONF_RANGE, 0, 99, NULL},
122 {"vstats", &lavc_param_vstats, CONF_TYPE_FLAG, 0, 0, 1, NULL},
123 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 9999999, NULL},
124 {"vismv", &lavc_param_vismv, CONF_TYPE_INT, CONF_RANGE, 0, 9999999, NULL},
125 {"st", &lavc_param_skip_top, CONF_TYPE_INT, CONF_RANGE, 0, 999, NULL},
126 {"sb", &lavc_param_skip_bottom, CONF_TYPE_INT, CONF_RANGE, 0, 999, NULL},
127 #ifdef CODEC_FLAG2_FAST
128 {"fast", &lavc_param_fast, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG2_FAST, NULL},
129 #endif
130 {"lowres", &lavc_param_lowres_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
131 #if LIBAVCODEC_BUILD >= 4758
132 {"skiploopfilter", &lavc_param_skip_loop_filter_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
133 {"skipidct", &lavc_param_skip_idct_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
134 {"skipframe", &lavc_param_skip_frame_str, CONF_TYPE_STRING, 0, 0, 0, NULL},
135 #endif
136 {"threads", &lavc_param_threads, CONF_TYPE_INT, CONF_RANGE, 1, 8, NULL},
137 {NULL, NULL, 0, 0, 0, 0, NULL}
140 #if LIBAVCODEC_BUILD >= 4758
141 static enum AVDiscard str2AVDiscard(char *str) {
142 if (!str)
143 return AVDISCARD_DEFAULT;
144 if (strcasecmp(str, "none") == 0)
145 return AVDISCARD_NONE;
146 if (strcasecmp(str, "default") == 0)
147 return AVDISCARD_DEFAULT;
148 if (strcasecmp(str, "nonref") == 0)
149 return AVDISCARD_NONREF;
150 if (strcasecmp(str, "bidir") == 0)
151 return AVDISCARD_BIDIR;
152 if (strcasecmp(str, "nonkey") == 0)
153 return AVDISCARD_NONKEY;
154 if (strcasecmp(str, "all") == 0)
155 return AVDISCARD_ALL;
156 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Unknown discard value %s\n", str);
157 return AVDISCARD_DEFAULT;
159 #endif
161 // to set/get/query special features/parameters
162 static int control(sh_video_t *sh,int cmd,void* arg,...){
163 vd_ffmpeg_ctx *ctx = sh->context;
164 AVCodecContext *avctx = ctx->avctx;
165 switch(cmd){
166 case VDCTRL_QUERY_FORMAT:
168 int format =(*((int*)arg));
169 if( format == ctx->best_csp ) return CONTROL_TRUE;//supported
170 // possible conversions:
171 switch( format ){
172 case IMGFMT_YV12:
173 case IMGFMT_IYUV:
174 case IMGFMT_I420:
175 // "converted" using pointer/stride modification
176 if(avctx->pix_fmt==PIX_FMT_YUV420P) return CONTROL_TRUE;// u/v swap
177 if(avctx->pix_fmt==PIX_FMT_YUV422P && !ctx->do_dr1) return CONTROL_TRUE;// half stride
178 break;
179 #ifdef HAVE_XVMC
180 case IMGFMT_XVMC_IDCT_MPEG2:
181 case IMGFMT_XVMC_MOCO_MPEG2:
182 if(avctx->pix_fmt==PIX_FMT_XVMC_MPEG2_IDCT) return CONTROL_TRUE;
183 #endif
185 return CONTROL_FALSE;
187 break;
188 case VDCTRL_RESYNC_STREAM:
189 avcodec_flush_buffers(avctx);
190 return CONTROL_TRUE;
192 return CONTROL_UNKNOWN;
195 // init driver
196 static int init(sh_video_t *sh){
197 AVCodecContext *avctx;
198 vd_ffmpeg_ctx *ctx;
199 AVCodec *lavc_codec;
200 #if LIBAVCODEC_BUILD >= 4722
201 int lowres_w=0;
202 #endif
203 int do_vis_debug= lavc_param_vismv || (lavc_param_debug&(FF_DEBUG_VIS_MB_TYPE|FF_DEBUG_VIS_QP));
205 if(!avcodec_inited){
206 avcodec_init();
207 avcodec_register_all();
208 avcodec_inited=1;
211 ctx = sh->context = malloc(sizeof(vd_ffmpeg_ctx));
212 if (!ctx)
213 return(0);
214 memset(ctx, 0, sizeof(vd_ffmpeg_ctx));
216 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh->codec->dll);
217 if(!lavc_codec){
218 mp_msg(MSGT_DECVIDEO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh->codec->dll);
219 return 0;
222 if(vd_use_slices && (lavc_codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND) && !do_vis_debug)
223 ctx->do_slices=1;
225 if(lavc_codec->capabilities&CODEC_CAP_DR1 && !do_vis_debug && lavc_codec->id != CODEC_ID_H264)
226 ctx->do_dr1=1;
227 ctx->b_age= ctx->ip_age[0]= ctx->ip_age[1]= 256*256*256*64;
228 ctx->ip_count= ctx->b_count= 0;
230 #if LIBAVCODEC_BUILD >= 4645
231 ctx->pic = avcodec_alloc_frame();
232 #else
233 ctx->pic = avcodec_alloc_picture();
234 #endif
235 ctx->avctx = avcodec_alloc_context();
236 avctx = ctx->avctx;
238 #if LIBAVCODEC_BUILD >= 4691 && LIBAVCODEC_BUILD <= 4692
239 if(lavc_codec->capabilities&CODEC_CAP_CR)
240 avctx->cr_available = 1;
241 #endif
243 #ifdef HAVE_XVMC
245 #ifdef CODEC_CAP_HWACCEL
246 if(lavc_codec->capabilities & CODEC_CAP_HWACCEL){
247 #else
248 if(lavc_codec->id == CODEC_ID_MPEG2VIDEO_XVMC){
249 #endif
250 printf("vd_ffmpeg: XVMC accelerated codec\n");
251 assert(ctx->do_dr1);//these are must to!
252 assert(ctx->do_slices); //it is (vo_)ffmpeg bug if this fails
253 avctx->flags|= CODEC_FLAG_EMU_EDGE;//do i need that??!!
254 avctx->get_format= get_format;//for now only this decoder will use it
255 avctx->get_buffer= mc_get_buffer;
256 avctx->release_buffer= mc_release_buffer;
257 avctx->draw_horiz_band = mc_render_slice;
258 avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
259 }else
260 #endif
261 if(ctx->do_dr1){
262 avctx->flags|= CODEC_FLAG_EMU_EDGE;
263 avctx->get_buffer= get_buffer;
264 avctx->release_buffer= release_buffer;
265 #if LIBAVCODEC_BUILD >= 4693
266 avctx->reget_buffer= get_buffer;
267 #endif
270 #ifdef CODEC_FLAG_NOT_TRUNCATED
271 avctx->flags|= CODEC_FLAG_NOT_TRUNCATED;
272 #endif
274 avctx->width = sh->disp_w;
275 avctx->height= sh->disp_h;
276 avctx->workaround_bugs= lavc_param_workaround_bugs;
277 avctx->error_resilience= lavc_param_error_resilience;
278 if(lavc_param_gray) avctx->flags|= CODEC_FLAG_GRAY;
279 #ifdef CODEC_FLAG2_FAST
280 avctx->flags2|= lavc_param_fast;
281 #endif
282 avctx->codec_tag= sh->format;
283 #if LIBAVCODEC_BUILD >= 4679
284 avctx->stream_codec_tag= sh->video.fccHandler;
285 #endif
286 avctx->idct_algo= lavc_param_idct_algo;
287 avctx->error_concealment= lavc_param_error_concealment;
288 #if LIBAVCODEC_BUILD >= 4642
289 avctx->debug= lavc_param_debug;
290 if (lavc_param_debug)
291 av_log_set_level(AV_LOG_DEBUG);
292 #endif
293 #if LIBAVCODEC_BUILD >= 4698
294 avctx->debug_mv= lavc_param_vismv;
295 #endif
296 #if LIBAVCODEC_BUILD >= 4717
297 avctx->skip_top = lavc_param_skip_top;
298 avctx->skip_bottom= lavc_param_skip_bottom;
299 #endif
300 #if LIBAVCODEC_BUILD >= 4722
301 if(lavc_param_lowres_str != NULL)
303 sscanf(lavc_param_lowres_str, "%d,%d", &lavc_param_lowres, &lowres_w);
304 if(lavc_param_lowres < 1 || lavc_param_lowres > 16 || (lowres_w > 0 && avctx->width < lowres_w))
305 lavc_param_lowres = 0;
306 avctx->lowres = lavc_param_lowres;
308 #endif
309 #if LIBAVCODEC_BUILD >= 4758
310 avctx->skip_loop_filter = str2AVDiscard(lavc_param_skip_loop_filter_str);
311 avctx->skip_idct = str2AVDiscard(lavc_param_skip_idct_str);
312 avctx->skip_frame = str2AVDiscard(lavc_param_skip_frame_str);
313 #endif
314 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2,"libavcodec.size: %d x %d\n",avctx->width,avctx->height);
315 /* AVRn stores huffman table in AVI header */
316 /* Pegasus MJPEG stores it also in AVI header, but it uses the common
317 MJPG fourcc :( */
318 if (sh->bih && (sh->bih->biSize != sizeof(BITMAPINFOHEADER)) &&
319 (sh->format == mmioFOURCC('A','V','R','n') ||
320 sh->format == mmioFOURCC('M','J','P','G')))
322 avctx->flags |= CODEC_FLAG_EXTERN_HUFF;
323 avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER);
324 avctx->extradata = av_malloc(avctx->extradata_size);
325 memcpy(avctx->extradata, sh->bih+sizeof(BITMAPINFOHEADER),
326 avctx->extradata_size);
328 #if 0
330 int x;
331 uint8_t *p = avctx->extradata;
333 for (x=0; x<avctx->extradata_size; x++)
334 printf("[%x] ", p[x]);
335 printf("\n");
337 #endif
339 if( sh->format == mmioFOURCC('R', 'V', '1', '0')
340 || sh->format == mmioFOURCC('R', 'V', '1', '3')
341 || sh->format == mmioFOURCC('R', 'V', '2', '0')
342 || sh->format == mmioFOURCC('R', 'V', '3', '0')
343 || sh->format == mmioFOURCC('R', 'V', '4', '0')
345 avctx->extradata_size= 8;
346 avctx->extradata = av_malloc(avctx->extradata_size);
347 if(sh->bih->biSize!=sizeof(*sh->bih)+8){
348 /* only 1 packet per frame & sub_id from fourcc */
349 ((uint32_t*)avctx->extradata)[0] = 0;
350 avctx->sub_id=
351 ((uint32_t*)avctx->extradata)[1] =
352 (sh->format == mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
353 } else {
354 /* has extra slice header (demux_rm or rm->avi streamcopy) */
355 unsigned int* extrahdr=(unsigned int*)(sh->bih+1);
356 ((uint32_t*)avctx->extradata)[0] = be2me_32(extrahdr[0]);
357 avctx->sub_id= extrahdr[1];
358 ((uint32_t*)avctx->extradata)[1] = be2me_32(extrahdr[1]);
361 // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]);
363 if (sh->bih && (sh->bih->biSize != sizeof(BITMAPINFOHEADER)) &&
364 (sh->format == mmioFOURCC('M','4','S','2') ||
365 sh->format == mmioFOURCC('M','P','4','S') ||
366 sh->format == mmioFOURCC('H','F','Y','U') ||
367 sh->format == mmioFOURCC('F','F','V','H') ||
368 sh->format == mmioFOURCC('W','M','V','2') ||
369 sh->format == mmioFOURCC('W','M','V','3') ||
370 sh->format == mmioFOURCC('A','S','V','1') ||
371 sh->format == mmioFOURCC('A','S','V','2') ||
372 sh->format == mmioFOURCC('V','S','S','H') ||
373 sh->format == mmioFOURCC('M','S','Z','H') ||
374 sh->format == mmioFOURCC('Z','L','I','B') ||
375 sh->format == mmioFOURCC('M','P','4','V') ||
376 sh->format == mmioFOURCC('F','L','I','C') ||
377 sh->format == mmioFOURCC('S','N','O','W') ||
378 sh->format == mmioFOURCC('a','v','c','1') ||
379 sh->format == mmioFOURCC('L','O','C','O') ||
380 sh->format == mmioFOURCC('t','h','e','o')
383 avctx->extradata_size = sh->bih->biSize-sizeof(BITMAPINFOHEADER);
384 avctx->extradata = av_malloc(avctx->extradata_size);
385 memcpy(avctx->extradata, sh->bih+1, avctx->extradata_size);
387 /* Pass palette to codec */
388 #if LIBAVCODEC_BUILD >= 4689
389 if (sh->bih && (sh->bih->biBitCount <= 8)) {
390 avctx->palctrl = (AVPaletteControl*)calloc(1,sizeof(AVPaletteControl));
391 avctx->palctrl->palette_changed = 1;
392 if (sh->bih->biSize-sizeof(BITMAPINFOHEADER))
393 /* Palette size in biSize */
394 memcpy(avctx->palctrl->palette, sh->bih+1,
395 min(sh->bih->biSize-sizeof(BITMAPINFOHEADER), AVPALETTE_SIZE));
396 else
397 /* Palette size in biClrUsed */
398 memcpy(avctx->palctrl->palette, sh->bih+1,
399 min(sh->bih->biClrUsed * 4, AVPALETTE_SIZE));
401 #endif
402 if (sh->ImageDesc &&
403 sh->format == mmioFOURCC('S','V','Q','3')){
404 avctx->extradata_size = (*(int*)sh->ImageDesc) - sizeof(int);
405 avctx->extradata = av_malloc(avctx->extradata_size);
406 memcpy(avctx->extradata, ((int*)sh->ImageDesc)+1, avctx->extradata_size);
409 if(sh->bih)
410 avctx->bits_per_sample= sh->bih->biBitCount;
412 #if LIBAVCODEC_BUILD >= 4716
413 if(lavc_param_threads > 1)
414 avcodec_thread_init(avctx, lavc_param_threads);
415 #endif
416 /* open it */
417 if (avcodec_open(avctx, lavc_codec) < 0) {
418 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantOpenCodec);
419 return 0;
421 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: libavcodec init OK!\n");
422 ctx->last_aspect=-3;
423 return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
426 // uninit driver
427 static void uninit(sh_video_t *sh){
428 vd_ffmpeg_ctx *ctx = sh->context;
429 AVCodecContext *avctx = ctx->avctx;
431 if(lavc_param_vstats){
432 int i;
433 for(i=1; i<32; i++){
434 printf("QP: %d, count: %d\n", i, ctx->qp_stat[i]);
436 printf("Arithmetic mean of QP: %2.4f, Harmonic mean of QP: %2.4f\n",
437 ctx->qp_sum / avctx->coded_frame->coded_picture_number,
438 1.0/(ctx->inv_qp_sum / avctx->coded_frame->coded_picture_number)
442 if (avcodec_close(avctx) < 0)
443 mp_msg(MSGT_DECVIDEO,MSGL_ERR, MSGTR_CantCloseCodec);
445 av_freep(&avctx->extradata);
446 #if LIBAVCODEC_BUILD >= 4689
447 av_freep(&avctx->palctrl);
448 #endif
449 av_freep(&avctx->slice_offset);
451 av_freep(&avctx);
452 av_freep(&ctx->pic);
453 if (ctx)
454 free(ctx);
457 static void draw_slice(struct AVCodecContext *s,
458 #if LIBAVCODEC_BUILD >= 4670
459 AVFrame *src, int offset[4],
460 #else
461 uint8_t **src, int linesize,
462 #endif
463 int y, int type, int height){
464 sh_video_t * sh = s->opaque;
465 int start=0, i;
466 int width= s->width;
467 int skip_stride= ((width<<lavc_param_lowres)+15)>>4;
468 uint8_t *skip= &s->coded_frame->mbskip_table[(y>>4)*skip_stride];
469 int threshold= s->coded_frame->age;
470 #if LIBAVCODEC_BUILD >= 4670
471 uint8_t *source[3]= {src->data[0] + offset[0], src->data[1] + offset[1], src->data[2] + offset[2]};
472 #else
473 int stride[3];
475 stride[0]=linesize;
476 if(s->coded_frame->linesize[1]){
477 stride[1]= s->coded_frame->linesize[1];
478 stride[2]= s->coded_frame->linesize[2];
479 }else
480 stride[1]=stride[2]=stride[0]/2;
481 #endif
482 #if 0
483 if(s->pict_type!=B_TYPE){
484 for(i=0; i*16<width+16; i++){
485 if(i*16>=width || skip[i]>=threshold){
486 if(start==i) start++;
487 else{
488 uint8_t *src2[3]= {src[0] + start*16,
489 src[1] + start*8,
490 src[2] + start*8};
491 //printf("%2d-%2d x %d\n", start, i, y);
492 mpcodecs_draw_slice (sh,src2, stride, (i-start)*16, height, start*16, y);
493 start= i+1;
497 }else
498 #endif
499 if (y < sh->disp_h) {
500 #if LIBAVCODEC_BUILD >= 4670
501 mpcodecs_draw_slice (sh, source, src->linesize, sh->disp_w, (y+height)<=sh->disp_h?height:sh->disp_h-y, 0, y);
502 #else
503 mpcodecs_draw_slice (sh,src, stride, sh->disp_w, (y+height)<=sh->disp_h?height:sh->disp_h-y, 0, y);
504 #endif
509 static int init_vo(sh_video_t *sh, enum PixelFormat pix_fmt){
510 vd_ffmpeg_ctx *ctx = sh->context;
511 AVCodecContext *avctx = ctx->avctx;
512 #if LIBAVCODEC_BUILD >= 4687
513 float aspect= av_q2d(avctx->sample_aspect_ratio) * avctx->width / avctx->height;
514 #else
515 float aspect= avctx->aspect_ratio;
516 #endif
517 int width, height;
519 width = avctx->width;
520 height = avctx->height;
522 // HACK!
523 // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
524 // use dimensions from BIH to avoid black borders at the right and bottom.
525 if (sh->bih && sh->ImageDesc) {
526 width = sh->bih->biWidth>>lavc_param_lowres;
527 height = sh->bih->biHeight>>lavc_param_lowres;
530 // it is possible another vo buffers to be used after vo config()
531 // lavc reset its buffers on width/heigh change but not on aspect change!!!
532 if (// aspect != ctx->last_aspect ||
533 width != sh->disp_w ||
534 height != sh->disp_h ||
535 !ctx->vo_inited)
537 mp_msg(MSGT_DECVIDEO, MSGL_V, "[ffmpeg] aspect_ratio: %f\n", aspect);
538 ctx->last_aspect = aspect;
539 // if(ctx->last_aspect>=0.01 && ctx->last_aspect<100)
540 if(sh->aspect==0.0)
541 sh->aspect = ctx->last_aspect;
542 sh->disp_w = width;
543 sh->disp_h = height;
544 switch(pix_fmt){
545 // YUVJ are YUV formats that use the full Y range and not just
546 // 16 - 235 (see colorspaces.txt).
547 // Currently they are all treated the same way.
548 case PIX_FMT_YUV410P: ctx->best_csp=IMGFMT_YVU9;break; //svq1
549 case PIX_FMT_YUVJ420P:
550 case PIX_FMT_YUV420P: ctx->best_csp=IMGFMT_YV12;break; //mpegs
551 case PIX_FMT_YUVJ422P:
552 case PIX_FMT_YUV422P: ctx->best_csp=IMGFMT_422P;break; //mjpeg / huffyuv
553 case PIX_FMT_YUVJ444P:
554 case PIX_FMT_YUV444P: ctx->best_csp=IMGFMT_444P;break; //photo jpeg
555 case PIX_FMT_YUV411P: ctx->best_csp=IMGFMT_411P;break; //dv ntsc
556 case PIX_FMT_YUV422: ctx->best_csp=IMGFMT_YUY2;break; //huffyuv perhaps in the future
557 case PIX_FMT_RGB24 : ctx->best_csp=IMGFMT_RGB24;break; //qtrle
558 case PIX_FMT_RGBA32: ctx->best_csp=IMGFMT_BGR32;break; //huffyuv / mjpeg
559 case PIX_FMT_BGR24 : ctx->best_csp=IMGFMT_BGR24;break; //8bps
560 case PIX_FMT_RGB555: ctx->best_csp=IMGFMT_BGR15;break; //rpza,cram
561 case PIX_FMT_RGB565: ctx->best_csp=IMGFMT_BGR16;break; //4xm
562 case PIX_FMT_PAL8: ctx->best_csp=IMGFMT_BGR8;break; //8bps,mrle,cram
563 #ifdef HAVE_XVMC
564 case PIX_FMT_XVMC_MPEG2_MC:ctx->best_csp=IMGFMT_XVMC_MOCO_MPEG2;break;
565 case PIX_FMT_XVMC_MPEG2_IDCT:ctx->best_csp=IMGFMT_XVMC_IDCT_MPEG2;break;
566 #endif
567 default:
568 ctx->best_csp=0;
570 if (!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h, ctx->best_csp))
571 return -1;
572 ctx->vo_inited = 1;
574 return 0;
577 static int get_buffer(AVCodecContext *avctx, AVFrame *pic){
578 sh_video_t * sh = avctx->opaque;
579 vd_ffmpeg_ctx *ctx = sh->context;
580 mp_image_t* mpi=NULL;
581 int flags= MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE;
582 int type= MP_IMGTYPE_IPB;
583 int width= avctx->width;
584 int height= avctx->height;
585 int align=15;
586 //printf("get_buffer %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
587 if(avctx->pix_fmt == PIX_FMT_YUV410P)
588 align=63; //yes seriously, its really needed (16x16 chroma blocks in SVQ1 -> 64x64)
590 #if LIBAVCODEC_BUILD >= 4691
591 if (pic->buffer_hints) {
592 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, "Buffer hints: %u\n", pic->buffer_hints);
593 type = MP_IMGTYPE_TEMP;
594 if (pic->buffer_hints & FF_BUFFER_HINTS_READABLE)
595 flags |= MP_IMGFLAG_READABLE;
596 if (pic->buffer_hints & FF_BUFFER_HINTS_PRESERVE) {
597 type = MP_IMGTYPE_STATIC;
598 flags |= MP_IMGFLAG_PRESERVE;
600 if (pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE) {
601 type = MP_IMGTYPE_STATIC;
602 flags |= MP_IMGFLAG_PRESERVE;
604 flags|=(!avctx->hurry_up && ctx->do_slices) ?
605 MP_IMGFLAG_DRAW_CALLBACK:0;
606 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, type == MP_IMGTYPE_STATIC ? "using STATIC\n" : "using TEMP\n");
607 } else {
608 #endif
609 if(!pic->reference){
610 ctx->b_count++;
611 flags|=(!avctx->hurry_up && ctx->do_slices) ?
612 MP_IMGFLAG_DRAW_CALLBACK:0;
613 }else{
614 ctx->ip_count++;
615 flags|= MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE
616 | (ctx->do_slices ? MP_IMGFLAG_DRAW_CALLBACK : 0);
618 #if LIBAVCODEC_BUILD >= 4691
620 #endif
622 if(init_vo(sh,avctx->pix_fmt) < 0){
623 avctx->release_buffer= avcodec_default_release_buffer;
624 avctx->get_buffer= avcodec_default_get_buffer;
625 return avctx->get_buffer(avctx, pic);
628 #if LIBAVCODEC_BUILD >= 4691
629 if (!pic->buffer_hints) {
630 #endif
631 if(ctx->b_count>1 || ctx->ip_count>2){
632 printf("DR1 failure\n");
634 ctx->do_dr1=0; //FIXME
635 avctx->get_buffer= avcodec_default_get_buffer;
636 return avctx->get_buffer(avctx, pic);
639 if(avctx->has_b_frames){
640 type= MP_IMGTYPE_IPB;
641 }else{
642 type= MP_IMGTYPE_IP;
644 mp_msg(MSGT_DECVIDEO,MSGL_DBG2, type== MP_IMGTYPE_IPB ? "using IPB\n" : "using IP\n");
645 #if LIBAVCODEC_BUILD >= 4691
647 #endif
649 mpi= mpcodecs_get_image(sh,type, flags,
650 (width+align)&(~align), (height+align)&(~align));
652 // ok, let's see what did we get:
653 if( mpi->flags&MP_IMGFLAG_DRAW_CALLBACK &&
654 !(mpi->flags&MP_IMGFLAG_DIRECT)){
655 // nice, filter/vo likes draw_callback :)
656 avctx->draw_horiz_band= draw_slice;
657 } else
658 avctx->draw_horiz_band= NULL;
660 #if LIBAVCODEC_BUILD >= 4689
661 // Palette support: libavcodec copies palette to *data[1]
662 if (mpi->bpp == 8)
663 mpi->planes[1] = av_malloc(AVPALETTE_SIZE);
664 #endif
666 pic->data[0]= mpi->planes[0];
667 pic->data[1]= mpi->planes[1];
668 pic->data[2]= mpi->planes[2];
670 #if 0
671 assert(mpi->width >= ((width +align)&(~align)));
672 assert(mpi->height >= ((height+align)&(~align)));
673 assert(mpi->stride[0] >= mpi->width);
674 if(mpi->imgfmt==IMGFMT_I420 || mpi->imgfmt==IMGFMT_YV12 || mpi->imgfmt==IMGFMT_IYUV){
675 const int y_size= mpi->stride[0] * (mpi->h-1) + mpi->w;
676 const int c_size= mpi->stride[1] * ((mpi->h>>1)-1) + (mpi->w>>1);
678 assert(mpi->planes[0] > mpi->planes[1] || mpi->planes[0] + y_size <= mpi->planes[1]);
679 assert(mpi->planes[0] > mpi->planes[2] || mpi->planes[0] + y_size <= mpi->planes[2]);
680 assert(mpi->planes[1] > mpi->planes[0] || mpi->planes[1] + c_size <= mpi->planes[0]);
681 assert(mpi->planes[1] > mpi->planes[2] || mpi->planes[1] + c_size <= mpi->planes[2]);
682 assert(mpi->planes[2] > mpi->planes[0] || mpi->planes[2] + c_size <= mpi->planes[0]);
683 assert(mpi->planes[2] > mpi->planes[1] || mpi->planes[2] + c_size <= mpi->planes[1]);
685 #endif
687 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
688 * lavc will check that and die with an error message, if its not true
690 pic->linesize[0]= mpi->stride[0];
691 pic->linesize[1]= mpi->stride[1];
692 pic->linesize[2]= mpi->stride[2];
694 pic->opaque = mpi;
695 //printf("%X\n", (int)mpi->planes[0]);
696 #if 0
697 if(mpi->flags&MP_IMGFLAG_DIRECT)
698 printf("D");
699 else if(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK)
700 printf("S");
701 else
702 printf(".");
703 #endif
704 if(pic->reference){
705 pic->age= ctx->ip_age[0];
707 ctx->ip_age[0]= ctx->ip_age[1]+1;
708 ctx->ip_age[1]= 1;
709 ctx->b_age++;
710 }else{
711 pic->age= ctx->b_age;
713 ctx->ip_age[0]++;
714 ctx->ip_age[1]++;
715 ctx->b_age=1;
717 #if LIBAVCODEC_BUILD >= 4644
718 pic->type= FF_BUFFER_TYPE_USER;
719 #endif
720 return 0;
723 static void release_buffer(struct AVCodecContext *avctx, AVFrame *pic){
724 mp_image_t* mpi= pic->opaque;
725 sh_video_t * sh = avctx->opaque;
726 vd_ffmpeg_ctx *ctx = sh->context;
727 int i;
729 //printf("release buffer %d %d %d\n", mpi ? mpi->flags&MP_IMGFLAG_PRESERVE : -99, ctx->ip_count, ctx->b_count);
731 if(ctx->ip_count <= 2 && ctx->b_count<=1){
732 if(mpi->flags&MP_IMGFLAG_PRESERVE)
733 ctx->ip_count--;
734 else
735 ctx->b_count--;
738 // Palette support: free palette buffer allocated in get_buffer
739 if ( mpi && (mpi->bpp == 8))
740 av_freep(&mpi->planes[1]);
742 #if LIBAVCODEC_BUILD >= 4644
743 if(pic->type!=FF_BUFFER_TYPE_USER){
744 avcodec_default_release_buffer(avctx, pic);
745 return;
747 #endif
749 for(i=0; i<4; i++){
750 pic->data[i]= NULL;
752 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
755 // copypaste from demux_real.c - it should match to get it working!
756 //FIXME put into some header
757 typedef struct dp_hdr_s {
758 uint32_t chunks; // number of chunks
759 uint32_t timestamp; // timestamp from packet header
760 uint32_t len; // length of actual data
761 uint32_t chunktab; // offset to chunk offset array
762 } dp_hdr_t;
765 // decode a frame
766 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
767 int got_picture=0;
768 int ret;
769 vd_ffmpeg_ctx *ctx = sh->context;
770 AVFrame *pic= ctx->pic;
771 AVCodecContext *avctx = ctx->avctx;
772 mp_image_t* mpi=NULL;
773 int dr1= ctx->do_dr1;
774 unsigned char *buf = NULL;
776 if(len<=0) return NULL; // skipped frame
778 #if LIBAVCODEC_BUILD < 4707
780 #ifdef HAVE_XVMC
781 if( !avctx->xvmc_acceleration )
782 #endif
784 #else
785 //ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
786 if (!dr1)
787 #endif
788 avctx->draw_horiz_band=NULL;
789 avctx->opaque=sh;
790 if(ctx->vo_inited && !(flags&3) && !dr1){
791 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE |
792 (ctx->do_slices?MP_IMGFLAG_DRAW_CALLBACK:0),
793 sh->disp_w, sh->disp_h);
794 if(mpi && mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
795 // vd core likes slices!
796 avctx->draw_horiz_band=draw_slice;
800 avctx->hurry_up=(flags&3)?((flags&2)?2:1):0;
802 // if(sh->ds->demuxer->type == DEMUXER_TYPE_REAL){
803 if( sh->format == mmioFOURCC('R', 'V', '1', '0')
804 || sh->format == mmioFOURCC('R', 'V', '1', '3')
805 || sh->format == mmioFOURCC('R', 'V', '2', '0')
806 || sh->format == mmioFOURCC('R', 'V', '3', '0')
807 || sh->format == mmioFOURCC('R', 'V', '4', '0'))
808 if(sh->bih->biSize==sizeof(*sh->bih)+8){
809 int i;
810 dp_hdr_t *hdr= (dp_hdr_t*)data;
812 if(avctx->slice_offset==NULL)
813 avctx->slice_offset= av_malloc(sizeof(int)*1000);
815 // for(i=0; i<25; i++) printf("%02X ", ((uint8_t*)data)[i]);
817 avctx->slice_count= hdr->chunks+1;
818 for(i=0; i<avctx->slice_count; i++)
819 avctx->slice_offset[i]= ((uint32_t*)(data+hdr->chunktab))[2*i+1];
820 len=hdr->len;
821 data+= sizeof(dp_hdr_t);
824 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
825 ((int *)data)[0], ((int *)data)[1], ((int *)data)[2], ((int *)data)[3]);
826 ret = avcodec_decode_video(avctx, pic,
827 &got_picture, data, len);
829 dr1= ctx->do_dr1;
830 if(ret<0) mp_msg(MSGT_DECVIDEO,MSGL_WARN, "Error while decoding frame!\n");
831 //printf("repeat: %d\n", pic->repeat_pict);
832 //-- vstats generation
833 #if LIBAVCODEC_BUILD >= 4643
834 while(lavc_param_vstats){ // always one time loop
835 static FILE *fvstats=NULL;
836 char filename[20];
837 static long long int all_len=0;
838 static int frame_number=0;
839 static double all_frametime=0.0;
840 AVFrame *pic= avctx->coded_frame;
841 double quality=0.0;
843 if(!fvstats) {
844 time_t today2;
845 struct tm *today;
846 today2 = time(NULL);
847 today = localtime(&today2);
848 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
849 today->tm_min, today->tm_sec);
850 fvstats = fopen(filename,"w");
851 if(!fvstats) {
852 perror("fopen");
853 lavc_param_vstats=0; // disable block
854 break;
855 /*exit(1);*/
859 // average MB quantizer
861 int x, y;
862 int w = ((avctx->width << lavc_param_lowres)+15) >> 4;
863 int h = ((avctx->height << lavc_param_lowres)+15) >> 4;
864 int8_t *q = pic->qscale_table;
865 for( y = 0; y < h; y++ ) {
866 for( x = 0; x < w; x++ )
867 quality += (double)*(q+x);
868 q += pic->qstride;
870 quality /= w * h;
873 all_len+=len;
874 all_frametime+=sh->frametime;
875 fprintf(fvstats, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
876 ++frame_number, quality, len, (double)all_len/1024);
877 fprintf(fvstats, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
878 all_frametime, (double)(len*8)/sh->frametime/1000.0,
879 (double)(all_len*8)/all_frametime/1000.0);
880 switch(pic->pict_type){
881 case FF_I_TYPE:
882 fprintf(fvstats, "type= I\n");
883 break;
884 case FF_P_TYPE:
885 fprintf(fvstats, "type= P\n");
886 break;
887 case FF_S_TYPE:
888 fprintf(fvstats, "type= S\n");
889 break;
890 case FF_B_TYPE:
891 fprintf(fvstats, "type= B\n");
892 break;
893 default:
894 fprintf(fvstats, "type= ? (%d)\n", pic->pict_type);
895 break;
898 ctx->qp_stat[(int)(quality+0.5)]++;
899 ctx->qp_sum += quality;
900 ctx->inv_qp_sum += 1.0/(double)quality;
902 break;
904 #endif
905 //--
907 if(!got_picture) return NULL; // skipped image
909 if(init_vo(sh,avctx->pix_fmt) < 0) return NULL;
911 if(dr1 && pic->opaque){
912 mpi= (mp_image_t*)pic->opaque;
915 if(!mpi)
916 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, MP_IMGFLAG_PRESERVE,
917 avctx->width, avctx->height);
918 if(!mpi){ // temporary!
919 printf("couldn't allocate image for codec\n");
920 return NULL;
923 if(!dr1){
924 mpi->planes[0]=pic->data[0];
925 mpi->planes[1]=pic->data[1];
926 mpi->planes[2]=pic->data[2];
927 mpi->stride[0]=pic->linesize[0];
928 mpi->stride[1]=pic->linesize[1];
929 mpi->stride[2]=pic->linesize[2];
932 if (!mpi->planes[0])
933 return NULL;
935 if(avctx->pix_fmt==PIX_FMT_YUV422P && mpi->chroma_y_shift==1){
936 // we have 422p but user wants 420p
937 mpi->stride[1]*=2;
938 mpi->stride[2]*=2;
941 /* to comfirm with newer lavc style */
942 mpi->qscale =pic->qscale_table;
943 mpi->qstride=pic->qstride;
944 mpi->pict_type=pic->pict_type;
945 #if LIBAVCODEC_BUILD >= 4664
946 mpi->qscale_type= pic->qscale_type;
947 #endif
948 #if LIBAVCODEC_BUILD >= 4697
949 mpi->fields = MP_IMGFIELD_ORDERED;
950 if(pic->interlaced_frame) mpi->fields |= MP_IMGFIELD_INTERLACED;
951 if(pic->top_field_first ) mpi->fields |= MP_IMGFIELD_TOP_FIRST;
952 if(pic->repeat_pict == 1) mpi->fields |= MP_IMGFIELD_REPEAT_FIRST;
953 #endif
955 return mpi;
958 static enum PixelFormat get_format(struct AVCodecContext * avctx,
959 const enum PixelFormat * fmt){
960 sh_video_t * sh = avctx->opaque;
961 vd_ffmpeg_ctx *ctx = sh->context;
962 int i;
964 #ifdef HAVE_XVMC
965 if(avctx->xvmc_acceleration){
966 avctx->get_buffer= mc_get_buffer;
967 avctx->release_buffer= mc_release_buffer;
968 avctx->draw_horiz_band = mc_render_slice;
969 printf("vd_ffmpeg: XVMC accelerated MPEG2\n");
970 assert(ctx->do_dr1);//these are must to!
971 assert(ctx->do_slices); //it is (vo_)ffmpeg bug if this fails
972 avctx->flags|= CODEC_FLAG_EMU_EDGE;//do i need that??!!
973 avctx->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
975 #endif
976 for(i=0;fmt[i]!=-1;i++){
977 printf("trying pixfmt=%d\n",i);
978 if( init_vo(sh,fmt[i]) >= 0)
979 return fmt[i];
981 return fmt[0];
984 #ifdef HAVE_XVMC
985 static int mc_get_buffer(AVCodecContext *avctx, AVFrame *pic){
986 sh_video_t * sh = avctx->opaque;
987 vd_ffmpeg_ctx *ctx = sh->context;
988 mp_image_t* mpi=NULL;
989 xvmc_render_state_t * render;
990 int flags= MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE|
991 MP_IMGFLAG_DRAW_CALLBACK;
993 // printf("vd_ffmpeg::mc_get_buffer (xvmc) %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
994 if(!avctx->xvmc_acceleration){
995 printf("vd_ffmpeg::mc_get_buffer should work only with XVMC acceleration !!");
996 assert(0);
997 exit(1);
998 // return -1;//!!fixme check error conditions
1000 assert(avctx->draw_horiz_band == mc_render_slice);
1001 assert(avctx->release_buffer == mc_release_buffer);
1002 if(verbose > 4)
1003 printf("vd_ffmpeg::mc_get_buffer\n");
1005 if(init_vo(sh,avctx->pix_fmt) < 0){
1006 printf("vd_ffmpeg: Unexpected init_vo error\n");
1007 exit(1);
1008 // return -1;//!!fixme check error conditions
1013 if(!pic->reference){
1014 ctx->b_count++;
1015 }else{
1016 ctx->ip_count++;
1017 flags|= MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE;
1020 mpi= mpcodecs_get_image(sh, MP_IMGTYPE_IPB,flags ,
1021 avctx->width, avctx->height);
1022 if(mpi==NULL){
1023 printf("Unrecoverable error, render buffers not taken\n");
1024 assert(0);
1025 exit(1);
1026 // return -1;//!!fixme check error conditions in ffmpeg
1029 if( (mpi->flags & MP_IMGFLAG_DIRECT) == 0){
1030 printf("Only buffers allocated by vo_xvmc allowed\n");
1031 assert(0);
1032 exit(1);
1033 // return -1;//!!fixme check error conditions in ffmpeg
1036 pic->data[0]= mpi->planes[0];
1037 pic->data[1]= mpi->planes[1];
1038 pic->data[2]= mpi->planes[2];
1041 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
1042 * lavc will check that and die with an error message, if its not true
1044 pic->linesize[0]= mpi->stride[0];
1045 pic->linesize[1]= mpi->stride[1];
1046 pic->linesize[2]= mpi->stride[2];
1048 pic->opaque = mpi;
1050 if(pic->reference){
1051 //I or P frame
1052 pic->age= ctx->ip_age[0];
1054 ctx->ip_age[0]= ctx->ip_age[1]+1;
1055 ctx->ip_age[1]= 1;
1056 ctx->b_age++;
1057 }else{
1058 //B frame
1059 pic->age= ctx->b_age;
1061 ctx->ip_age[0]++;
1062 ctx->ip_age[1]++;
1063 ctx->b_age=1;
1066 pic->type= FF_BUFFER_TYPE_USER;
1068 render=(xvmc_render_state_t*)mpi->priv;//same as data[2]
1069 if(verbose > 4)
1070 printf("vd_ffmpeg::mc_get_buffer (render=%p)\n",render);
1071 assert(render != 0);
1072 assert(render->magic == MP_XVMC_RENDER_MAGIC);
1073 render->state |= MP_XVMC_STATE_PREDICTION;
1074 return 0;
1078 static void mc_release_buffer(AVCodecContext *avctx, AVFrame *pic){
1079 mp_image_t* mpi= pic->opaque;
1080 sh_video_t * sh = avctx->opaque;
1081 vd_ffmpeg_ctx *ctx = sh->context;
1082 xvmc_render_state_t * render;
1083 int i;
1086 if(ctx->ip_count <= 2 && ctx->b_count<=1){
1087 if(mpi->flags&MP_IMGFLAG_PRESERVE)
1088 ctx->ip_count--;
1089 else
1090 ctx->b_count--;
1093 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
1094 //mark the surface as not requared for prediction
1095 render=(xvmc_render_state_t*)pic->data[2];//same as mpi->priv
1096 if(verbose > 4)
1097 printf("vd_ffmpeg::mc_release_buffer (render=%p)\n",render);
1098 assert(render!=NULL);
1099 assert(render->magic==MP_XVMC_RENDER_MAGIC);
1100 render->state&=~MP_XVMC_STATE_PREDICTION;
1101 for(i=0; i<4; i++){
1102 pic->data[i]= NULL;
1106 static void mc_render_slice(struct AVCodecContext *s,
1107 AVFrame *src, int offset[4],
1108 int y, int type, int height){
1109 int width= s->width;
1110 sh_video_t * sh = s->opaque;
1111 uint8_t *source[3]= {src->data[0], src->data[1], src->data[2]};
1113 assert(src->linesize[0]==0 && src->linesize[1]==0 && src->linesize[2]==0);
1114 assert(offset[0]==0 && offset[1]==0 && offset[2]==0);
1116 mpcodecs_draw_slice (sh, source, src->linesize, width, height, 0, y);
1120 #endif // HAVE_XVMC
1122 #endif