11 #include "libavutil/common.h"
12 #include "libavutil/intreadwrite.h"
15 #include "vd_internal.h"
17 static vd_info_t info
= {
18 "FFmpeg's libavcodec codec family",
21 "A'rpi, Michael, Alex",
27 #include "libavcodec/avcodec.h"
30 #include "xvmc_render.h"
33 int avcodec_initialized
=0;
36 AVCodecContext
*avctx
;
38 enum PixelFormat pix_fmt
;
50 AVRational last_sample_aspect_ratio
;
55 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
56 static void release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
59 static enum PixelFormat
get_format(struct AVCodecContext
* avctx
,
60 const enum PixelFormat
* pix_fmt
);
61 static int mc_get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
62 static void mc_release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
63 static void mc_render_slice(struct AVCodecContext
*s
,
64 AVFrame
*src
, int offset
[4],
65 int y
, int type
, int height
);
68 static int lavc_param_workaround_bugs
= FF_BUG_AUTODETECT
;
69 static int lavc_param_error_resilience
=2;
70 static int lavc_param_error_concealment
=3;
71 static int lavc_param_gray
=0;
72 static int lavc_param_vstats
=0;
73 static int lavc_param_idct_algo
=0;
74 static int lavc_param_debug
=0;
75 static int lavc_param_vismv
=0;
76 static int lavc_param_skip_top
=0;
77 static int lavc_param_skip_bottom
=0;
78 static int lavc_param_fast
=0;
79 static int lavc_param_lowres
=0;
80 static char *lavc_param_lowres_str
=NULL
;
81 static char *lavc_param_skip_loop_filter_str
= NULL
;
82 static char *lavc_param_skip_idct_str
= NULL
;
83 static char *lavc_param_skip_frame_str
= NULL
;
84 static int lavc_param_threads
=1;
85 static int lavc_param_bitexact
=0;
86 static char *lavc_avopt
= NULL
;
88 const m_option_t lavc_decode_opts_conf
[]={
89 {"bug", &lavc_param_workaround_bugs
, CONF_TYPE_INT
, CONF_RANGE
, -1, 999999, NULL
},
90 {"er", &lavc_param_error_resilience
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
91 {"gray", &lavc_param_gray
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_PART
, NULL
},
92 {"idct", &lavc_param_idct_algo
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
93 {"ec", &lavc_param_error_concealment
, CONF_TYPE_INT
, CONF_RANGE
, 0, 99, NULL
},
94 {"vstats", &lavc_param_vstats
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
95 {"debug", &lavc_param_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9999999, NULL
},
96 {"vismv", &lavc_param_vismv
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9999999, NULL
},
97 {"st", &lavc_param_skip_top
, CONF_TYPE_INT
, CONF_RANGE
, 0, 999, NULL
},
98 {"sb", &lavc_param_skip_bottom
, CONF_TYPE_INT
, CONF_RANGE
, 0, 999, NULL
},
99 {"fast", &lavc_param_fast
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG2_FAST
, NULL
},
100 {"lowres", &lavc_param_lowres_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
101 {"skiploopfilter", &lavc_param_skip_loop_filter_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
102 {"skipidct", &lavc_param_skip_idct_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
103 {"skipframe", &lavc_param_skip_frame_str
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
104 {"threads", &lavc_param_threads
, CONF_TYPE_INT
, CONF_RANGE
, 1, 8, NULL
},
105 {"bitexact", &lavc_param_bitexact
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_BITEXACT
, NULL
},
106 {"o", &lavc_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
107 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
110 static enum AVDiscard
str2AVDiscard(char *str
) {
111 if (!str
) return AVDISCARD_DEFAULT
;
112 if (strcasecmp(str
, "none" ) == 0) return AVDISCARD_NONE
;
113 if (strcasecmp(str
, "default") == 0) return AVDISCARD_DEFAULT
;
114 if (strcasecmp(str
, "nonref" ) == 0) return AVDISCARD_NONREF
;
115 if (strcasecmp(str
, "bidir" ) == 0) return AVDISCARD_BIDIR
;
116 if (strcasecmp(str
, "nonkey" ) == 0) return AVDISCARD_NONKEY
;
117 if (strcasecmp(str
, "all" ) == 0) return AVDISCARD_ALL
;
118 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Unknown discard value %s\n", str
);
119 return AVDISCARD_DEFAULT
;
122 // to set/get/query special features/parameters
123 static int control(sh_video_t
*sh
,int cmd
,void* arg
,...){
124 vd_ffmpeg_ctx
*ctx
= sh
->context
;
125 AVCodecContext
*avctx
= ctx
->avctx
;
127 case VDCTRL_QUERY_FORMAT
:
129 int format
=(*((int*)arg
));
130 if( format
== ctx
->best_csp
) return CONTROL_TRUE
;//supported
131 // possible conversions:
136 // "converted" using pointer/stride modification
137 if(avctx
->pix_fmt
==PIX_FMT_YUV420P
) return CONTROL_TRUE
;// u/v swap
138 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& !ctx
->do_dr1
) return CONTROL_TRUE
;// half stride
141 case IMGFMT_XVMC_IDCT_MPEG2
:
142 case IMGFMT_XVMC_MOCO_MPEG2
:
143 if(avctx
->pix_fmt
==PIX_FMT_XVMC_MPEG2_IDCT
) return CONTROL_TRUE
;
146 return CONTROL_FALSE
;
149 case VDCTRL_RESYNC_STREAM
:
150 avcodec_flush_buffers(avctx
);
152 case VDCTRL_QUERY_UNSEEN_FRAMES
:
153 return avctx
->has_b_frames
+ 10;
155 return CONTROL_UNKNOWN
;
158 void mp_msp_av_log_callback(void* ptr
, int level
, const char* fmt
, va_list vl
)
160 static int print_prefix
=1;
161 AVClass
* avc
= ptr
? *(AVClass
**)ptr
: NULL
;
162 int type
= MSGT_FIXME
;
167 case AV_LOG_DEBUG
: mp_level
= MSGL_V
; break;
168 case AV_LOG_INFO
: mp_level
= MSGL_INFO
; break;
169 case AV_LOG_ERROR
: mp_level
= MSGL_ERR
; break;
170 default : mp_level
= MSGL_ERR
; break;
173 if (!mp_msg_test(type
, mp_level
)) return;
176 if(!strcmp(avc
->class_name
, "AVCodecContext")){
177 AVCodecContext
* s
= ptr
;
179 if(s
->codec
->type
== CODEC_TYPE_AUDIO
){
182 }else if(s
->codec
->type
== CODEC_TYPE_VIDEO
){
186 //FIXME subtitles, encoders (what msgt for them? there is no appropriate ...)
188 }else if(!strcmp(avc
->class_name
, "AVFormatContext")){
189 #if 0 //needs libavformat include FIXME iam too lazy to do this cleanly,probably the whole should be moved out of this file ...
190 AVFormatContext
* s
= ptr
;
199 if(print_prefix
&& avc
) {
200 mp_msg(type
, mp_level
, "[%s @ %p]", avc
->item_name(ptr
), avc
);
203 print_prefix
= strchr(fmt
, '\n') != NULL
;
204 vsnprintf(buf
, sizeof(buf
), fmt
, vl
);
205 mp_msg(type
, mp_level
, buf
);
209 static int init(sh_video_t
*sh
){
210 AVCodecContext
*avctx
;
214 int do_vis_debug
= lavc_param_vismv
|| (lavc_param_debug
&(FF_DEBUG_VIS_MB_TYPE
|FF_DEBUG_VIS_QP
));
216 if(!avcodec_initialized
){
218 avcodec_register_all();
219 avcodec_initialized
=1;
220 av_log_set_callback(mp_msp_av_log_callback
);
223 ctx
= sh
->context
= malloc(sizeof(vd_ffmpeg_ctx
));
226 memset(ctx
, 0, sizeof(vd_ffmpeg_ctx
));
228 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh
->codec
->dll
);
230 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
,MSGTR_MissingLAVCcodec
,sh
->codec
->dll
);
235 if(vd_use_slices
&& (lavc_codec
->capabilities
&CODEC_CAP_DRAW_HORIZ_BAND
) && !do_vis_debug
)
238 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
)
240 ctx
->b_age
= ctx
->ip_age
[0]= ctx
->ip_age
[1]= 256*256*256*64;
241 ctx
->ip_count
= ctx
->b_count
= 0;
243 ctx
->pic
= avcodec_alloc_frame();
244 ctx
->avctx
= avcodec_alloc_context();
249 if(lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
){
250 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_XVMCAcceleratedCodec
);
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
;
260 #endif /* CONFIG_XVMC */
262 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
263 avctx
->get_buffer
= get_buffer
;
264 avctx
->release_buffer
= release_buffer
;
265 avctx
->reget_buffer
= get_buffer
;
268 avctx
->flags
|= CODEC_FLAG_NOT_TRUNCATED
;
269 avctx
->flags
|= lavc_param_bitexact
;
271 avctx
->width
= sh
->disp_w
;
272 avctx
->height
= sh
->disp_h
;
273 avctx
->workaround_bugs
= lavc_param_workaround_bugs
;
274 avctx
->error_recognition
= lavc_param_error_resilience
;
275 if(lavc_param_gray
) avctx
->flags
|= CODEC_FLAG_GRAY
;
276 avctx
->flags2
|= lavc_param_fast
;
277 avctx
->codec_tag
= sh
->format
;
278 avctx
->stream_codec_tag
= sh
->video
.fccHandler
;
279 avctx
->idct_algo
= lavc_param_idct_algo
;
280 avctx
->error_concealment
= lavc_param_error_concealment
;
281 avctx
->debug
= lavc_param_debug
;
282 if (lavc_param_debug
)
283 av_log_set_level(AV_LOG_DEBUG
);
284 avctx
->debug_mv
= lavc_param_vismv
;
285 avctx
->skip_top
= lavc_param_skip_top
;
286 avctx
->skip_bottom
= lavc_param_skip_bottom
;
287 if(lavc_param_lowres_str
!= NULL
)
289 sscanf(lavc_param_lowres_str
, "%d,%d", &lavc_param_lowres
, &lowres_w
);
290 if(lavc_param_lowres
< 1 || lavc_param_lowres
> 16 || (lowres_w
> 0 && avctx
->width
< lowres_w
))
291 lavc_param_lowres
= 0;
292 avctx
->lowres
= lavc_param_lowres
;
294 avctx
->skip_loop_filter
= str2AVDiscard(lavc_param_skip_loop_filter_str
);
295 avctx
->skip_idct
= str2AVDiscard(lavc_param_skip_idct_str
);
296 avctx
->skip_frame
= str2AVDiscard(lavc_param_skip_frame_str
);
299 if(parse_avopts(avctx
, lavc_avopt
) < 0){
300 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavc_avopt
);
306 mp_dbg(MSGT_DECVIDEO
,MSGL_DBG2
,"libavcodec.size: %d x %d\n",avctx
->width
,avctx
->height
);
307 switch (sh
->format
) {
308 case mmioFOURCC('S','V','Q','3'):
309 /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or
310 in the phony AVI header if demux_lavf is used. The first case is
311 handled here; the second case falls through to the next section. */
313 avctx
->extradata_size
= (*(int*)sh
->ImageDesc
) - sizeof(int);
314 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
315 memcpy(avctx
->extradata
, ((int*)sh
->ImageDesc
)+1, avctx
->extradata_size
);
320 case mmioFOURCC('A','V','R','n'):
321 case mmioFOURCC('M','J','P','G'):
322 /* AVRn stores huffman table in AVI header */
323 /* Pegasus MJPEG stores it also in AVI header, but it uses the common
325 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
327 avctx
->flags
|= CODEC_FLAG_EXTERN_HUFF
;
328 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
329 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
330 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
335 uint8_t *p
= avctx
->extradata
;
337 for (x
=0; x
<avctx
->extradata_size
; x
++)
338 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,"[%x] ", p
[x
]);
339 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,"\n");
344 case mmioFOURCC('R', 'V', '1', '0'):
345 case mmioFOURCC('R', 'V', '1', '3'):
346 case mmioFOURCC('R', 'V', '2', '0'):
347 case mmioFOURCC('R', 'V', '3', '0'):
348 case mmioFOURCC('R', 'V', '4', '0'):
349 if(sh
->bih
->biSize
<sizeof(*sh
->bih
)+8){
350 /* only 1 packet per frame & sub_id from fourcc */
351 avctx
->extradata_size
= 8;
352 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
353 ((uint32_t*)avctx
->extradata
)[0] = 0;
354 ((uint32_t*)avctx
->extradata
)[1] =
355 (sh
->format
== mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
357 /* has extra slice header (demux_rm or rm->avi streamcopy) */
358 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
359 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
360 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
362 avctx
->sub_id
= AV_RB32(avctx
->extradata
+4);
364 // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]);
368 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
370 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
371 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
372 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
375 /* Pass palette to codec */
376 if (sh
->bih
&& (sh
->bih
->biBitCount
<= 8)) {
377 avctx
->palctrl
= calloc(1,sizeof(AVPaletteControl
));
378 avctx
->palctrl
->palette_changed
= 1;
379 if (sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
))
380 /* Palette size in biSize */
381 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
382 FFMIN(sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
), AVPALETTE_SIZE
));
384 /* Palette size in biClrUsed */
385 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
386 FFMIN(sh
->bih
->biClrUsed
* 4, AVPALETTE_SIZE
));
390 avctx
->bits_per_coded_sample
= sh
->bih
->biBitCount
;
392 if(lavc_param_threads
> 1)
393 avcodec_thread_init(avctx
, lavc_param_threads
);
395 if (avcodec_open(avctx
, lavc_codec
) < 0) {
396 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
, MSGTR_CantOpenCodec
);
400 mp_msg(MSGT_DECVIDEO
,MSGL_V
,"INFO: libavcodec init OK!\n");
401 return 1; //mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12);
405 static void uninit(sh_video_t
*sh
){
406 vd_ffmpeg_ctx
*ctx
= sh
->context
;
407 AVCodecContext
*avctx
= ctx
->avctx
;
409 if(lavc_param_vstats
){
412 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,"QP: %d, count: %d\n", i
, ctx
->qp_stat
[i
]);
414 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,MSGTR_MPCODECS_ArithmeticMeanOfQP
,
415 ctx
->qp_sum
/ avctx
->coded_frame
->coded_picture_number
,
416 1.0/(ctx
->inv_qp_sum
/ avctx
->coded_frame
->coded_picture_number
)
421 if (avctx
->codec
&& avcodec_close(avctx
) < 0)
422 mp_msg(MSGT_DECVIDEO
,MSGL_ERR
, MSGTR_CantCloseCodec
);
424 av_freep(&avctx
->extradata
);
425 av_freep(&avctx
->palctrl
);
426 av_freep(&avctx
->slice_offset
);
435 static void draw_slice(struct AVCodecContext
*s
,
436 AVFrame
*src
, int offset
[4],
437 int y
, int type
, int height
){
438 sh_video_t
* sh
= s
->opaque
;
439 uint8_t *source
[3]= {src
->data
[0] + offset
[0], src
->data
[1] + offset
[1], src
->data
[2] + offset
[2]};
443 int skip_stride
= ((width
<<lavc_param_lowres
)+15)>>4;
444 uint8_t *skip
= &s
->coded_frame
->mbskip_table
[(y
>>4)*skip_stride
];
445 int threshold
= s
->coded_frame
->age
;
446 if(s
->pict_type
!=B_TYPE
){
447 for(i
=0; i
*16<width
+16; i
++){
448 if(i
*16>=width
|| skip
[i
]>=threshold
){
449 if(start
==i
) start
++;
451 uint8_t *src2
[3]= {src
[0] + start
*16,
454 //printf("%2d-%2d x %d\n", start, i, y);
455 mpcodecs_draw_slice (sh
,src2
, stride
, (i
-start
)*16, height
, start
*16, y
);
462 if (y
< sh
->disp_h
) {
463 mpcodecs_draw_slice (sh
, source
, src
->linesize
, sh
->disp_w
, (y
+height
)<=sh
->disp_h
?height
:sh
->disp_h
-y
, 0, y
);
468 static int init_vo(sh_video_t
*sh
, enum PixelFormat pix_fmt
){
469 vd_ffmpeg_ctx
*ctx
= sh
->context
;
470 AVCodecContext
*avctx
= ctx
->avctx
;
471 float aspect
= av_q2d(avctx
->sample_aspect_ratio
) * avctx
->width
/ avctx
->height
;
474 width
= avctx
->width
;
475 height
= avctx
->height
;
478 // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
479 // use dimensions from BIH to avoid black borders at the right and bottom.
480 if (sh
->bih
&& sh
->ImageDesc
) {
481 width
= sh
->bih
->biWidth
>>lavc_param_lowres
;
482 height
= sh
->bih
->biHeight
>>lavc_param_lowres
;
485 // it is possible another vo buffers to be used after vo config()
486 // lavc reset its buffers on width/heigh change but not on aspect change!!!
487 if (av_cmp_q(avctx
->sample_aspect_ratio
, ctx
->last_sample_aspect_ratio
) ||
488 width
!= sh
->disp_w
||
489 height
!= sh
->disp_h
||
490 pix_fmt
!= ctx
->pix_fmt
||
491 !ctx
->vo_initialized
)
493 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[ffmpeg] aspect_ratio: %f\n", aspect
);
494 if (sh
->aspect
== 0 ||
495 av_cmp_q(avctx
->sample_aspect_ratio
,
496 ctx
->last_sample_aspect_ratio
))
498 ctx
->last_sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
501 ctx
->pix_fmt
= pix_fmt
;
503 // YUVJ are YUV formats that use the full Y range and not just
504 // 16 - 235 (see colorspaces.txt).
505 // Currently they are all treated the same way.
506 case PIX_FMT_YUV410P
: ctx
->best_csp
=IMGFMT_YVU9
;break; //svq1
507 case PIX_FMT_YUVJ420P
:
508 case PIX_FMT_YUV420P
: ctx
->best_csp
=IMGFMT_YV12
;break; //mpegs
509 case PIX_FMT_YUVJ422P
:
510 case PIX_FMT_YUV422P
: ctx
->best_csp
=IMGFMT_422P
;break; //mjpeg / huffyuv
511 case PIX_FMT_YUVJ444P
:
512 case PIX_FMT_YUV444P
: ctx
->best_csp
=IMGFMT_444P
;break; //photo jpeg
513 case PIX_FMT_YUV411P
: ctx
->best_csp
=IMGFMT_411P
;break; //dv ntsc
514 case PIX_FMT_YUYV422
: ctx
->best_csp
=IMGFMT_YUY2
;break; //huffyuv perhaps in the future
515 case PIX_FMT_RGB24
: ctx
->best_csp
=IMGFMT_RGB24
;break; //qtrle
516 case PIX_FMT_RGB32
: ctx
->best_csp
=IMGFMT_BGR32
;break; //huffyuv / mjpeg
517 case PIX_FMT_BGR24
: ctx
->best_csp
=IMGFMT_BGR24
;break; //8bps
518 case PIX_FMT_RGB555
: ctx
->best_csp
=IMGFMT_BGR15
;break; //rpza,cram
519 case PIX_FMT_RGB565
: ctx
->best_csp
=IMGFMT_BGR16
;break; //4xm
520 case PIX_FMT_GRAY8
: ctx
->best_csp
=IMGFMT_Y800
;break; // gray jpeg
521 case PIX_FMT_PAL8
: ctx
->best_csp
=IMGFMT_BGR8
;break; //8bps,mrle,cram
523 case PIX_FMT_XVMC_MPEG2_MC
:ctx
->best_csp
=IMGFMT_XVMC_MOCO_MPEG2
;break;
524 case PIX_FMT_XVMC_MPEG2_IDCT
:ctx
->best_csp
=IMGFMT_XVMC_IDCT_MPEG2
;break;
529 if (!mpcodecs_config_vo(sh
,sh
->disp_w
,sh
->disp_h
, ctx
->best_csp
))
531 ctx
->vo_initialized
= 1;
536 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
537 sh_video_t
* sh
= avctx
->opaque
;
538 vd_ffmpeg_ctx
*ctx
= sh
->context
;
539 mp_image_t
* mpi
=NULL
;
540 int flags
= MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
;
541 int type
= MP_IMGTYPE_IPB
;
542 int width
= avctx
->width
;
543 int height
= avctx
->height
;
545 //printf("get_buffer %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
546 if(avctx
->pix_fmt
== PIX_FMT_YUV410P
)
547 align
=63; //yes seriously, its really needed (16x16 chroma blocks in SVQ1 -> 64x64)
549 if (pic
->buffer_hints
) {
550 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, "Buffer hints: %u\n", pic
->buffer_hints
);
551 type
= MP_IMGTYPE_TEMP
;
552 if (pic
->buffer_hints
& FF_BUFFER_HINTS_READABLE
)
553 flags
|= MP_IMGFLAG_READABLE
;
554 if (pic
->buffer_hints
& FF_BUFFER_HINTS_PRESERVE
) {
555 type
= MP_IMGTYPE_STATIC
;
556 flags
|= MP_IMGFLAG_PRESERVE
;
558 if (pic
->buffer_hints
& FF_BUFFER_HINTS_REUSABLE
) {
559 type
= MP_IMGTYPE_STATIC
;
560 flags
|= MP_IMGFLAG_PRESERVE
;
562 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
563 MP_IMGFLAG_DRAW_CALLBACK
:0;
564 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, type
== MP_IMGTYPE_STATIC
? "using STATIC\n" : "using TEMP\n");
568 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
569 MP_IMGFLAG_DRAW_CALLBACK
:0;
572 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
573 | (ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0);
577 if(init_vo(sh
,avctx
->pix_fmt
) < 0){
578 avctx
->release_buffer
= avcodec_default_release_buffer
;
579 avctx
->get_buffer
= avcodec_default_get_buffer
;
580 return avctx
->get_buffer(avctx
, pic
);
583 if (!pic
->buffer_hints
) {
584 if(ctx
->b_count
>1 || ctx
->ip_count
>2){
585 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_DRIFailure
);
587 ctx
->do_dr1
=0; //FIXME
588 avctx
->get_buffer
= avcodec_default_get_buffer
;
589 return avctx
->get_buffer(avctx
, pic
);
592 if(avctx
->has_b_frames
){
593 type
= MP_IMGTYPE_IPB
;
597 mp_msg(MSGT_DECVIDEO
,MSGL_DBG2
, type
== MP_IMGTYPE_IPB
? "using IPB\n" : "using IP\n");
600 mpi
= mpcodecs_get_image(sh
,type
, flags
,
601 (width
+align
)&(~align
), (height
+align
)&(~align
));
603 // ok, let's see what did we get:
604 if( mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&&
605 !(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
606 // nice, filter/vo likes draw_callback :)
607 avctx
->draw_horiz_band
= draw_slice
;
609 avctx
->draw_horiz_band
= NULL
;
611 // Palette support: libavcodec copies palette to *data[1]
613 mpi
->planes
[1] = av_malloc(AVPALETTE_SIZE
);
615 pic
->data
[0]= mpi
->planes
[0];
616 pic
->data
[1]= mpi
->planes
[1];
617 pic
->data
[2]= mpi
->planes
[2];
620 assert(mpi
->width
>= ((width
+align
)&(~align
)));
621 assert(mpi
->height
>= ((height
+align
)&(~align
)));
622 assert(mpi
->stride
[0] >= mpi
->width
);
623 if(mpi
->imgfmt
==IMGFMT_I420
|| mpi
->imgfmt
==IMGFMT_YV12
|| mpi
->imgfmt
==IMGFMT_IYUV
){
624 const int y_size
= mpi
->stride
[0] * (mpi
->h
-1) + mpi
->w
;
625 const int c_size
= mpi
->stride
[1] * ((mpi
->h
>>1)-1) + (mpi
->w
>>1);
627 assert(mpi
->planes
[0] > mpi
->planes
[1] || mpi
->planes
[0] + y_size
<= mpi
->planes
[1]);
628 assert(mpi
->planes
[0] > mpi
->planes
[2] || mpi
->planes
[0] + y_size
<= mpi
->planes
[2]);
629 assert(mpi
->planes
[1] > mpi
->planes
[0] || mpi
->planes
[1] + c_size
<= mpi
->planes
[0]);
630 assert(mpi
->planes
[1] > mpi
->planes
[2] || mpi
->planes
[1] + c_size
<= mpi
->planes
[2]);
631 assert(mpi
->planes
[2] > mpi
->planes
[0] || mpi
->planes
[2] + c_size
<= mpi
->planes
[0]);
632 assert(mpi
->planes
[2] > mpi
->planes
[1] || mpi
->planes
[2] + c_size
<= mpi
->planes
[1]);
636 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
637 * lavc will check that and die with an error message, if its not true
639 pic
->linesize
[0]= mpi
->stride
[0];
640 pic
->linesize
[1]= mpi
->stride
[1];
641 pic
->linesize
[2]= mpi
->stride
[2];
644 //printf("%X\n", (int)mpi->planes[0]);
646 if(mpi
->flags
&MP_IMGFLAG_DIRECT
)
648 else if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)
654 pic
->age
= ctx
->ip_age
[0];
656 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
660 pic
->age
= ctx
->b_age
;
666 pic
->type
= FF_BUFFER_TYPE_USER
;
670 static void release_buffer(struct AVCodecContext
*avctx
, AVFrame
*pic
){
671 mp_image_t
* mpi
= pic
->opaque
;
672 sh_video_t
* sh
= avctx
->opaque
;
673 vd_ffmpeg_ctx
*ctx
= sh
->context
;
676 //printf("release buffer %d %d %d\n", mpi ? mpi->flags&MP_IMGFLAG_PRESERVE : -99, ctx->ip_count, ctx->b_count);
678 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
679 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
685 // Palette support: free palette buffer allocated in get_buffer
686 if ( mpi
&& (mpi
->bpp
== 8))
687 av_freep(&mpi
->planes
[1]);
689 if(pic
->type
!=FF_BUFFER_TYPE_USER
){
690 avcodec_default_release_buffer(avctx
, pic
);
697 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
700 // copypaste from demux_real.c - it should match to get it working!
701 //FIXME put into some header
702 typedef struct dp_hdr_s
{
703 uint32_t chunks
; // number of chunks
704 uint32_t timestamp
; // timestamp from packet header
705 uint32_t len
; // length of actual data
706 uint32_t chunktab
; // offset to chunk offset array
709 void swap_palette(void *pal
) {
712 for (i
= 0; i
< AVPALETTE_COUNT
; i
++)
713 p
[i
] = le2me_32(p
[i
]);
717 static mp_image_t
* decode(sh_video_t
*sh
,void* data
,int len
,int flags
){
720 vd_ffmpeg_ctx
*ctx
= sh
->context
;
721 AVFrame
*pic
= ctx
->pic
;
722 AVCodecContext
*avctx
= ctx
->avctx
;
723 mp_image_t
* mpi
=NULL
;
724 int dr1
= ctx
->do_dr1
;
726 if(len
<=0) return NULL
; // skipped frame
728 //ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
730 avctx
->draw_horiz_band
=NULL
;
732 if(ctx
->vo_initialized
&& !(flags
&3) && !dr1
){
733 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
|
734 (ctx
->do_slices
?MP_IMGFLAG_DRAW_CALLBACK
:0),
735 sh
->disp_w
, sh
->disp_h
);
736 if(mpi
&& mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
){
737 // vd core likes slices!
738 avctx
->draw_horiz_band
=draw_slice
;
742 avctx
->hurry_up
=(flags
&3)?((flags
&2)?2:1):0;
744 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
745 ((int *)data
)[0], ((int *)data
)[1], ((int *)data
)[2], ((int *)data
)[3]);
746 ret
= avcodec_decode_video(avctx
, pic
,
747 &got_picture
, data
, len
);
750 if(ret
<0) mp_msg(MSGT_DECVIDEO
,MSGL_WARN
, "Error while decoding frame!\n");
751 //printf("repeat: %d\n", pic->repeat_pict);
752 //-- vstats generation
753 while(lavc_param_vstats
){ // always one time loop
754 static FILE *fvstats
=NULL
;
756 static long long int all_len
=0;
757 static int frame_number
=0;
758 static double all_frametime
=0.0;
759 AVFrame
*pic
= avctx
->coded_frame
;
766 today
= localtime(&today2
);
767 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
768 today
->tm_min
, today
->tm_sec
);
769 fvstats
= fopen(filename
,"w");
772 lavc_param_vstats
=0; // disable block
778 // average MB quantizer
781 int w
= ((avctx
->width
<< lavc_param_lowres
)+15) >> 4;
782 int h
= ((avctx
->height
<< lavc_param_lowres
)+15) >> 4;
783 int8_t *q
= pic
->qscale_table
;
784 for( y
= 0; y
< h
; y
++ ) {
785 for( x
= 0; x
< w
; x
++ )
786 quality
+= (double)*(q
+x
);
793 all_frametime
+=sh
->frametime
;
794 fprintf(fvstats
, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
795 ++frame_number
, quality
, len
, (double)all_len
/1024);
796 fprintf(fvstats
, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
797 all_frametime
, (double)(len
*8)/sh
->frametime
/1000.0,
798 (double)(all_len
*8)/all_frametime
/1000.0);
799 switch(pic
->pict_type
){
801 fprintf(fvstats
, "type= I\n");
804 fprintf(fvstats
, "type= P\n");
807 fprintf(fvstats
, "type= S\n");
810 fprintf(fvstats
, "type= B\n");
813 fprintf(fvstats
, "type= ? (%d)\n", pic
->pict_type
);
817 ctx
->qp_stat
[(int)(quality
+0.5)]++;
818 ctx
->qp_sum
+= quality
;
819 ctx
->inv_qp_sum
+= 1.0/(double)quality
;
825 if(!got_picture
) return NULL
; // skipped image
827 if(init_vo(sh
,avctx
->pix_fmt
) < 0) return NULL
;
829 if(dr1
&& pic
->opaque
){
830 mpi
= (mp_image_t
*)pic
->opaque
;
834 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
,
835 avctx
->width
, avctx
->height
);
836 if(!mpi
){ // temporary!
837 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_CouldntAllocateImageForCodec
);
842 mpi
->planes
[0]=pic
->data
[0];
843 mpi
->planes
[1]=pic
->data
[1];
844 mpi
->planes
[2]=pic
->data
[2];
845 mpi
->stride
[0]=pic
->linesize
[0];
846 mpi
->stride
[1]=pic
->linesize
[1];
847 mpi
->stride
[2]=pic
->linesize
[2];
853 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& mpi
->chroma_y_shift
==1){
854 // we have 422p but user wants 420p
859 #ifdef WORDS_BIGENDIAN
860 // FIXME: this might cause problems for buffers with FF_BUFFER_HINTS_PRESERVE
862 swap_palette(mpi
->planes
[1]);
864 /* to comfirm with newer lavc style */
865 mpi
->qscale
=pic
->qscale_table
;
866 mpi
->qstride
=pic
->qstride
;
867 mpi
->pict_type
=pic
->pict_type
;
868 mpi
->qscale_type
= pic
->qscale_type
;
869 mpi
->fields
= MP_IMGFIELD_ORDERED
;
870 if(pic
->interlaced_frame
) mpi
->fields
|= MP_IMGFIELD_INTERLACED
;
871 if(pic
->top_field_first
) mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
872 if(pic
->repeat_pict
== 1) mpi
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
878 static enum PixelFormat
get_format(struct AVCodecContext
* avctx
,
879 const enum PixelFormat
* fmt
){
880 sh_video_t
* sh
= avctx
->opaque
;
883 if(avctx
->xvmc_acceleration
){
884 vd_ffmpeg_ctx
*ctx
= sh
->context
;
885 avctx
->get_buffer
= mc_get_buffer
;
886 avctx
->release_buffer
= mc_release_buffer
;
887 avctx
->draw_horiz_band
= mc_render_slice
;
888 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_XVMCAcceleratedMPEG2
);
889 assert(ctx
->do_dr1
);//these are must to!
890 assert(ctx
->do_slices
); //it is (vo_)ffmpeg bug if this fails
891 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;//do i need that??!!
892 avctx
->slice_flags
=SLICE_FLAG_CODED_ORDER
|SLICE_FLAG_ALLOW_FIELD
;
894 for(i
=0;fmt
[i
]!=-1;i
++){
895 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_TryingPixfmt
,i
);
896 if( init_vo(sh
,fmt
[i
]) >= 0)
902 static int mc_get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
903 sh_video_t
* sh
= avctx
->opaque
;
904 vd_ffmpeg_ctx
*ctx
= sh
->context
;
905 mp_image_t
* mpi
=NULL
;
906 struct xvmc_render_state
* render
;
907 int flags
= MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
|
908 MP_IMGFLAG_DRAW_CALLBACK
;
910 // printf("vd_ffmpeg::mc_get_buffer (xvmc) %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
911 if(!avctx
->xvmc_acceleration
){
912 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, MSGTR_MPCODECS_McGetBufferShouldWorkOnlyWithXVMC
);
915 // return -1;//!!fixme check error conditions
917 assert(avctx
->draw_horiz_band
== mc_render_slice
);
918 assert(avctx
->release_buffer
== mc_release_buffer
);
919 if( mp_msg_test(MSGT_DECVIDEO
,MSGL_DBG5
) )
920 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::mc_get_buffer\n");
922 if(init_vo(sh
,avctx
->pix_fmt
) < 0){
923 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, MSGTR_MPCODECS_UnexpectedInitVoError
);
925 // return -1;//!!fixme check error conditions
934 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
;
937 mpi
= mpcodecs_get_image(sh
, MP_IMGTYPE_IPB
,flags
,
938 avctx
->width
, avctx
->height
);
940 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MPCODECS_UnrecoverableErrorRenderBuffersNotTaken
);
943 // return -1;//!!fixme check error conditions in ffmpeg
946 if( (mpi
->flags
& MP_IMGFLAG_DIRECT
) == 0){
947 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, MSGTR_MPCODECS_OnlyBuffersAllocatedByVoXvmcAllowed
);
950 // return -1;//!!fixme check error conditions in ffmpeg
953 pic
->data
[0]= mpi
->planes
[0];
954 pic
->data
[1]= mpi
->planes
[1];
955 pic
->data
[2]= mpi
->planes
[2];
958 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
959 * lavc will check that and die with an error message, if its not true
961 pic
->linesize
[0]= mpi
->stride
[0];
962 pic
->linesize
[1]= mpi
->stride
[1];
963 pic
->linesize
[2]= mpi
->stride
[2];
969 pic
->age
= ctx
->ip_age
[0];
971 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
976 pic
->age
= ctx
->b_age
;
983 pic
->type
= FF_BUFFER_TYPE_USER
;
985 render
=(struct xvmc_render_state
*)mpi
->priv
;//same as data[2]
986 if( mp_msg_test(MSGT_DECVIDEO
,MSGL_DBG5
) )
987 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::mc_get_buffer (render=%p)\n",render
);
989 assert(render
->magic
== MP_XVMC_RENDER_MAGIC
);
990 render
->state
|= MP_XVMC_STATE_PREDICTION
;
995 static void mc_release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
996 mp_image_t
* mpi
= pic
->opaque
;
997 sh_video_t
* sh
= avctx
->opaque
;
998 vd_ffmpeg_ctx
*ctx
= sh
->context
;
999 struct xvmc_render_state
* render
;
1003 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
1004 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
1010 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
1011 //mark the surface as not requared for prediction
1012 render
=(struct xvmc_render_state
*)pic
->data
[2];//same as mpi->priv
1013 if( mp_msg_test(MSGT_DECVIDEO
,MSGL_DBG5
) )
1014 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::mc_release_buffer (render=%p)\n",render
);
1015 assert(render
!=NULL
);
1016 assert(render
->magic
==MP_XVMC_RENDER_MAGIC
);
1017 render
->state
&=~MP_XVMC_STATE_PREDICTION
;
1023 static void mc_render_slice(struct AVCodecContext
*s
,
1024 AVFrame
*src
, int offset
[4],
1025 int y
, int type
, int height
){
1026 int width
= s
->width
;
1027 sh_video_t
* sh
= s
->opaque
;
1028 uint8_t *source
[3]= {src
->data
[0], src
->data
[1], src
->data
[2]};
1030 assert(src
->linesize
[0]==0 && src
->linesize
[1]==0 && src
->linesize
[2]==0);
1031 assert(offset
[0]==0 && offset
[1]==0 && offset
[2]==0);
1033 mpcodecs_draw_slice (sh
, source
, src
->linesize
, width
, height
, 0, y
);
1037 #endif /* CONFIG_XVMC */