11 #include "libavutil/common.h"
12 #include "libavutil/intreadwrite.h"
14 #include "fmt-conversion.h"
16 #include "vd_internal.h"
18 static vd_info_t info
= {
19 "FFmpeg's libavcodec codec family",
22 "A'rpi, Michael, Alex",
28 #include "libavcodec/avcodec.h"
31 #include "libavcodec/xvmc.h"
34 int avcodec_initialized
=0;
37 AVCodecContext
*avctx
;
39 enum PixelFormat pix_fmt
;
51 AVRational last_sample_aspect_ratio
;
56 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
57 static void release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
58 static void draw_slice(struct AVCodecContext
*s
, AVFrame
*src
, int offset
[4],
59 int y
, int type
, int height
);
61 static enum PixelFormat
get_format(struct AVCodecContext
*avctx
,
62 const enum PixelFormat
*pix_fmt
);
64 static int lavc_param_workaround_bugs
= FF_BUG_AUTODETECT
;
65 static int lavc_param_error_resilience
=2;
66 static int lavc_param_error_concealment
=3;
67 static int lavc_param_gray
=0;
68 static int lavc_param_vstats
=0;
69 static int lavc_param_idct_algo
=0;
70 static int lavc_param_debug
=0;
71 static int lavc_param_vismv
=0;
72 static int lavc_param_skip_top
=0;
73 static int lavc_param_skip_bottom
=0;
74 static int lavc_param_fast
=0;
75 static int lavc_param_lowres
=0;
76 static char *lavc_param_lowres_str
=NULL
;
77 static char *lavc_param_skip_loop_filter_str
= NULL
;
78 static char *lavc_param_skip_idct_str
= NULL
;
79 static char *lavc_param_skip_frame_str
= NULL
;
80 static int lavc_param_threads
=1;
81 static int lavc_param_bitexact
=0;
82 static char *lavc_avopt
= NULL
;
84 const m_option_t lavc_decode_opts_conf
[]={
85 {"bug", &lavc_param_workaround_bugs
, CONF_TYPE_INT
, CONF_RANGE
, -1, 999999, NULL
},
86 {"er", &lavc_param_error_resilience
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
87 {"gray", &lavc_param_gray
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_PART
, NULL
},
88 {"idct", &lavc_param_idct_algo
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
89 {"ec", &lavc_param_error_concealment
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
90 {"vstats", &lavc_param_vstats
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
91 {"debug", &lavc_param_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9999999, NULL
},
92 {"vismv", &lavc_param_vismv
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9999999, NULL
},
93 {"st", &lavc_param_skip_top
, CONF_TYPE_INT
, CONF_RANGE
, 0, 999, NULL
},
94 {"sb", &lavc_param_skip_bottom
, CONF_TYPE_INT
, CONF_RANGE
, 0, 999, NULL
},
95 {"fast", &lavc_param_fast
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG2_FAST
, NULL
},
96 {"lowres", &lavc_param_lowres_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
97 {"skiploopfilter", &lavc_param_skip_loop_filter_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
98 {"skipidct", &lavc_param_skip_idct_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
99 {"skipframe", &lavc_param_skip_frame_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
100 {"threads", &lavc_param_threads
, CONF_TYPE_INT
, CONF_RANGE
, 1, 8, NULL
},
101 {"bitexact", &lavc_param_bitexact
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_BITEXACT
, NULL
},
102 {"o", &lavc_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
103 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
106 static enum AVDiscard
str2AVDiscard(char *str
) {
107 if (!str
) return AVDISCARD_DEFAULT
;
108 if (strcasecmp(str
, "none" ) == 0) return AVDISCARD_NONE
;
109 if (strcasecmp(str
, "default") == 0) return AVDISCARD_DEFAULT
;
110 if (strcasecmp(str
, "nonref" ) == 0) return AVDISCARD_NONREF
;
111 if (strcasecmp(str
, "bidir" ) == 0) return AVDISCARD_BIDIR
;
112 if (strcasecmp(str
, "nonkey" ) == 0) return AVDISCARD_NONKEY
;
113 if (strcasecmp(str
, "all" ) == 0) return AVDISCARD_ALL
;
114 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Unknown discard value %s\n", str
);
115 return AVDISCARD_DEFAULT
;
118 // to set/get/query special features/parameters
119 static int control(sh_video_t
*sh
, int cmd
, void *arg
, ...){
120 vd_ffmpeg_ctx
*ctx
= sh
->context
;
121 AVCodecContext
*avctx
= ctx
->avctx
;
123 case VDCTRL_QUERY_FORMAT
:
125 int format
=(*((int *)arg
));
126 if(format
== ctx
->best_csp
) return CONTROL_TRUE
;//supported
127 // possible conversions:
132 // "converted" using pointer/stride modification
133 if(avctx
->pix_fmt
==PIX_FMT_YUV420P
) return CONTROL_TRUE
;// u/v swap
134 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& !ctx
->do_dr1
) return CONTROL_TRUE
;// half stride
137 case IMGFMT_XVMC_IDCT_MPEG2
:
138 case IMGFMT_XVMC_MOCO_MPEG2
:
139 if(avctx
->pix_fmt
==PIX_FMT_XVMC_MPEG2_IDCT
) return CONTROL_TRUE
;
142 return CONTROL_FALSE
;
144 case VDCTRL_RESYNC_STREAM
:
145 avcodec_flush_buffers(avctx
);
147 case VDCTRL_QUERY_UNSEEN_FRAMES
:
148 return avctx
->has_b_frames
+ 10;
150 return CONTROL_UNKNOWN
;
153 void mp_msp_av_log_callback(void *ptr
, int level
, const char *fmt
, va_list vl
)
155 static int print_prefix
=1;
156 AVClass
*avc
= ptr
? *(AVClass
**)ptr
: NULL
;
157 int type
= MSGT_FIXME
;
162 case AV_LOG_DEBUG
: mp_level
= MSGL_V
; break;
163 case AV_LOG_INFO
: mp_level
= MSGL_INFO
; break;
164 case AV_LOG_ERROR
: mp_level
= MSGL_ERR
; break;
165 default : mp_level
= MSGL_ERR
; break;
168 if (!mp_msg_test(type
, mp_level
)) return;
171 if(!strcmp(avc
->class_name
, "AVCodecContext")){
172 AVCodecContext
*s
= ptr
;
174 if(s
->codec
->type
== CODEC_TYPE_AUDIO
){
177 }else if(s
->codec
->type
== CODEC_TYPE_VIDEO
){
181 //FIXME subtitles, encoders (what msgt for them? there is no appropriate ...)
183 }else if(!strcmp(avc
->class_name
, "AVFormatContext")){
184 #if 0 //needs libavformat include FIXME iam too lazy to do this cleanly, probably the whole should be moved out of this file ...
185 AVFormatContext
*s
= ptr
;
194 if(print_prefix
&& avc
) {
195 mp_msg(type
, mp_level
, "[%s @ %p]", avc
->item_name(ptr
), avc
);
198 print_prefix
= strchr(fmt
, '\n') != NULL
;
199 vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
200 mp_msg(type
, mp_level
, buf
);
204 static int init(sh_video_t
*sh
){
205 AVCodecContext
*avctx
;
209 int do_vis_debug
= lavc_param_vismv
|| (lavc_param_debug
&(FF_DEBUG_VIS_MB_TYPE
|FF_DEBUG_VIS_QP
));
211 if(!avcodec_initialized
){
213 avcodec_register_all();
214 avcodec_initialized
=1;
215 av_log_set_callback(mp_msp_av_log_callback
);
218 ctx
= sh
->context
= malloc(sizeof(vd_ffmpeg_ctx
));
221 memset(ctx
, 0, sizeof(vd_ffmpeg_ctx
));
223 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh
->codec
->dll
);
225 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MissingLAVCcodec
, sh
->codec
->dll
);
230 if(vd_use_slices
&& (lavc_codec
->capabilities
&CODEC_CAP_DRAW_HORIZ_BAND
) && !do_vis_debug
)
233 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
)
235 ctx
->b_age
= ctx
->ip_age
[0]= ctx
->ip_age
[1]= 256*256*256*64;
236 ctx
->ip_count
= ctx
->b_count
= 0;
238 ctx
->pic
= avcodec_alloc_frame();
239 ctx
->avctx
= avcodec_alloc_context();
244 if(lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
){
245 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_XVMCAcceleratedCodec
);
246 assert(ctx
->do_dr1
);//these are must to!
247 assert(ctx
->do_slices
); //it is (vo_)ffmpeg bug if this fails
248 avctx
->get_format
= get_format
;//for now only this decoder will use it
249 avctx
->draw_horiz_band
= draw_slice
;
250 avctx
->slice_flags
=SLICE_FLAG_CODED_ORDER
|SLICE_FLAG_ALLOW_FIELD
;
252 #endif /* CONFIG_XVMC */
254 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
255 avctx
->get_buffer
= get_buffer
;
256 avctx
->release_buffer
= release_buffer
;
257 avctx
->reget_buffer
= get_buffer
;
260 avctx
->flags
|= lavc_param_bitexact
;
262 avctx
->width
= sh
->disp_w
;
263 avctx
->height
= sh
->disp_h
;
264 avctx
->workaround_bugs
= lavc_param_workaround_bugs
;
265 avctx
->error_recognition
= lavc_param_error_resilience
;
266 if(lavc_param_gray
) avctx
->flags
|= CODEC_FLAG_GRAY
;
267 avctx
->flags2
|= lavc_param_fast
;
268 avctx
->codec_tag
= sh
->format
;
269 avctx
->stream_codec_tag
= sh
->video
.fccHandler
;
270 avctx
->idct_algo
= lavc_param_idct_algo
;
271 avctx
->error_concealment
= lavc_param_error_concealment
;
272 avctx
->debug
= lavc_param_debug
;
273 if (lavc_param_debug
)
274 av_log_set_level(AV_LOG_DEBUG
);
275 avctx
->debug_mv
= lavc_param_vismv
;
276 avctx
->skip_top
= lavc_param_skip_top
;
277 avctx
->skip_bottom
= lavc_param_skip_bottom
;
278 if(lavc_param_lowres_str
!= NULL
)
280 sscanf(lavc_param_lowres_str
, "%d,%d", &lavc_param_lowres
, &lowres_w
);
281 if(lavc_param_lowres
< 1 || lavc_param_lowres
> 16 || (lowres_w
> 0 && avctx
->width
< lowres_w
))
282 lavc_param_lowres
= 0;
283 avctx
->lowres
= lavc_param_lowres
;
285 avctx
->skip_loop_filter
= str2AVDiscard(lavc_param_skip_loop_filter_str
);
286 avctx
->skip_idct
= str2AVDiscard(lavc_param_skip_idct_str
);
287 avctx
->skip_frame
= str2AVDiscard(lavc_param_skip_frame_str
);
290 if(parse_avopts(avctx
, lavc_avopt
) < 0){
291 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavc_avopt
);
297 mp_dbg(MSGT_DECVIDEO
, MSGL_DBG2
, "libavcodec.size: %d x %d\n", avctx
->width
, avctx
->height
);
298 switch (sh
->format
) {
299 case mmioFOURCC('S','V','Q','3'):
300 /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or
301 in the phony AVI header if demux_lavf is used. The first case is
302 handled here; the second case falls through to the next section. */
304 avctx
->extradata_size
= (*(int *)sh
->ImageDesc
) - sizeof(int);
305 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
306 memcpy(avctx
->extradata
, ((int *)sh
->ImageDesc
)+1, avctx
->extradata_size
);
311 case mmioFOURCC('A','V','R','n'):
312 case mmioFOURCC('M','J','P','G'):
313 /* AVRn stores huffman table in AVI header */
314 /* Pegasus MJPEG stores it also in AVI header, but it uses the common
316 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
318 avctx
->flags
|= CODEC_FLAG_EXTERN_HUFF
;
319 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
320 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
321 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
326 uint8_t *p
= avctx
->extradata
;
328 for (x
=0; x
<avctx
->extradata_size
; x
++)
329 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "[%x] ", p
[x
]);
330 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "\n");
335 case mmioFOURCC('R', 'V', '1', '0'):
336 case mmioFOURCC('R', 'V', '1', '3'):
337 case mmioFOURCC('R', 'V', '2', '0'):
338 case mmioFOURCC('R', 'V', '3', '0'):
339 case mmioFOURCC('R', 'V', '4', '0'):
340 if(sh
->bih
->biSize
<sizeof(*sh
->bih
)+8){
341 /* only 1 packet per frame & sub_id from fourcc */
342 avctx
->extradata_size
= 8;
343 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
344 ((uint32_t *)avctx
->extradata
)[0] = 0;
345 ((uint32_t *)avctx
->extradata
)[1] =
346 (sh
->format
== mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
348 /* has extra slice header (demux_rm or rm->avi streamcopy) */
349 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
350 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
351 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
353 avctx
->sub_id
= AV_RB32(avctx
->extradata
+4);
355 // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]);
359 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
361 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
362 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
363 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
366 /* Pass palette to codec */
367 if (sh
->bih
&& (sh
->bih
->biBitCount
<= 8)) {
368 avctx
->palctrl
= calloc(1, sizeof(AVPaletteControl
));
369 avctx
->palctrl
->palette_changed
= 1;
370 if (sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
))
371 /* Palette size in biSize */
372 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
373 FFMIN(sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
), AVPALETTE_SIZE
));
375 /* Palette size in biClrUsed */
376 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
377 FFMIN(sh
->bih
->biClrUsed
* 4, AVPALETTE_SIZE
));
381 avctx
->bits_per_coded_sample
= sh
->bih
->biBitCount
;
383 if(lavc_param_threads
> 1)
384 avcodec_thread_init(avctx
, lavc_param_threads
);
386 if (avcodec_open(avctx
, lavc_codec
) < 0) {
387 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantOpenCodec
);
391 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "INFO: libavcodec init OK!\n");
392 return 1; //mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h, IMGFMT_YV12);
396 static void uninit(sh_video_t
*sh
){
397 vd_ffmpeg_ctx
*ctx
= sh
->context
;
398 AVCodecContext
*avctx
= ctx
->avctx
;
400 if(lavc_param_vstats
){
403 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "QP: %d, count: %d\n", i
, ctx
->qp_stat
[i
]);
405 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_ArithmeticMeanOfQP
,
406 ctx
->qp_sum
/ avctx
->coded_frame
->coded_picture_number
,
407 1.0/(ctx
->inv_qp_sum
/ avctx
->coded_frame
->coded_picture_number
)
412 if (avctx
->codec
&& avcodec_close(avctx
) < 0)
413 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_CantCloseCodec
);
415 av_freep(&avctx
->extradata
);
416 av_freep(&avctx
->palctrl
);
417 av_freep(&avctx
->slice_offset
);
426 static void draw_slice(struct AVCodecContext
*s
,
427 AVFrame
*src
, int offset
[4],
428 int y
, int type
, int height
){
429 sh_video_t
*sh
= s
->opaque
;
430 uint8_t *source
[3]= {src
->data
[0] + offset
[0], src
->data
[1] + offset
[1], src
->data
[2] + offset
[2]};
434 int skip_stride
= ((width
<<lavc_param_lowres
)+15)>>4;
435 uint8_t *skip
= &s
->coded_frame
->mbskip_table
[(y
>>4)*skip_stride
];
436 int threshold
= s
->coded_frame
->age
;
437 if(s
->pict_type
!=B_TYPE
){
438 for(i
=0; i
*16<width
+16; i
++){
439 if(i
*16>=width
|| skip
[i
]>=threshold
){
440 if(start
==i
) start
++;
442 uint8_t *src2
[3]= {src
[0] + start
*16,
445 //printf("%2d-%2d x %d\n", start, i, y);
446 mpcodecs_draw_slice (sh
, src2
, stride
, (i
-start
)*16, height
, start
*16, y
);
453 if (y
< sh
->disp_h
) {
454 mpcodecs_draw_slice (sh
, source
, src
->linesize
, sh
->disp_w
, (y
+height
)<=sh
->disp_h
?height
:sh
->disp_h
-y
, 0, y
);
459 static int init_vo(sh_video_t
*sh
, enum PixelFormat pix_fmt
){
460 vd_ffmpeg_ctx
*ctx
= sh
->context
;
461 AVCodecContext
*avctx
= ctx
->avctx
;
462 float aspect
= av_q2d(avctx
->sample_aspect_ratio
) * avctx
->width
/ avctx
->height
;
465 width
= avctx
->width
;
466 height
= avctx
->height
;
469 // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
470 // use dimensions from BIH to avoid black borders at the right and bottom.
471 if (sh
->bih
&& sh
->ImageDesc
) {
472 width
= sh
->bih
->biWidth
>>lavc_param_lowres
;
473 height
= sh
->bih
->biHeight
>>lavc_param_lowres
;
476 // it is possible another vo buffers to be used after vo config()
477 // lavc reset its buffers on width/heigh change but not on aspect change!!!
478 if (av_cmp_q(avctx
->sample_aspect_ratio
, ctx
->last_sample_aspect_ratio
) ||
479 width
!= sh
->disp_w
||
480 height
!= sh
->disp_h
||
481 pix_fmt
!= ctx
->pix_fmt
||
482 !ctx
->vo_initialized
)
484 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[ffmpeg] aspect_ratio: %f\n", aspect
);
485 if (sh
->aspect
== 0 ||
486 av_cmp_q(avctx
->sample_aspect_ratio
,
487 ctx
->last_sample_aspect_ratio
))
489 ctx
->last_sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
492 ctx
->pix_fmt
= pix_fmt
;
493 ctx
->best_csp
= pixfmt2imgfmt(pix_fmt
);
494 if (!mpcodecs_config_vo(sh
, sh
->disp_w
, sh
->disp_h
, ctx
->best_csp
))
496 ctx
->vo_initialized
= 1;
501 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
502 sh_video_t
*sh
= avctx
->opaque
;
503 vd_ffmpeg_ctx
*ctx
= sh
->context
;
504 mp_image_t
*mpi
=NULL
;
505 int flags
= MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
;
506 int type
= MP_IMGTYPE_IPB
;
507 int width
= avctx
->width
;
508 int height
= avctx
->height
;
510 //printf("get_buffer %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
511 if(avctx
->pix_fmt
== PIX_FMT_YUV410P
)
512 align
=63; //yes seriously, its really needed (16x16 chroma blocks in SVQ1 -> 64x64)
514 if (pic
->buffer_hints
) {
515 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "Buffer hints: %u\n", pic
->buffer_hints
);
516 type
= MP_IMGTYPE_TEMP
;
517 if (pic
->buffer_hints
& FF_BUFFER_HINTS_READABLE
)
518 flags
|= MP_IMGFLAG_READABLE
;
519 if (pic
->buffer_hints
& FF_BUFFER_HINTS_PRESERVE
) {
520 type
= MP_IMGTYPE_STATIC
;
521 flags
|= MP_IMGFLAG_PRESERVE
;
523 if (pic
->buffer_hints
& FF_BUFFER_HINTS_REUSABLE
) {
524 type
= MP_IMGTYPE_STATIC
;
525 flags
|= MP_IMGFLAG_PRESERVE
;
527 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
528 MP_IMGFLAG_DRAW_CALLBACK
:0;
529 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, type
== MP_IMGTYPE_STATIC
? "using STATIC\n" : "using TEMP\n");
533 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
534 MP_IMGFLAG_DRAW_CALLBACK
:0;
537 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
538 | (ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0);
542 if(init_vo(sh
, avctx
->pix_fmt
) < 0){
543 avctx
->release_buffer
= avcodec_default_release_buffer
;
544 avctx
->get_buffer
= avcodec_default_get_buffer
;
545 return avctx
->get_buffer(avctx
, pic
);
548 if (IMGFMT_IS_XVMC(ctx
->best_csp
)) {
549 type
= MP_IMGTYPE_NUMBERED
| (0xffff << 16);
551 if (!pic
->buffer_hints
) {
552 if(ctx
->b_count
>1 || ctx
->ip_count
>2){
553 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_DRIFailure
);
555 ctx
->do_dr1
=0; //FIXME
556 avctx
->get_buffer
= avcodec_default_get_buffer
;
557 return avctx
->get_buffer(avctx
, pic
);
560 if(avctx
->has_b_frames
){
561 type
= MP_IMGTYPE_IPB
;
565 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, type
== MP_IMGTYPE_IPB
? "using IPB\n" : "using IP\n");
568 mpi
= mpcodecs_get_image(sh
, type
, flags
,
569 (width
+align
)&(~align
), (height
+align
)&(~align
));
571 // ok, let's see what did we get:
572 if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&&
573 !(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
574 // nice, filter/vo likes draw_callback :)
575 avctx
->draw_horiz_band
= draw_slice
;
577 avctx
->draw_horiz_band
= NULL
;
579 if(IMGFMT_IS_XVMC(mpi
->imgfmt
)) {
580 struct xvmc_pixfmt_render
*render
= mpi
->priv
;//same as data[2]
581 avctx
->draw_horiz_band
= draw_slice
;
582 if(!avctx
->xvmc_acceleration
) {
583 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_McGetBufferShouldWorkOnlyWithXVMC
);
586 // return -1;//!!fixme check error conditions in ffmpeg
588 if(!(mpi
->flags
& MP_IMGFLAG_DIRECT
)) {
589 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MPCODECS_OnlyBuffersAllocatedByVoXvmcAllowed
);
592 // return -1;//!!fixme check error conditions in ffmpeg
594 if(mp_msg_test(MSGT_DECVIDEO
, MSGL_DBG5
))
595 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::get_buffer (xvmc render=%p)\n", render
);
597 assert(render
->magic_id
== AV_XVMC_RENDER_MAGIC
);
598 render
->state
|= AV_XVMC_STATE_PREDICTION
;
602 // Palette support: libavcodec copies palette to *data[1]
604 mpi
->planes
[1] = av_malloc(AVPALETTE_SIZE
);
606 pic
->data
[0]= mpi
->planes
[0];
607 pic
->data
[1]= mpi
->planes
[1];
608 pic
->data
[2]= mpi
->planes
[2];
611 assert(mpi
->width
>= ((width
+align
)&(~align
)));
612 assert(mpi
->height
>= ((height
+align
)&(~align
)));
613 assert(mpi
->stride
[0] >= mpi
->width
);
614 if(mpi
->imgfmt
==IMGFMT_I420
|| mpi
->imgfmt
==IMGFMT_YV12
|| mpi
->imgfmt
==IMGFMT_IYUV
){
615 const int y_size
= mpi
->stride
[0] * (mpi
->h
-1) + mpi
->w
;
616 const int c_size
= mpi
->stride
[1] * ((mpi
->h
>>1)-1) + (mpi
->w
>>1);
618 assert(mpi
->planes
[0] > mpi
->planes
[1] || mpi
->planes
[0] + y_size
<= mpi
->planes
[1]);
619 assert(mpi
->planes
[0] > mpi
->planes
[2] || mpi
->planes
[0] + y_size
<= mpi
->planes
[2]);
620 assert(mpi
->planes
[1] > mpi
->planes
[0] || mpi
->planes
[1] + c_size
<= mpi
->planes
[0]);
621 assert(mpi
->planes
[1] > mpi
->planes
[2] || mpi
->planes
[1] + c_size
<= mpi
->planes
[2]);
622 assert(mpi
->planes
[2] > mpi
->planes
[0] || mpi
->planes
[2] + c_size
<= mpi
->planes
[0]);
623 assert(mpi
->planes
[2] > mpi
->planes
[1] || mpi
->planes
[2] + c_size
<= mpi
->planes
[1]);
627 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
628 * lavc will check that and die with an error message, if its not true
630 pic
->linesize
[0]= mpi
->stride
[0];
631 pic
->linesize
[1]= mpi
->stride
[1];
632 pic
->linesize
[2]= mpi
->stride
[2];
635 //printf("%X\n", (int)mpi->planes[0]);
637 if(mpi
->flags
&MP_IMGFLAG_DIRECT
)
639 else if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)
645 pic
->age
= ctx
->ip_age
[0];
647 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
651 pic
->age
= ctx
->b_age
;
657 pic
->type
= FF_BUFFER_TYPE_USER
;
661 static void release_buffer(struct AVCodecContext
*avctx
, AVFrame
*pic
){
662 mp_image_t
*mpi
= pic
->opaque
;
663 sh_video_t
*sh
= avctx
->opaque
;
664 vd_ffmpeg_ctx
*ctx
= sh
->context
;
667 //printf("release buffer %d %d %d\n", mpi ? mpi->flags&MP_IMGFLAG_PRESERVE : -99, ctx->ip_count, ctx->b_count);
669 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
670 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
677 // Palette support: free palette buffer allocated in get_buffer
679 av_freep(&mpi
->planes
[1]);
681 if (IMGFMT_IS_XVMC(mpi
->imgfmt
)) {
682 struct xvmc_pixfmt_render
*render
= (struct xvmc_pixfmt_render
*)pic
->data
[2];//same as mpi->priv
683 if(mp_msg_test(MSGT_DECVIDEO
, MSGL_DBG5
))
684 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::release_buffer (xvmc render=%p)\n", render
);
685 assert(render
!=NULL
);
686 assert(render
->magic_id
==AV_XVMC_RENDER_MAGIC
);
687 render
->state
&=~AV_XVMC_STATE_PREDICTION
;
690 // release mpi (in case MPI_IMGTYPE_NUMBERED is used, e.g. for VDPAU)
691 mpi
->flags
&= ~MP_IMGFLAG_IN_USE
;
694 if(pic
->type
!=FF_BUFFER_TYPE_USER
){
695 avcodec_default_release_buffer(avctx
, pic
);
702 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
705 // copypaste from demux_real.c - it should match to get it working!
706 //FIXME put into some header
707 typedef struct dp_hdr_s
{
708 uint32_t chunks
; // number of chunks
709 uint32_t timestamp
; // timestamp from packet header
710 uint32_t len
; // length of actual data
711 uint32_t chunktab
; // offset to chunk offset array
714 void swap_palette(void *pal
) {
717 for (i
= 0; i
< AVPALETTE_COUNT
; i
++)
718 p
[i
] = le2me_32(p
[i
]);
722 static mp_image_t
*decode(sh_video_t
*sh
, void *data
, int len
, int flags
){
725 vd_ffmpeg_ctx
*ctx
= sh
->context
;
726 AVFrame
*pic
= ctx
->pic
;
727 AVCodecContext
*avctx
= ctx
->avctx
;
728 mp_image_t
*mpi
=NULL
;
729 int dr1
= ctx
->do_dr1
;
731 if(len
<=0) return NULL
; // skipped frame
733 //ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
735 avctx
->draw_horiz_band
=NULL
;
737 if(ctx
->vo_initialized
&& !(flags
&3) && !dr1
){
738 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
|
739 (ctx
->do_slices
?MP_IMGFLAG_DRAW_CALLBACK
:0),
740 sh
->disp_w
, sh
->disp_h
);
741 if(mpi
&& mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
){
742 // vd core likes slices!
743 avctx
->draw_horiz_band
=draw_slice
;
747 avctx
->hurry_up
=(flags
&3)?((flags
&2)?2:1):0;
749 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
750 ((int *)data
)[0], ((int *)data
)[1], ((int *)data
)[2], ((int *)data
)[3]);
751 ret
= avcodec_decode_video(avctx
, pic
,
752 &got_picture
, data
, len
);
755 if(ret
<0) mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error while decoding frame!\n");
756 //printf("repeat: %d\n", pic->repeat_pict);
757 //-- vstats generation
758 while(lavc_param_vstats
){ // always one time loop
759 static FILE *fvstats
=NULL
;
761 static long long int all_len
=0;
762 static int frame_number
=0;
763 static double all_frametime
=0.0;
764 AVFrame
*pic
= avctx
->coded_frame
;
771 today
= localtime(&today2
);
772 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
773 today
->tm_min
, today
->tm_sec
);
774 fvstats
= fopen(filename
, "w");
777 lavc_param_vstats
=0; // disable block
783 // average MB quantizer
786 int w
= ((avctx
->width
<< lavc_param_lowres
)+15) >> 4;
787 int h
= ((avctx
->height
<< lavc_param_lowres
)+15) >> 4;
788 int8_t *q
= pic
->qscale_table
;
789 for(y
= 0; y
< h
; y
++) {
790 for(x
= 0; x
< w
; x
++)
791 quality
+= (double)*(q
+x
);
798 all_frametime
+=sh
->frametime
;
799 fprintf(fvstats
, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
800 ++frame_number
, quality
, len
, (double)all_len
/1024);
801 fprintf(fvstats
, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
802 all_frametime
, (double)(len
*8)/sh
->frametime
/1000.0,
803 (double)(all_len
*8)/all_frametime
/1000.0);
804 switch(pic
->pict_type
){
806 fprintf(fvstats
, "type= I\n");
809 fprintf(fvstats
, "type= P\n");
812 fprintf(fvstats
, "type= S\n");
815 fprintf(fvstats
, "type= B\n");
818 fprintf(fvstats
, "type= ? (%d)\n", pic
->pict_type
);
822 ctx
->qp_stat
[(int)(quality
+0.5)]++;
823 ctx
->qp_sum
+= quality
;
824 ctx
->inv_qp_sum
+= 1.0/(double)quality
;
830 if(!got_picture
) return NULL
; // skipped image
832 if(init_vo(sh
, avctx
->pix_fmt
) < 0) return NULL
;
834 if(dr1
&& pic
->opaque
){
835 mpi
= (mp_image_t
*)pic
->opaque
;
839 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
,
840 avctx
->width
, avctx
->height
);
841 if(!mpi
){ // temporary!
842 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_CouldntAllocateImageForCodec
);
847 mpi
->planes
[0]=pic
->data
[0];
848 mpi
->planes
[1]=pic
->data
[1];
849 mpi
->planes
[2]=pic
->data
[2];
850 mpi
->stride
[0]=pic
->linesize
[0];
851 mpi
->stride
[1]=pic
->linesize
[1];
852 mpi
->stride
[2]=pic
->linesize
[2];
858 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& mpi
->chroma_y_shift
==1){
859 // we have 422p but user wants 420p
864 #ifdef WORDS_BIGENDIAN
865 // FIXME: this might cause problems for buffers with FF_BUFFER_HINTS_PRESERVE
867 swap_palette(mpi
->planes
[1]);
869 /* to comfirm with newer lavc style */
870 mpi
->qscale
=pic
->qscale_table
;
871 mpi
->qstride
=pic
->qstride
;
872 mpi
->pict_type
=pic
->pict_type
;
873 mpi
->qscale_type
= pic
->qscale_type
;
874 mpi
->fields
= MP_IMGFIELD_ORDERED
;
875 if(pic
->interlaced_frame
) mpi
->fields
|= MP_IMGFIELD_INTERLACED
;
876 if(pic
->top_field_first
) mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
877 if(pic
->repeat_pict
== 1) mpi
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
883 static enum PixelFormat
get_format(struct AVCodecContext
*avctx
,
884 const enum PixelFormat
*fmt
){
885 enum PixelFormat selected_format
= fmt
[0];
887 sh_video_t
*sh
= avctx
->opaque
;
890 for(i
=0;fmt
[i
]!=PIX_FMT_NONE
;i
++){
891 imgfmt
= pixfmt2imgfmt(fmt
[i
]);
892 if(!IMGFMT_IS_XVMC(imgfmt
)) continue;
893 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_TryingPixfmt
, i
);
894 if(init_vo(sh
, fmt
[i
]) >= 0) {
895 selected_format
= fmt
[i
];
899 imgfmt
= pixfmt2imgfmt(selected_format
);
900 if(IMGFMT_IS_XVMC(imgfmt
)) {
901 vd_ffmpeg_ctx
*ctx
= sh
->context
;
902 avctx
->get_buffer
= get_buffer
;
903 avctx
->release_buffer
= release_buffer
;
904 avctx
->draw_horiz_band
= draw_slice
;
905 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_XVMCAcceleratedMPEG2
);
906 assert(ctx
->do_dr1
);//these are must to!
907 assert(ctx
->do_slices
); //it is (vo_)ffmpeg bug if this fails
908 avctx
->slice_flags
=SLICE_FLAG_CODED_ORDER
|SLICE_FLAG_ALLOW_FIELD
;
910 return selected_format
;
913 #endif /* CONFIG_XVMC */