10 #include "libavutil/common.h"
11 #include "libavutil/intreadwrite.h"
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"
33 #include "xvmc_render.h"
39 AVCodecContext
*avctx
;
41 enum PixelFormat pix_fmt
;
53 AVRational last_sample_aspect_ratio
;
56 //#ifdef USE_LIBPOSTPROC
57 //unsigned int lavc_pp=0;
62 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
63 static void release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
66 static enum PixelFormat
get_format(struct AVCodecContext
* avctx
,
67 const enum PixelFormat
* pix_fmt
);
68 static int mc_get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
69 static void mc_release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
70 static void mc_render_slice(struct AVCodecContext
*s
,
71 AVFrame
*src
, int offset
[4],
72 int y
, int type
, int height
);
75 static int lavc_param_workaround_bugs
= FF_BUG_AUTODETECT
;
76 static int lavc_param_error_resilience
=2;
77 static int lavc_param_error_concealment
=3;
78 static int lavc_param_gray
=0;
79 static int lavc_param_vstats
=0;
80 static int lavc_param_idct_algo
=0;
81 static int lavc_param_debug
=0;
82 static int lavc_param_vismv
=0;
83 static int lavc_param_skip_top
=0;
84 static int lavc_param_skip_bottom
=0;
85 static int lavc_param_fast
=0;
86 static int lavc_param_lowres
=0;
87 static char *lavc_param_lowres_str
=NULL
;
88 static char *lavc_param_skip_loop_filter_str
= NULL
;
89 static char *lavc_param_skip_idct_str
= NULL
;
90 static char *lavc_param_skip_frame_str
= NULL
;
91 static int lavc_param_threads
=1;
92 static int lavc_param_bitexact
=0;
94 const m_option_t lavc_decode_opts_conf
[]={
95 {"bug", &lavc_param_workaround_bugs
, CONF_TYPE_INT
, CONF_RANGE
, -1, 999999, NULL
},
96 {"er", &lavc_param_error_resilience
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
97 {"gray", &lavc_param_gray
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_PART
, NULL
},
98 {"idct", &lavc_param_idct_algo
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
99 {"ec", &lavc_param_error_concealment
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
100 {"vstats", &lavc_param_vstats
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
101 {"debug", &lavc_param_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9999999, NULL
},
102 {"vismv", &lavc_param_vismv
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9999999, NULL
},
103 {"st", &lavc_param_skip_top
, CONF_TYPE_INT
, CONF_RANGE
, 0, 999, NULL
},
104 {"sb", &lavc_param_skip_bottom
, CONF_TYPE_INT
, CONF_RANGE
, 0, 999, NULL
},
105 #ifdef CODEC_FLAG2_FAST
106 {"fast", &lavc_param_fast
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG2_FAST
, NULL
},
108 {"lowres", &lavc_param_lowres_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
109 {"skiploopfilter", &lavc_param_skip_loop_filter_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
110 {"skipidct", &lavc_param_skip_idct_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
111 {"skipframe", &lavc_param_skip_frame_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
112 {"threads", &lavc_param_threads
, CONF_TYPE_INT
, CONF_RANGE
, 1, 8, NULL
},
113 {"bitexact", &lavc_param_bitexact
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_BITEXACT
, NULL
},
114 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
117 static enum AVDiscard
str2AVDiscard(char *str
) {
119 return AVDISCARD_DEFAULT
;
120 if (strcasecmp(str
, "none") == 0)
121 return AVDISCARD_NONE
;
122 if (strcasecmp(str
, "default") == 0)
123 return AVDISCARD_DEFAULT
;
124 if (strcasecmp(str
, "nonref") == 0)
125 return AVDISCARD_NONREF
;
126 if (strcasecmp(str
, "bidir") == 0)
127 return AVDISCARD_BIDIR
;
128 if (strcasecmp(str
, "nonkey") == 0)
129 return AVDISCARD_NONKEY
;
130 if (strcasecmp(str
, "all") == 0)
131 return AVDISCARD_ALL
;
132 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Unknown discard value %s\n", str
);
133 return AVDISCARD_DEFAULT
;
136 // to set/get/query special features/parameters
137 static int control(sh_video_t
*sh
,int cmd
,void* arg
,...){
138 vd_ffmpeg_ctx
*ctx
= sh
->context
;
139 AVCodecContext
*avctx
= ctx
->avctx
;
141 case VDCTRL_QUERY_FORMAT
:
143 int format
=(*((int*)arg
));
144 if( format
== ctx
->best_csp
) return CONTROL_TRUE
;//supported
145 // possible conversions:
150 // "converted" using pointer/stride modification
151 if(avctx
->pix_fmt
==PIX_FMT_YUV420P
) return CONTROL_TRUE
;// u/v swap
152 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& !ctx
->do_dr1
) return CONTROL_TRUE
;// half stride
155 case IMGFMT_XVMC_IDCT_MPEG2
:
156 case IMGFMT_XVMC_MOCO_MPEG2
:
157 if(avctx
->pix_fmt
==PIX_FMT_XVMC_MPEG2_IDCT
) return CONTROL_TRUE
;
160 return CONTROL_FALSE
;
163 case VDCTRL_RESYNC_STREAM
:
164 avcodec_flush_buffers(avctx
);
166 case VDCTRL_QUERY_UNSEEN_FRAMES
:
167 return avctx
->has_b_frames
+ 10;
169 return CONTROL_UNKNOWN
;
172 void mp_msp_av_log_callback(void* ptr
, int level
, const char* fmt
, va_list vl
)
174 static int print_prefix
=1;
175 AVClass
* avc
= ptr
? *(AVClass
**)ptr
: NULL
;
176 int type
= MSGT_FIXME
;
181 case AV_LOG_DEBUG
: mp_level
= MSGL_V
; break;
182 case AV_LOG_INFO
: mp_level
= MSGL_INFO
; break;
183 case AV_LOG_ERROR
: mp_level
= MSGL_ERR
; break;
184 default : mp_level
= MSGL_ERR
; break;
187 if (!mp_msg_test(type
, mp_level
)) return;
190 if(!strcmp(avc
->class_name
, "AVCodecContext")){
191 AVCodecContext
* s
= ptr
;
193 if(s
->codec
->type
== CODEC_TYPE_AUDIO
){
196 }else if(s
->codec
->type
== CODEC_TYPE_VIDEO
){
200 //FIXME subtitles, encoders (what msgt for them? there is no appropriate ...)
202 }else if(!strcmp(avc
->class_name
, "AVFormatContext")){
203 #if 0 //needs libavformat include FIXME iam too lazy to do this cleanly,probably the whole should be moved out of this file ...
204 AVFormatContext
* s
= ptr
;
213 if(print_prefix
&& avc
) {
214 mp_msg(type
, mp_level
, "[%s @ %p]", avc
->item_name(ptr
), avc
);
217 print_prefix
= strchr(fmt
, '\n') != NULL
;
218 vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
219 mp_msg(type
, mp_level
, buf
);
223 static int init(sh_video_t
*sh
){
224 AVCodecContext
*avctx
;
228 int do_vis_debug
= lavc_param_vismv
|| (lavc_param_debug
&(FF_DEBUG_VIS_MB_TYPE
|FF_DEBUG_VIS_QP
));
232 avcodec_register_all();
234 av_log_set_callback(mp_msp_av_log_callback
);
237 ctx
= sh
->context
= malloc(sizeof(vd_ffmpeg_ctx
));
240 memset(ctx
, 0, sizeof(vd_ffmpeg_ctx
));
242 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh
->codec
->dll
);
244 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh
->codec
->dll
);
249 if(vd_use_slices
&& (lavc_codec
->capabilities
&CODEC_CAP_DRAW_HORIZ_BAND
) && !do_vis_debug
)
252 if(lavc_codec
->capabilities
&CODEC_CAP_DR1
&& !do_vis_debug
&& lavc_codec
->id
!= CODEC_ID_H264
&& lavc_codec
->id
!= CODEC_ID_INTERPLAY_VIDEO
&& lavc_codec
->id
!= CODEC_ID_ROQ
)
254 ctx
->b_age
= ctx
->ip_age
[0]= ctx
->ip_age
[1]= 256*256*256*64;
255 ctx
->ip_count
= ctx
->b_count
= 0;
257 ctx
->pic
= avcodec_alloc_frame();
258 ctx
->avctx
= avcodec_alloc_context();
263 #ifdef CODEC_CAP_HWACCEL
264 if(lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
){
266 if(lavc_codec
->id
== CODEC_ID_MPEG2VIDEO_XVMC
){
267 #endif /* CODEC_CAP_HWACCEL */
268 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_XVMCAcceleratedCodec
);
269 assert(ctx
->do_dr1
);//these are must to!
270 assert(ctx
->do_slices
); //it is (vo_)ffmpeg bug if this fails
271 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;//do i need that??!!
272 avctx
->get_format
= get_format
;//for now only this decoder will use it
273 avctx
->get_buffer
= mc_get_buffer
;
274 avctx
->release_buffer
= mc_release_buffer
;
275 avctx
->draw_horiz_band
= mc_render_slice
;
276 avctx
->slice_flags
=SLICE_FLAG_CODED_ORDER
|SLICE_FLAG_ALLOW_FIELD
;
278 #endif /* HAVE_XVMC */
280 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
281 avctx
->get_buffer
= get_buffer
;
282 avctx
->release_buffer
= release_buffer
;
283 avctx
->reget_buffer
= get_buffer
;
286 #ifdef CODEC_FLAG_NOT_TRUNCATED
287 avctx
->flags
|= CODEC_FLAG_NOT_TRUNCATED
;
289 avctx
->flags
|= lavc_param_bitexact
;
291 avctx
->width
= sh
->disp_w
;
292 avctx
->height
= sh
->disp_h
;
293 avctx
->workaround_bugs
= lavc_param_workaround_bugs
;
294 avctx
->error_resilience
= lavc_param_error_resilience
;
295 if(lavc_param_gray
) avctx
->flags
|= CODEC_FLAG_GRAY
;
296 #ifdef CODEC_FLAG2_FAST
297 avctx
->flags2
|= lavc_param_fast
;
299 avctx
->codec_tag
= sh
->format
;
300 avctx
->stream_codec_tag
= sh
->video
.fccHandler
;
301 avctx
->idct_algo
= lavc_param_idct_algo
;
302 avctx
->error_concealment
= lavc_param_error_concealment
;
303 avctx
->debug
= lavc_param_debug
;
304 if (lavc_param_debug
)
305 av_log_set_level(AV_LOG_DEBUG
);
306 avctx
->debug_mv
= lavc_param_vismv
;
307 avctx
->skip_top
= lavc_param_skip_top
;
308 avctx
->skip_bottom
= lavc_param_skip_bottom
;
309 if(lavc_param_lowres_str
!= NULL
)
311 sscanf(lavc_param_lowres_str
, "%d,%d", &lavc_param_lowres
, &lowres_w
);
312 if(lavc_param_lowres
< 1 || lavc_param_lowres
> 16 || (lowres_w
> 0 && avctx
->width
< lowres_w
))
313 lavc_param_lowres
= 0;
314 avctx
->lowres
= lavc_param_lowres
;
316 avctx
->skip_loop_filter
= str2AVDiscard(lavc_param_skip_loop_filter_str
);
317 avctx
->skip_idct
= str2AVDiscard(lavc_param_skip_idct_str
);
318 avctx
->skip_frame
= str2AVDiscard(lavc_param_skip_frame_str
);
319 mp_dbg(MSGT_DECVIDEO
,MSGL_DBG2
,"libavcodec.size: %d x %d\n",avctx
->width
,avctx
->height
);
320 switch (sh
->format
) {
321 case mmioFOURCC('S','V','Q','3'):
322 /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or
323 in the phony AVI header if demux_lavf is used. The first case is
324 handled here; the second case falls through to the next section. */
326 avctx
->extradata_size
= (*(int*)sh
->ImageDesc
) - sizeof(int);
327 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
328 memcpy(avctx
->extradata
, ((int*)sh
->ImageDesc
)+1, avctx
->extradata_size
);
333 case mmioFOURCC('A','V','R','n'):
334 case mmioFOURCC('M','J','P','G'):
335 /* AVRn stores huffman table in AVI header */
336 /* Pegasus MJPEG stores it also in AVI header, but it uses the common
338 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
340 avctx
->flags
|= CODEC_FLAG_EXTERN_HUFF
;
341 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
342 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
343 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
348 uint8_t *p
= avctx
->extradata
;
350 for (x
=0; x
<avctx
->extradata_size
; x
++)
351 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,"[%x] ", p
[x
]);
352 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,"\n");
357 case mmioFOURCC('R', 'V', '1', '0'):
358 case mmioFOURCC('R', 'V', '1', '3'):
359 case mmioFOURCC('R', 'V', '2', '0'):
360 case mmioFOURCC('R', 'V', '3', '0'):
361 case mmioFOURCC('R', 'V', '4', '0'):
362 if(sh
->bih
->biSize
<sizeof(*sh
->bih
)+8){
363 /* only 1 packet per frame & sub_id from fourcc */
364 avctx
->extradata_size
= 8;
365 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
366 ((uint32_t*)avctx
->extradata
)[0] = 0;
367 ((uint32_t*)avctx
->extradata
)[1] =
368 (sh
->format
== mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
370 /* has extra slice header (demux_rm or rm->avi streamcopy) */
371 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
372 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
373 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
375 avctx
->sub_id
= AV_RB32(avctx
->extradata
+4);
377 // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]);
381 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
383 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
384 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
385 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
388 /* Pass palette to codec */
389 if (sh
->bih
&& (sh
->bih
->biBitCount
<= 8)) {
390 avctx
->palctrl
= 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 FFMIN(sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
), AVPALETTE_SIZE
));
397 /* Palette size in biClrUsed */
398 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
399 FFMIN(sh
->bih
->biClrUsed
* 4, AVPALETTE_SIZE
));
403 avctx
->bits_per_sample
= sh
->bih
->biBitCount
;
405 if(lavc_param_threads
> 1)
406 avcodec_thread_init(avctx
, lavc_param_threads
);
408 if (avcodec_open(avctx
, lavc_codec
) < 0) {
409 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
, MSGTR_CantOpenCodec
);
413 mp_msg(MSGT_DECVIDEO
,MSGL_V
,"INFO: libavcodec init OK!\n");
414 return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
418 static void uninit(sh_video_t
*sh
){
419 vd_ffmpeg_ctx
*ctx
= sh
->context
;
420 AVCodecContext
*avctx
= ctx
->avctx
;
422 if(lavc_param_vstats
){
425 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,"QP: %d, count: %d\n", i
, ctx
->qp_stat
[i
]);
427 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,MSGTR_MPCODECS_ArithmeticMeanOfQP
,
428 ctx
->qp_sum
/ avctx
->coded_frame
->coded_picture_number
,
429 1.0/(ctx
->inv_qp_sum
/ avctx
->coded_frame
->coded_picture_number
)
434 if (avctx
->codec
&& avcodec_close(avctx
) < 0)
435 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
, MSGTR_CantCloseCodec
);
437 av_freep(&avctx
->extradata
);
438 av_freep(&avctx
->palctrl
);
439 av_freep(&avctx
->slice_offset
);
448 static void draw_slice(struct AVCodecContext
*s
,
449 AVFrame
*src
, int offset
[4],
450 int y
, int type
, int height
){
451 sh_video_t
* sh
= s
->opaque
;
452 uint8_t *source
[3]= {src
->data
[0] + offset
[0], src
->data
[1] + offset
[1], src
->data
[2] + offset
[2]};
456 int skip_stride
= ((width
<<lavc_param_lowres
)+15)>>4;
457 uint8_t *skip
= &s
->coded_frame
->mbskip_table
[(y
>>4)*skip_stride
];
458 int threshold
= s
->coded_frame
->age
;
459 if(s
->pict_type
!=B_TYPE
){
460 for(i
=0; i
*16<width
+16; i
++){
461 if(i
*16>=width
|| skip
[i
]>=threshold
){
462 if(start
==i
) start
++;
464 uint8_t *src2
[3]= {src
[0] + start
*16,
467 //printf("%2d-%2d x %d\n", start, i, y);
468 mpcodecs_draw_slice (sh
,src2
, stride
, (i
-start
)*16, height
, start
*16, y
);
475 if (y
< sh
->disp_h
) {
476 mpcodecs_draw_slice (sh
, source
, src
->linesize
, sh
->disp_w
, (y
+height
)<=sh
->disp_h
?height
:sh
->disp_h
-y
, 0, y
);
481 static int init_vo(sh_video_t
*sh
, enum PixelFormat pix_fmt
){
482 vd_ffmpeg_ctx
*ctx
= sh
->context
;
483 AVCodecContext
*avctx
= ctx
->avctx
;
484 float aspect
= av_q2d(avctx
->sample_aspect_ratio
) * avctx
->width
/ avctx
->height
;
487 width
= avctx
->width
;
488 height
= avctx
->height
;
491 // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
492 // use dimensions from BIH to avoid black borders at the right and bottom.
493 if (sh
->bih
&& sh
->ImageDesc
) {
494 width
= sh
->bih
->biWidth
>>lavc_param_lowres
;
495 height
= sh
->bih
->biHeight
>>lavc_param_lowres
;
498 // it is possible another vo buffers to be used after vo config()
499 // lavc reset its buffers on width/heigh change but not on aspect change!!!
500 if (av_cmp_q(avctx
->sample_aspect_ratio
, ctx
->last_sample_aspect_ratio
) ||
501 width
!= sh
->disp_w
||
502 height
!= sh
->disp_h
||
503 pix_fmt
!= ctx
->pix_fmt
||
506 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[ffmpeg] aspect_ratio: %f\n", aspect
);
507 if (sh
->aspect
== 0 ||
508 av_cmp_q(avctx
->sample_aspect_ratio
,
509 ctx
->last_sample_aspect_ratio
))
511 ctx
->last_sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
514 ctx
->pix_fmt
= pix_fmt
;
516 // YUVJ are YUV formats that use the full Y range and not just
517 // 16 - 235 (see colorspaces.txt).
518 // Currently they are all treated the same way.
519 case PIX_FMT_YUV410P
: ctx
->best_csp
=IMGFMT_YVU9
;break; //svq1
520 case PIX_FMT_YUVJ420P
:
521 case PIX_FMT_YUV420P
: ctx
->best_csp
=IMGFMT_YV12
;break; //mpegs
522 case PIX_FMT_YUVJ422P
:
523 case PIX_FMT_YUV422P
: ctx
->best_csp
=IMGFMT_422P
;break; //mjpeg / huffyuv
524 case PIX_FMT_YUVJ444P
:
525 case PIX_FMT_YUV444P
: ctx
->best_csp
=IMGFMT_444P
;break; //photo jpeg
526 case PIX_FMT_YUV411P
: ctx
->best_csp
=IMGFMT_411P
;break; //dv ntsc
527 case PIX_FMT_YUYV422
: ctx
->best_csp
=IMGFMT_YUY2
;break; //huffyuv perhaps in the future
528 case PIX_FMT_RGB24
: ctx
->best_csp
=IMGFMT_RGB24
;break; //qtrle
529 case PIX_FMT_RGB32
: ctx
->best_csp
=IMGFMT_BGR32
;break; //huffyuv / mjpeg
530 case PIX_FMT_BGR24
: ctx
->best_csp
=IMGFMT_BGR24
;break; //8bps
531 case PIX_FMT_RGB555
: ctx
->best_csp
=IMGFMT_BGR15
;break; //rpza,cram
532 case PIX_FMT_RGB565
: ctx
->best_csp
=IMGFMT_BGR16
;break; //4xm
533 case PIX_FMT_GRAY8
: ctx
->best_csp
=IMGFMT_Y800
;break; // gray jpeg
534 case PIX_FMT_PAL8
: ctx
->best_csp
=IMGFMT_BGR8
;break; //8bps,mrle,cram
536 case PIX_FMT_XVMC_MPEG2_MC
:ctx
->best_csp
=IMGFMT_XVMC_MOCO_MPEG2
;break;
537 case PIX_FMT_XVMC_MPEG2_IDCT
:ctx
->best_csp
=IMGFMT_XVMC_IDCT_MPEG2
;break;
542 if (!mpcodecs_config_vo(sh
,sh
->disp_w
,sh
->disp_h
, ctx
->best_csp
))
549 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
550 sh_video_t
* sh
= avctx
->opaque
;
551 vd_ffmpeg_ctx
*ctx
= sh
->context
;
552 mp_image_t
* mpi
=NULL
;
553 int flags
= MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
;
554 int type
= MP_IMGTYPE_IPB
;
555 int width
= avctx
->width
;
556 int height
= avctx
->height
;
558 //printf("get_buffer %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
559 if(avctx
->pix_fmt
== PIX_FMT_YUV410P
)
560 align
=63; //yes seriously, its really needed (16x16 chroma blocks in SVQ1 -> 64x64)
562 if (pic
->buffer_hints
) {
563 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, "Buffer hints: %u\n", pic
->buffer_hints
);
564 type
= MP_IMGTYPE_TEMP
;
565 if (pic
->buffer_hints
& FF_BUFFER_HINTS_READABLE
)
566 flags
|= MP_IMGFLAG_READABLE
;
567 if (pic
->buffer_hints
& FF_BUFFER_HINTS_PRESERVE
) {
568 type
= MP_IMGTYPE_STATIC
;
569 flags
|= MP_IMGFLAG_PRESERVE
;
571 if (pic
->buffer_hints
& FF_BUFFER_HINTS_REUSABLE
) {
572 type
= MP_IMGTYPE_STATIC
;
573 flags
|= MP_IMGFLAG_PRESERVE
;
575 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
576 MP_IMGFLAG_DRAW_CALLBACK
:0;
577 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, type
== MP_IMGTYPE_STATIC
? "using STATIC\n" : "using TEMP\n");
581 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
582 MP_IMGFLAG_DRAW_CALLBACK
:0;
585 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
586 | (ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0);
590 if(init_vo(sh
,avctx
->pix_fmt
) < 0){
591 avctx
->release_buffer
= avcodec_default_release_buffer
;
592 avctx
->get_buffer
= avcodec_default_get_buffer
;
593 return avctx
->get_buffer(avctx
, pic
);
596 if (!pic
->buffer_hints
) {
597 if(ctx
->b_count
>1 || ctx
->ip_count
>2){
598 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_DRIFailure
);
600 ctx
->do_dr1
=0; //FIXME
601 avctx
->get_buffer
= avcodec_default_get_buffer
;
602 return avctx
->get_buffer(avctx
, pic
);
605 if(avctx
->has_b_frames
){
606 type
= MP_IMGTYPE_IPB
;
610 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, type
== MP_IMGTYPE_IPB
? "using IPB\n" : "using IP\n");
613 mpi
= mpcodecs_get_image(sh
,type
, flags
,
614 (width
+align
)&(~align
), (height
+align
)&(~align
));
616 // ok, let's see what did we get:
617 if( mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&&
618 !(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
619 // nice, filter/vo likes draw_callback :)
620 avctx
->draw_horiz_band
= draw_slice
;
622 avctx
->draw_horiz_band
= NULL
;
624 // Palette support: libavcodec copies palette to *data[1]
626 mpi
->planes
[1] = av_malloc(AVPALETTE_SIZE
);
628 pic
->data
[0]= mpi
->planes
[0];
629 pic
->data
[1]= mpi
->planes
[1];
630 pic
->data
[2]= mpi
->planes
[2];
633 assert(mpi
->width
>= ((width
+align
)&(~align
)));
634 assert(mpi
->height
>= ((height
+align
)&(~align
)));
635 assert(mpi
->stride
[0] >= mpi
->width
);
636 if(mpi
->imgfmt
==IMGFMT_I420
|| mpi
->imgfmt
==IMGFMT_YV12
|| mpi
->imgfmt
==IMGFMT_IYUV
){
637 const int y_size
= mpi
->stride
[0] * (mpi
->h
-1) + mpi
->w
;
638 const int c_size
= mpi
->stride
[1] * ((mpi
->h
>>1)-1) + (mpi
->w
>>1);
640 assert(mpi
->planes
[0] > mpi
->planes
[1] || mpi
->planes
[0] + y_size
<= mpi
->planes
[1]);
641 assert(mpi
->planes
[0] > mpi
->planes
[2] || mpi
->planes
[0] + y_size
<= mpi
->planes
[2]);
642 assert(mpi
->planes
[1] > mpi
->planes
[0] || mpi
->planes
[1] + c_size
<= mpi
->planes
[0]);
643 assert(mpi
->planes
[1] > mpi
->planes
[2] || mpi
->planes
[1] + c_size
<= mpi
->planes
[2]);
644 assert(mpi
->planes
[2] > mpi
->planes
[0] || mpi
->planes
[2] + c_size
<= mpi
->planes
[0]);
645 assert(mpi
->planes
[2] > mpi
->planes
[1] || mpi
->planes
[2] + c_size
<= mpi
->planes
[1]);
649 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
650 * lavc will check that and die with an error message, if its not true
652 pic
->linesize
[0]= mpi
->stride
[0];
653 pic
->linesize
[1]= mpi
->stride
[1];
654 pic
->linesize
[2]= mpi
->stride
[2];
657 //printf("%X\n", (int)mpi->planes[0]);
659 if(mpi
->flags
&MP_IMGFLAG_DIRECT
)
661 else if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)
667 pic
->age
= ctx
->ip_age
[0];
669 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
673 pic
->age
= ctx
->b_age
;
679 pic
->type
= FF_BUFFER_TYPE_USER
;
683 static void release_buffer(struct AVCodecContext
*avctx
, AVFrame
*pic
){
684 mp_image_t
* mpi
= pic
->opaque
;
685 sh_video_t
* sh
= avctx
->opaque
;
686 vd_ffmpeg_ctx
*ctx
= sh
->context
;
689 //printf("release buffer %d %d %d\n", mpi ? mpi->flags&MP_IMGFLAG_PRESERVE : -99, ctx->ip_count, ctx->b_count);
691 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
692 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
698 // Palette support: free palette buffer allocated in get_buffer
699 if ( mpi
&& (mpi
->bpp
== 8))
700 av_freep(&mpi
->planes
[1]);
702 if(pic
->type
!=FF_BUFFER_TYPE_USER
){
703 avcodec_default_release_buffer(avctx
, pic
);
710 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
713 // copypaste from demux_real.c - it should match to get it working!
714 //FIXME put into some header
715 typedef struct dp_hdr_s
{
716 uint32_t chunks
; // number of chunks
717 uint32_t timestamp
; // timestamp from packet header
718 uint32_t len
; // length of actual data
719 uint32_t chunktab
; // offset to chunk offset array
722 void swap_palette(void *pal
) {
725 for (i
= 0; i
< AVPALETTE_COUNT
; i
++)
726 p
[i
] = le2me_32(p
[i
]);
730 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
733 vd_ffmpeg_ctx
*ctx
= sh
->context
;
734 AVFrame
*pic
= ctx
->pic
;
735 AVCodecContext
*avctx
= ctx
->avctx
;
736 mp_image_t
* mpi
=NULL
;
737 int dr1
= ctx
->do_dr1
;
739 if(len
<=0) return NULL
; // skipped frame
741 //ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
743 avctx
->draw_horiz_band
=NULL
;
745 if(ctx
->vo_inited
&& !(flags
&3) && !dr1
){
746 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
|
747 (ctx
->do_slices
?MP_IMGFLAG_DRAW_CALLBACK
:0),
748 sh
->disp_w
, sh
->disp_h
);
749 if(mpi
&& mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
){
750 // vd core likes slices!
751 avctx
->draw_horiz_band
=draw_slice
;
755 avctx
->hurry_up
=(flags
&3)?((flags
&2)?2:1):0;
757 if(sh
->ds
->demuxer
->type
!= DEMUXER_TYPE_LAVF
)
758 if( sh
->format
== mmioFOURCC('R', 'V', '1', '0')
759 || sh
->format
== mmioFOURCC('R', 'V', '1', '3')
760 || sh
->format
== mmioFOURCC('R', 'V', '2', '0')
761 || sh
->format
== mmioFOURCC('R', 'V', '3', '0')
762 || sh
->format
== mmioFOURCC('R', 'V', '4', '0'))
764 dp_hdr_t
*hdr
= (dp_hdr_t
*)data
;
765 uint32_t *offsets
= (uint32_t*)(data
+ hdr
->chunktab
);
766 uint8_t *offstab
= av_malloc((hdr
->chunks
+1) * 8);
768 int chunks
= hdr
->chunks
;
772 memcpy(offstab
, offsets
, (chunks
+ 1) * 8);
773 memmove(buf
+ 1 + (chunks
+ 1) * 8, data
+ sizeof(dp_hdr_t
), dlen
);
774 memcpy(buf
+ 1, offstab
, (chunks
+ 1) * 8);
778 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
779 ((int *)data
)[0], ((int *)data
)[1], ((int *)data
)[2], ((int *)data
)[3]);
780 ret
= avcodec_decode_video(avctx
, pic
,
781 &got_picture
, data
, len
);
784 if(ret
<0) mp_msg(MSGT_DECVIDEO
,MSGL_WARN
, "Error while decoding frame!\n");
785 //printf("repeat: %d\n", pic->repeat_pict);
786 //-- vstats generation
787 while(lavc_param_vstats
){ // always one time loop
788 static FILE *fvstats
=NULL
;
790 static long long int all_len
=0;
791 static int frame_number
=0;
792 static double all_frametime
=0.0;
793 AVFrame
*pic
= avctx
->coded_frame
;
800 today
= localtime(&today2
);
801 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
802 today
->tm_min
, today
->tm_sec
);
803 fvstats
= fopen(filename
,"w");
806 lavc_param_vstats
=0; // disable block
812 // average MB quantizer
815 int w
= ((avctx
->width
<< lavc_param_lowres
)+15) >> 4;
816 int h
= ((avctx
->height
<< lavc_param_lowres
)+15) >> 4;
817 int8_t *q
= pic
->qscale_table
;
818 for( y
= 0; y
< h
; y
++ ) {
819 for( x
= 0; x
< w
; x
++ )
820 quality
+= (double)*(q
+x
);
827 all_frametime
+=sh
->frametime
;
828 fprintf(fvstats
, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
829 ++frame_number
, quality
, len
, (double)all_len
/1024);
830 fprintf(fvstats
, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
831 all_frametime
, (double)(len
*8)/sh
->frametime
/1000.0,
832 (double)(all_len
*8)/all_frametime
/1000.0);
833 switch(pic
->pict_type
){
835 fprintf(fvstats
, "type= I\n");
838 fprintf(fvstats
, "type= P\n");
841 fprintf(fvstats
, "type= S\n");
844 fprintf(fvstats
, "type= B\n");
847 fprintf(fvstats
, "type= ? (%d)\n", pic
->pict_type
);
851 ctx
->qp_stat
[(int)(quality
+0.5)]++;
852 ctx
->qp_sum
+= quality
;
853 ctx
->inv_qp_sum
+= 1.0/(double)quality
;
859 if(!got_picture
) return NULL
; // skipped image
861 if(init_vo(sh
,avctx
->pix_fmt
) < 0) return NULL
;
863 if(dr1
&& pic
->opaque
){
864 mpi
= (mp_image_t
*)pic
->opaque
;
868 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
,
869 avctx
->width
, avctx
->height
);
870 if(!mpi
){ // temporary!
871 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_CouldntAllocateImageForCodec
);
876 mpi
->planes
[0]=pic
->data
[0];
877 mpi
->planes
[1]=pic
->data
[1];
878 mpi
->planes
[2]=pic
->data
[2];
879 mpi
->stride
[0]=pic
->linesize
[0];
880 mpi
->stride
[1]=pic
->linesize
[1];
881 mpi
->stride
[2]=pic
->linesize
[2];
887 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& mpi
->chroma_y_shift
==1){
888 // we have 422p but user wants 420p
893 #ifdef WORDS_BIGENDIAN
894 // FIXME: this might cause problems for buffers with FF_BUFFER_HINTS_PRESERVE
896 swap_palette(mpi
->planes
[1]);
898 /* to comfirm with newer lavc style */
899 mpi
->qscale
=pic
->qscale_table
;
900 mpi
->qstride
=pic
->qstride
;
901 mpi
->pict_type
=pic
->pict_type
;
902 mpi
->qscale_type
= pic
->qscale_type
;
903 mpi
->fields
= MP_IMGFIELD_ORDERED
;
904 if(pic
->interlaced_frame
) mpi
->fields
|= MP_IMGFIELD_INTERLACED
;
905 if(pic
->top_field_first
) mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
906 if(pic
->repeat_pict
== 1) mpi
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
912 static enum PixelFormat
get_format(struct AVCodecContext
* avctx
,
913 const enum PixelFormat
* fmt
){
914 sh_video_t
* sh
= avctx
->opaque
;
917 if(avctx
->xvmc_acceleration
){
918 vd_ffmpeg_ctx
*ctx
= sh
->context
;
919 avctx
->get_buffer
= mc_get_buffer
;
920 avctx
->release_buffer
= mc_release_buffer
;
921 avctx
->draw_horiz_band
= mc_render_slice
;
922 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_XVMCAcceleratedMPEG2
);
923 assert(ctx
->do_dr1
);//these are must to!
924 assert(ctx
->do_slices
); //it is (vo_)ffmpeg bug if this fails
925 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;//do i need that??!!
926 avctx
->slice_flags
=SLICE_FLAG_CODED_ORDER
|SLICE_FLAG_ALLOW_FIELD
;
928 for(i
=0;fmt
[i
]!=-1;i
++){
929 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_TryingPixfmt
,i
);
930 if( init_vo(sh
,fmt
[i
]) >= 0)
936 static int mc_get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
937 sh_video_t
* sh
= avctx
->opaque
;
938 vd_ffmpeg_ctx
*ctx
= sh
->context
;
939 mp_image_t
* mpi
=NULL
;
940 xvmc_render_state_t
* render
;
941 int flags
= MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
|
942 MP_IMGFLAG_DRAW_CALLBACK
;
944 // printf("vd_ffmpeg::mc_get_buffer (xvmc) %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
945 if(!avctx
->xvmc_acceleration
){
946 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_McGetBufferShouldWorkOnlyWithXVMC
);
949 // return -1;//!!fixme check error conditions
951 assert(avctx
->draw_horiz_band
== mc_render_slice
);
952 assert(avctx
->release_buffer
== mc_release_buffer
);
953 if( mp_msg_test(MSGT_DECVIDEO
,MSGL_DBG5
) )
954 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::mc_get_buffer\n");
956 if(init_vo(sh
,avctx
->pix_fmt
) < 0){
957 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_UnexpectedInitVoError
);
959 // return -1;//!!fixme check error conditions
968 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
;
971 mpi
= mpcodecs_get_image(sh
, MP_IMGTYPE_IPB
,flags
,
972 avctx
->width
, avctx
->height
);
974 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MPCODECS_UnrecoverableErrorRenderBuffersNotTaken
);
977 // return -1;//!!fixme check error conditions in ffmpeg
980 if( (mpi
->flags
& MP_IMGFLAG_DIRECT
) == 0){
981 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MPCODECS_OnlyBuffersAllocatedByVoXvmcAllowed
);
984 // return -1;//!!fixme check error conditions in ffmpeg
987 pic
->data
[0]= mpi
->planes
[0];
988 pic
->data
[1]= mpi
->planes
[1];
989 pic
->data
[2]= mpi
->planes
[2];
992 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
993 * lavc will check that and die with an error message, if its not true
995 pic
->linesize
[0]= mpi
->stride
[0];
996 pic
->linesize
[1]= mpi
->stride
[1];
997 pic
->linesize
[2]= mpi
->stride
[2];
1003 pic
->age
= ctx
->ip_age
[0];
1005 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
1010 pic
->age
= ctx
->b_age
;
1017 pic
->type
= FF_BUFFER_TYPE_USER
;
1019 render
=(xvmc_render_state_t
*)mpi
->priv
;//same as data[2]
1020 if( mp_msg_test(MSGT_DECVIDEO
,MSGL_DBG5
) )
1021 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::mc_get_buffer (render=%p)\n",render
);
1022 assert(render
!= 0);
1023 assert(render
->magic
== MP_XVMC_RENDER_MAGIC
);
1024 render
->state
|= MP_XVMC_STATE_PREDICTION
;
1029 static void mc_release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
1030 mp_image_t
* mpi
= pic
->opaque
;
1031 sh_video_t
* sh
= avctx
->opaque
;
1032 vd_ffmpeg_ctx
*ctx
= sh
->context
;
1033 xvmc_render_state_t
* render
;
1037 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
1038 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
1044 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
1045 //mark the surface as not requared for prediction
1046 render
=(xvmc_render_state_t
*)pic
->data
[2];//same as mpi->priv
1047 if( mp_msg_test(MSGT_DECVIDEO
,MSGL_DBG5
) )
1048 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::mc_release_buffer (render=%p)\n",render
);
1049 assert(render
!=NULL
);
1050 assert(render
->magic
==MP_XVMC_RENDER_MAGIC
);
1051 render
->state
&=~MP_XVMC_STATE_PREDICTION
;
1057 static void mc_render_slice(struct AVCodecContext
*s
,
1058 AVFrame
*src
, int offset
[4],
1059 int y
, int type
, int height
){
1060 int width
= s
->width
;
1061 sh_video_t
* sh
= s
->opaque
;
1062 uint8_t *source
[3]= {src
->data
[0], src
->data
[1], src
->data
[2]};
1064 assert(src
->linesize
[0]==0 && src
->linesize
[1]==0 && src
->linesize
[2]==0);
1065 assert(offset
[0]==0 && offset
[1]==0 && offset
[2]==0);
1067 mpcodecs_draw_slice (sh
, source
, src
->linesize
, width
, height
, 0, y
);