14 #include "vd_internal.h"
16 static vd_info_t info
= {
17 "FFmpeg's libavcodec codec family",
20 "A'rpi, Michael, Alex",
21 "native codecs (http://ffmpeg.sf.net/)"
26 #ifdef USE_LIBAVCODEC_SO
27 #include <ffmpeg/avcodec.h>
29 #include "libavcodec/avcodec.h"
32 #if LIBAVCODEC_BUILD < 4641
33 #error we do not support libavcodec prior to build 4641, get the latest libavcodec CVS
36 #if LIBAVCODEC_BUILD < 4645
37 #warning your version of libavcodec is old, u might want to get a newer one
40 #if LIBAVCODEC_BUILD < 4645
41 #define AVFrame AVVideoFrame
42 #define coded_frame coded_picture
45 #if LIBAVCODEC_BUILD < 4654
46 #define PIX_FMT_RGB24 PIX_FMT_BGR24
47 #define PIX_FMT_RGBA32 PIX_FMT_BGRA32
50 #if LIBAVCODEC_BUILD < 4672
55 #include "xvmc_render.h"
61 AVCodecContext
*avctx
;
77 //#ifdef FF_POSTPROCESS
78 //unsigned int lavc_pp=0;
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
);
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
);
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
;
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
},
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
},
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
) {
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
;
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
;
166 case VDCTRL_QUERY_FORMAT
:
168 int format
=(*((int*)arg
));
169 if( format
== ctx
->best_csp
) return CONTROL_TRUE
;//supported
170 // possible conversions:
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
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
;
185 return CONTROL_FALSE
;
188 case VDCTRL_RESYNC_STREAM
:
189 avcodec_flush_buffers(avctx
);
192 return CONTROL_UNKNOWN
;
196 static int init(sh_video_t
*sh
){
197 AVCodecContext
*avctx
;
200 #if LIBAVCODEC_BUILD >= 4722
203 int do_vis_debug
= lavc_param_vismv
|| (lavc_param_debug
&(FF_DEBUG_VIS_MB_TYPE
|FF_DEBUG_VIS_QP
));
207 avcodec_register_all();
211 ctx
= sh
->context
= malloc(sizeof(vd_ffmpeg_ctx
));
214 memset(ctx
, 0, sizeof(vd_ffmpeg_ctx
));
216 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh
->codec
->dll
);
218 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh
->codec
->dll
);
222 if(vd_use_slices
&& (lavc_codec
->capabilities
&CODEC_CAP_DRAW_HORIZ_BAND
) && !do_vis_debug
)
225 if(lavc_codec
->capabilities
&CODEC_CAP_DR1
&& !do_vis_debug
&& lavc_codec
->id
!= CODEC_ID_H264
)
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();
233 ctx
->pic
= avcodec_alloc_picture();
235 ctx
->avctx
= avcodec_alloc_context();
238 #if LIBAVCODEC_BUILD >= 4691 && LIBAVCODEC_BUILD <= 4692
239 if(lavc_codec
->capabilities
&CODEC_CAP_CR
)
240 avctx
->cr_available
= 1;
245 #ifdef CODEC_CAP_HWACCEL
246 if(lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
){
248 if(lavc_codec
->id
== CODEC_ID_MPEG2VIDEO_XVMC
){
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
;
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
;
270 #ifdef CODEC_FLAG_NOT_TRUNCATED
271 avctx
->flags
|= CODEC_FLAG_NOT_TRUNCATED
;
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
;
282 avctx
->codec_tag
= sh
->format
;
283 #if LIBAVCODEC_BUILD >= 4679
284 avctx
->stream_codec_tag
= sh
->video
.fccHandler
;
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
);
293 #if LIBAVCODEC_BUILD >= 4698
294 avctx
->debug_mv
= lavc_param_vismv
;
296 #if LIBAVCODEC_BUILD >= 4717
297 avctx
->skip_top
= lavc_param_skip_top
;
298 avctx
->skip_bottom
= lavc_param_skip_bottom
;
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
;
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
);
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
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
);
331 uint8_t *p
= avctx
->extradata
;
333 for (x
=0; x
<avctx
->extradata_size
; x
++)
334 printf("[%x] ", p
[x
]);
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;
351 ((uint32_t*)avctx
->extradata
)[1] =
352 (sh
->format
== mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
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
));
397 /* Palette size in biClrUsed */
398 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
399 min(sh
->bih
->biClrUsed
* 4, AVPALETTE_SIZE
));
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
);
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
);
417 if (avcodec_open(avctx
, lavc_codec
) < 0) {
418 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
, MSGTR_CantOpenCodec
);
421 mp_msg(MSGT_DECVIDEO
,MSGL_V
,"INFO: libavcodec init OK!\n");
423 return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
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
){
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
);
449 av_freep(&avctx
->slice_offset
);
457 static void draw_slice(struct AVCodecContext
*s
,
458 #if LIBAVCODEC_BUILD >= 4670
459 AVFrame
*src
, int offset
[4],
461 uint8_t **src
, int linesize
,
463 int y
, int type
, int height
){
464 sh_video_t
* sh
= s
->opaque
;
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]};
476 if(s
->coded_frame
->linesize
[1]){
477 stride
[1]= s
->coded_frame
->linesize
[1];
478 stride
[2]= s
->coded_frame
->linesize
[2];
480 stride
[1]=stride
[2]=stride
[0]/2;
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
++;
488 uint8_t *src2
[3]= {src
[0] + start
*16,
491 //printf("%2d-%2d x %d\n", start, i, y);
492 mpcodecs_draw_slice (sh
,src2
, stride
, (i
-start
)*16, height
, start
*16, y
);
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
);
503 mpcodecs_draw_slice (sh
,src
, stride
, sh
->disp_w
, (y
+height
)<=sh
->disp_h
?height
:sh
->disp_h
-y
, 0, y
);
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
;
515 float aspect
= avctx
->aspect_ratio
;
519 width
= avctx
->width
;
520 height
= avctx
->height
;
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
||
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)
541 sh
->aspect
= ctx
->last_aspect
;
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
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;
570 if (!mpcodecs_config_vo(sh
,sh
->disp_w
,sh
->disp_h
, ctx
->best_csp
))
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
;
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");
611 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
612 MP_IMGFLAG_DRAW_CALLBACK
:0;
615 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
616 | (ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0);
618 #if LIBAVCODEC_BUILD >= 4691
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
) {
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
;
644 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, type
== MP_IMGTYPE_IPB
? "using IPB\n" : "using IP\n");
645 #if LIBAVCODEC_BUILD >= 4691
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
;
658 avctx
->draw_horiz_band
= NULL
;
660 #if LIBAVCODEC_BUILD >= 4689
661 // Palette support: libavcodec copies palette to *data[1]
663 mpi
->planes
[1] = av_malloc(AVPALETTE_SIZE
);
666 pic
->data
[0]= mpi
->planes
[0];
667 pic
->data
[1]= mpi
->planes
[1];
668 pic
->data
[2]= mpi
->planes
[2];
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]);
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];
695 //printf("%X\n", (int)mpi->planes[0]);
697 if(mpi
->flags
&MP_IMGFLAG_DIRECT
)
699 else if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)
705 pic
->age
= ctx
->ip_age
[0];
707 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
711 pic
->age
= ctx
->b_age
;
717 #if LIBAVCODEC_BUILD >= 4644
718 pic
->type
= FF_BUFFER_TYPE_USER
;
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
;
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
)
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
);
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
766 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
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
781 if( !avctx
->xvmc_acceleration
)
785 //ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
788 avctx
->draw_horiz_band
=NULL
;
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){
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];
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
);
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
;
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
;
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");
853 lavc_param_vstats
=0; // disable block
859 // average MB quantizer
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
);
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
){
882 fprintf(fvstats
, "type= I\n");
885 fprintf(fvstats
, "type= P\n");
888 fprintf(fvstats
, "type= S\n");
891 fprintf(fvstats
, "type= B\n");
894 fprintf(fvstats
, "type= ? (%d)\n", pic
->pict_type
);
898 ctx
->qp_stat
[(int)(quality
+0.5)]++;
899 ctx
->qp_sum
+= quality
;
900 ctx
->inv_qp_sum
+= 1.0/(double)quality
;
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
;
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");
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];
935 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& mpi
->chroma_y_shift
==1){
936 // we have 422p but user wants 420p
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
;
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
;
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
;
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
;
976 for(i
=0;fmt
[i
]!=-1;i
++){
977 printf("trying pixfmt=%d\n",i
);
978 if( init_vo(sh
,fmt
[i
]) >= 0)
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 !!");
998 // return -1;//!!fixme check error conditions
1000 assert(avctx
->draw_horiz_band
== mc_render_slice
);
1001 assert(avctx
->release_buffer
== mc_release_buffer
);
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");
1008 // return -1;//!!fixme check error conditions
1013 if(!pic
->reference
){
1017 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
;
1020 mpi
= mpcodecs_get_image(sh
, MP_IMGTYPE_IPB
,flags
,
1021 avctx
->width
, avctx
->height
);
1023 printf("Unrecoverable error, render buffers not taken\n");
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");
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];
1052 pic
->age
= ctx
->ip_age
[0];
1054 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
1059 pic
->age
= ctx
->b_age
;
1066 pic
->type
= FF_BUFFER_TYPE_USER
;
1068 render
=(xvmc_render_state_t
*)mpi
->priv
;//same as data[2]
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
;
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
;
1086 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
1087 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
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
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
;
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
);