2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "libavutil/common.h"
32 #include "ffmpeg_files/intreadwrite.h"
34 #include "fmt-conversion.h"
37 #include "img_format.h"
38 #include "libmpdemux/stheader.h"
39 #include "codec-cfg.h"
41 static const vd_info_t info
= {
42 "FFmpeg's libavcodec codec family",
45 "A'rpi, Michael, Alex",
49 #include "libavcodec/avcodec.h"
51 #if AVPALETTE_SIZE > 1024
52 #error palette too large, adapt libmpcodecs/vf.c:vf_get_image
56 #include "libavcodec/xvmc.h"
59 int avcodec_initialized
=0;
62 AVCodecContext
*avctx
;
64 enum PixelFormat pix_fmt
;
76 AVRational last_sample_aspect_ratio
;
82 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
83 static void release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
84 static void draw_slice(struct AVCodecContext
*s
, const AVFrame
*src
,
85 int offset
[4], int y
, int type
, int height
);
87 static enum PixelFormat
get_format(struct AVCodecContext
*avctx
,
88 const enum PixelFormat
*pix_fmt
);
89 static void uninit(struct sh_video
*sh
);
91 const m_option_t lavc_decode_opts_conf
[]={
92 OPT_INTRANGE("bug", lavc_param
.workaround_bugs
, 0, -1, 999999),
93 OPT_INTRANGE("er", lavc_param
.error_resilience
, 0, 0, 99),
94 OPT_FLAG_ON("gray", lavc_param
.gray
, 0),
95 OPT_INTRANGE("idct", lavc_param
.idct_algo
, 0, 0, 99),
96 OPT_INTRANGE("ec", lavc_param
.error_concealment
, 0, 0, 99),
97 OPT_FLAG_ON("vstats", lavc_param
.vstats
, 0),
98 OPT_INTRANGE("debug", lavc_param
.debug
, 0, 0, 9999999),
99 OPT_INTRANGE("vismv", lavc_param
.vismv
, 0, 0, 9999999),
100 OPT_INTRANGE("st", lavc_param
.skip_top
, 0, 0, 999),
101 OPT_INTRANGE("sb", lavc_param
.skip_bottom
, 0, 0, 999),
102 OPT_FLAG_CONSTANTS("fast", lavc_param
.fast
, 0, 0, CODEC_FLAG2_FAST
),
103 OPT_STRING("lowres", lavc_param
.lowres_str
, 0),
104 OPT_STRING("skiploopfilter", lavc_param
.skip_loop_filter_str
, 0),
105 OPT_STRING("skipidct", lavc_param
.skip_idct_str
, 0),
106 OPT_STRING("skipframe", lavc_param
.skip_frame_str
, 0),
107 OPT_INTRANGE("threads", lavc_param
.threads
, 0, 1, 8),
108 OPT_FLAG_CONSTANTS("bitexact", lavc_param
.bitexact
, 0, 0, CODEC_FLAG_BITEXACT
),
109 OPT_STRING("o", lavc_param
.avopt
, 0),
110 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
113 static enum AVDiscard
str2AVDiscard(char *str
) {
114 if (!str
) return AVDISCARD_DEFAULT
;
115 if (strcasecmp(str
, "none" ) == 0) return AVDISCARD_NONE
;
116 if (strcasecmp(str
, "default") == 0) return AVDISCARD_DEFAULT
;
117 if (strcasecmp(str
, "nonref" ) == 0) return AVDISCARD_NONREF
;
118 if (strcasecmp(str
, "bidir" ) == 0) return AVDISCARD_BIDIR
;
119 if (strcasecmp(str
, "nonkey" ) == 0) return AVDISCARD_NONKEY
;
120 if (strcasecmp(str
, "all" ) == 0) return AVDISCARD_ALL
;
121 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Unknown discard value %s\n", str
);
122 return AVDISCARD_DEFAULT
;
125 // to set/get/query special features/parameters
126 static int control(sh_video_t
*sh
, int cmd
, void *arg
, ...){
127 vd_ffmpeg_ctx
*ctx
= sh
->context
;
128 AVCodecContext
*avctx
= ctx
->avctx
;
130 case VDCTRL_QUERY_FORMAT
:
132 int format
=(*((int *)arg
));
133 if(format
== ctx
->best_csp
) return CONTROL_TRUE
;//supported
134 // possible conversions:
139 // "converted" using pointer/stride modification
140 if(avctx
->pix_fmt
==PIX_FMT_YUV420P
) return CONTROL_TRUE
;// u/v swap
141 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& !ctx
->do_dr1
) return CONTROL_TRUE
;// half stride
144 case IMGFMT_XVMC_IDCT_MPEG2
:
145 case IMGFMT_XVMC_MOCO_MPEG2
:
146 if(avctx
->pix_fmt
==PIX_FMT_XVMC_MPEG2_IDCT
) return CONTROL_TRUE
;
149 return CONTROL_FALSE
;
151 case VDCTRL_RESYNC_STREAM
:
152 avcodec_flush_buffers(avctx
);
154 case VDCTRL_QUERY_UNSEEN_FRAMES
:;
155 int delay
= avctx
->has_b_frames
;
156 #if defined(FF_THREAD_FRAME) && LIBAVCODEC_VERSION_MAJOR <= 52 && LIBAVCODEC_VERSION_MINOR < 49
157 // FFmpeg-mt has extra delay when using frame threading
158 // In newer versions that is included in has_b_frames
159 if (avctx
->thread_type
& FF_THREAD_FRAME
)
160 delay
+= avctx
->thread_count
- 1;
164 return CONTROL_UNKNOWN
;
168 static int init(sh_video_t
*sh
){
169 struct lavc_param
*lavc_param
= &sh
->opts
->lavc_param
;
170 AVCodecContext
*avctx
;
174 int do_vis_debug
= lavc_param
->vismv
|| (lavc_param
->debug
&(FF_DEBUG_VIS_MB_TYPE
|FF_DEBUG_VIS_QP
));
176 if(!avcodec_initialized
){
178 avcodec_register_all();
179 avcodec_initialized
=1;
182 ctx
= sh
->context
= malloc(sizeof(vd_ffmpeg_ctx
));
185 memset(ctx
, 0, sizeof(vd_ffmpeg_ctx
));
187 lavc_codec
= (AVCodec
*)avcodec_find_decoder_by_name(sh
->codec
->dll
);
189 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Cannot find codec '%s' in libavcodec...\n", sh
->codec
->dll
);
194 if(sh
->opts
->vd_use_slices
&& (lavc_codec
->capabilities
&CODEC_CAP_DRAW_HORIZ_BAND
) && !do_vis_debug
)
197 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
)
199 ctx
->b_age
= ctx
->ip_age
[0]= ctx
->ip_age
[1]= 256*256*256*64;
200 ctx
->ip_count
= ctx
->b_count
= 0;
202 ctx
->pic
= avcodec_alloc_frame();
203 ctx
->avctx
= avcodec_alloc_context();
206 avctx
->codec_type
= CODEC_TYPE_VIDEO
;
207 avctx
->codec_id
= lavc_codec
->id
;
209 if (lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
// XvMC
210 || lavc_codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
) {
212 ctx
->do_slices
= true;
213 lavc_param
->threads
= 1;
214 avctx
->get_format
= get_format
;
215 avctx
->get_buffer
= get_buffer
;
216 avctx
->release_buffer
= release_buffer
;
217 avctx
->reget_buffer
= get_buffer
;
218 avctx
->draw_horiz_band
= draw_slice
;
219 if (lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
)
220 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[VD_FFMPEG] XVMC-accelerated "
222 if (lavc_codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
)
223 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[VD_FFMPEG] VDPAU hardware "
225 avctx
->slice_flags
= SLICE_FLAG_CODED_ORDER
|SLICE_FLAG_ALLOW_FIELD
;
228 /* Our get_buffer and draw_horiz_band callbacks are not safe to call
229 * from other threads. */
230 if (lavc_param
->threads
> 1) {
232 ctx
->do_slices
= false;
236 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
237 avctx
->get_buffer
= get_buffer
;
238 avctx
->release_buffer
= release_buffer
;
239 avctx
->reget_buffer
= get_buffer
;
242 avctx
->flags
|= lavc_param
->bitexact
;
244 avctx
->width
= sh
->disp_w
;
245 avctx
->height
= sh
->disp_h
;
246 avctx
->workaround_bugs
= lavc_param
->workaround_bugs
;
247 avctx
->error_recognition
= lavc_param
->error_resilience
;
248 if(lavc_param
->gray
) avctx
->flags
|= CODEC_FLAG_GRAY
;
249 avctx
->flags2
|= lavc_param
->fast
;
250 avctx
->codec_tag
= sh
->format
;
251 avctx
->stream_codec_tag
= sh
->video
.fccHandler
;
252 avctx
->idct_algo
= lavc_param
->idct_algo
;
253 avctx
->error_concealment
= lavc_param
->error_concealment
;
254 avctx
->debug
= lavc_param
->debug
;
255 if (lavc_param
->debug
)
256 av_log_set_level(AV_LOG_DEBUG
);
257 avctx
->debug_mv
= lavc_param
->vismv
;
258 avctx
->skip_top
= lavc_param
->skip_top
;
259 avctx
->skip_bottom
= lavc_param
->skip_bottom
;
260 if(lavc_param
->lowres_str
!= NULL
)
262 sscanf(lavc_param
->lowres_str
, "%d,%d", &ctx
->lowres
, &lowres_w
);
263 if(ctx
->lowres
< 1 || ctx
->lowres
> 16 || (lowres_w
> 0 && avctx
->width
< lowres_w
))
265 avctx
->lowres
= ctx
->lowres
;
267 avctx
->skip_loop_filter
= str2AVDiscard(lavc_param
->skip_loop_filter_str
);
268 avctx
->skip_idct
= str2AVDiscard(lavc_param
->skip_idct_str
);
269 avctx
->skip_frame
= str2AVDiscard(lavc_param
->skip_frame_str
);
271 if(lavc_param
->avopt
){
272 if(parse_avopts(avctx
, lavc_param
->avopt
) < 0){
273 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavc_param
->avopt
);
279 mp_dbg(MSGT_DECVIDEO
, MSGL_DBG2
, "libavcodec.size: %d x %d\n", avctx
->width
, avctx
->height
);
280 switch (sh
->format
) {
281 case mmioFOURCC('S','V','Q','3'):
282 /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or
283 in the phony AVI header if demux_lavf is used. The first case is
284 handled here; the second case falls through to the next section. */
286 avctx
->extradata_size
= (*(int *)sh
->ImageDesc
) - sizeof(int);
287 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
288 memcpy(avctx
->extradata
, ((int *)sh
->ImageDesc
)+1, avctx
->extradata_size
);
293 case mmioFOURCC('A','V','R','n'):
294 case mmioFOURCC('M','J','P','G'):
295 /* AVRn stores huffman table in AVI header */
296 /* Pegasus MJPEG stores it also in AVI header, but it uses the common
298 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
300 avctx
->flags
|= CODEC_FLAG_EXTERN_HUFF
;
301 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
302 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
303 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
308 uint8_t *p
= avctx
->extradata
;
310 for (x
=0; x
<avctx
->extradata_size
; x
++)
311 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "[%x] ", p
[x
]);
312 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "\n");
317 case mmioFOURCC('R', 'V', '1', '0'):
318 case mmioFOURCC('R', 'V', '1', '3'):
319 case mmioFOURCC('R', 'V', '2', '0'):
320 case mmioFOURCC('R', 'V', '3', '0'):
321 case mmioFOURCC('R', 'V', '4', '0'):
322 if(sh
->bih
->biSize
<sizeof(*sh
->bih
)+8){
323 /* only 1 packet per frame & sub_id from fourcc */
324 avctx
->extradata_size
= 8;
325 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
326 ((uint32_t *)avctx
->extradata
)[0] = 0;
327 ((uint32_t *)avctx
->extradata
)[1] =
328 (sh
->format
== mmioFOURCC('R', 'V', '1', '3')) ? 0x10003001 : 0x10000000;
330 /* has extra slice header (demux_rm or rm->avi streamcopy) */
331 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
332 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
333 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
335 avctx
->sub_id
= AV_RB32(avctx
->extradata
+4);
337 // printf("%X %X %d %d\n", extrahdr[0], extrahdr[1]);
341 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(BITMAPINFOHEADER
))
343 avctx
->extradata_size
= sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
);
344 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
345 memcpy(avctx
->extradata
, sh
->bih
+1, avctx
->extradata_size
);
348 /* Pass palette to codec */
349 if (sh
->bih
&& (sh
->bih
->biBitCount
<= 8)) {
350 avctx
->palctrl
= calloc(1, sizeof(AVPaletteControl
));
351 avctx
->palctrl
->palette_changed
= 1;
352 if (sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
))
353 /* Palette size in biSize */
354 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
355 FFMIN(sh
->bih
->biSize
-sizeof(BITMAPINFOHEADER
), AVPALETTE_SIZE
));
357 /* Palette size in biClrUsed */
358 memcpy(avctx
->palctrl
->palette
, sh
->bih
+1,
359 FFMIN(sh
->bih
->biClrUsed
* 4, AVPALETTE_SIZE
));
363 avctx
->bits_per_coded_sample
= sh
->bih
->biBitCount
;
365 if(lavc_param
->threads
> 1)
366 avcodec_thread_init(avctx
, lavc_param
->threads
);
368 if (avcodec_open(avctx
, lavc_codec
) < 0) {
369 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not open codec.\n");
373 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "INFO: libavcodec init OK!\n");
374 return 1; //mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h, IMGFMT_YV12);
378 static void uninit(sh_video_t
*sh
){
379 vd_ffmpeg_ctx
*ctx
= sh
->context
;
380 AVCodecContext
*avctx
= ctx
->avctx
;
382 if(sh
->opts
->lavc_param
.vstats
){
385 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
, "QP: %d, count: %d\n", i
, ctx
->qp_stat
[i
]);
387 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "[VD_FFMPEG] Arithmetic mean of QP: %2.4f, Harmonic mean of QP: %2.4f\n",
388 ctx
->qp_sum
/ avctx
->coded_frame
->coded_picture_number
,
389 1.0/(ctx
->inv_qp_sum
/ avctx
->coded_frame
->coded_picture_number
)
394 if (avctx
->codec
&& avcodec_close(avctx
) < 0)
395 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not close codec.\n");
397 av_freep(&avctx
->extradata
);
398 av_freep(&avctx
->palctrl
);
399 av_freep(&avctx
->slice_offset
);
408 static void draw_slice(struct AVCodecContext
*s
,
409 const AVFrame
*src
, int offset
[4],
410 int y
, int type
, int height
){
411 sh_video_t
*sh
= s
->opaque
;
412 uint8_t *source
[MP_MAX_PLANES
]= {src
->data
[0] + offset
[0], src
->data
[1] + offset
[1], src
->data
[2] + offset
[2]};
413 int strides
[MP_MAX_PLANES
] = {src
->linesize
[0], src
->linesize
[1], src
->linesize
[2]};
417 vd_ffmpeg_ctx
*ctx
= sh
->context
;
418 int skip_stride
= ((width
<< ctx
->lowres
)+15)>>4;
419 uint8_t *skip
= &s
->coded_frame
->mbskip_table
[(y
>>4)*skip_stride
];
420 int threshold
= s
->coded_frame
->age
;
421 if(s
->pict_type
!=B_TYPE
){
422 for(i
=0; i
*16<width
+16; i
++){
423 if(i
*16>=width
|| skip
[i
]>=threshold
){
424 if(start
==i
) start
++;
426 uint8_t *src2
[3]= {src
[0] + start
*16,
429 //printf("%2d-%2d x %d\n", start, i, y);
430 mpcodecs_draw_slice (sh
, src2
, stride
, (i
-start
)*16, height
, start
*16, y
);
442 for (i
= 0; i
< MP_MAX_PLANES
; i
++)
444 strides
[i
] = -strides
[i
];
445 source
[i
] -= strides
[i
];
448 if (y
< sh
->disp_h
) {
449 mpcodecs_draw_slice (sh
, source
, strides
, sh
->disp_w
, (y
+height
)<=sh
->disp_h
?height
:sh
->disp_h
-y
, 0, y
);
454 static int init_vo(sh_video_t
*sh
, enum PixelFormat pix_fmt
){
455 vd_ffmpeg_ctx
*ctx
= sh
->context
;
456 AVCodecContext
*avctx
= ctx
->avctx
;
457 float aspect
= av_q2d(avctx
->sample_aspect_ratio
) * avctx
->width
/ avctx
->height
;
460 width
= avctx
->width
;
461 height
= avctx
->height
;
464 // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
465 // use dimensions from BIH to avoid black borders at the right and bottom.
466 if (sh
->bih
&& sh
->ImageDesc
) {
467 width
= sh
->bih
->biWidth
>> ctx
->lowres
;
468 height
= sh
->bih
->biHeight
>> ctx
->lowres
;
471 // it is possible another vo buffers to be used after vo config()
472 // lavc reset its buffers on width/heigh change but not on aspect change!!!
473 if (av_cmp_q(avctx
->sample_aspect_ratio
, ctx
->last_sample_aspect_ratio
) ||
474 width
!= sh
->disp_w
||
475 height
!= sh
->disp_h
||
476 pix_fmt
!= ctx
->pix_fmt
||
477 !ctx
->vo_initialized
)
479 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[ffmpeg] aspect_ratio: %f\n", aspect
);
480 if (sh
->aspect
== 0 ||
481 av_cmp_q(avctx
->sample_aspect_ratio
,
482 ctx
->last_sample_aspect_ratio
))
484 ctx
->last_sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
487 ctx
->pix_fmt
= pix_fmt
;
488 ctx
->best_csp
= pixfmt2imgfmt(pix_fmt
);
489 if (!mpcodecs_config_vo(sh
, sh
->disp_w
, sh
->disp_h
, ctx
->best_csp
))
491 ctx
->vo_initialized
= 1;
496 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
){
497 sh_video_t
*sh
= avctx
->opaque
;
498 vd_ffmpeg_ctx
*ctx
= sh
->context
;
499 mp_image_t
*mpi
=NULL
;
500 int flags
= MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_PREFER_ALIGNED_STRIDE
;
501 int type
= MP_IMGTYPE_IPB
;
502 int width
= avctx
->width
;
503 int height
= avctx
->height
;
504 avcodec_align_dimensions(avctx
, &width
, &height
);
505 //printf("get_buffer %d %d %d\n", pic->reference, ctx->ip_count, ctx->b_count);
507 if (pic
->buffer_hints
) {
508 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "Buffer hints: %u\n", pic
->buffer_hints
);
509 type
= MP_IMGTYPE_TEMP
;
510 if (pic
->buffer_hints
& FF_BUFFER_HINTS_READABLE
)
511 flags
|= MP_IMGFLAG_READABLE
;
512 if (pic
->buffer_hints
& FF_BUFFER_HINTS_PRESERVE
) {
513 type
= MP_IMGTYPE_STATIC
;
514 flags
|= MP_IMGFLAG_PRESERVE
;
516 if (pic
->buffer_hints
& FF_BUFFER_HINTS_REUSABLE
) {
517 type
= MP_IMGTYPE_STATIC
;
518 flags
|= MP_IMGFLAG_PRESERVE
;
520 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
521 MP_IMGFLAG_DRAW_CALLBACK
:0;
522 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, type
== MP_IMGTYPE_STATIC
? "using STATIC\n" : "using TEMP\n");
526 flags
|=(!avctx
->hurry_up
&& ctx
->do_slices
) ?
527 MP_IMGFLAG_DRAW_CALLBACK
:0;
530 flags
|= MP_IMGFLAG_PRESERVE
|MP_IMGFLAG_READABLE
531 | (ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0);
535 if(init_vo(sh
, avctx
->pix_fmt
) < 0){
536 avctx
->release_buffer
= avcodec_default_release_buffer
;
537 avctx
->get_buffer
= avcodec_default_get_buffer
;
538 return avctx
->get_buffer(avctx
, pic
);
541 if (IMGFMT_IS_XVMC(ctx
->best_csp
) || IMGFMT_IS_VDPAU(ctx
->best_csp
)) {
542 type
= MP_IMGTYPE_NUMBERED
| (0xffff << 16);
544 if (!pic
->buffer_hints
) {
545 if(ctx
->b_count
>1 || ctx
->ip_count
>2){
546 mp_tmsg(MSGT_DECVIDEO
, MSGL_WARN
, "[VD_FFMPEG] DRI failure.\n");
548 ctx
->do_dr1
=0; //FIXME
549 avctx
->get_buffer
= avcodec_default_get_buffer
;
550 return avctx
->get_buffer(avctx
, pic
);
553 if(avctx
->has_b_frames
){
554 type
= MP_IMGTYPE_IPB
;
558 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, type
== MP_IMGTYPE_IPB
? "using IPB\n" : "using IP\n");
561 if (ctx
->best_csp
== IMGFMT_RGB8
|| ctx
->best_csp
== IMGFMT_BGR8
)
562 flags
|= MP_IMGFLAG_RGB_PALETTE
;
563 mpi
= mpcodecs_get_image(sh
, type
, flags
, width
, height
);
566 // ok, let's see what did we get:
567 if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
&&
568 !(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
569 // nice, filter/vo likes draw_callback :)
570 avctx
->draw_horiz_band
= draw_slice
;
572 avctx
->draw_horiz_band
= NULL
;
573 if(IMGFMT_IS_VDPAU(mpi
->imgfmt
)) {
574 avctx
->draw_horiz_band
= draw_slice
;
577 if(IMGFMT_IS_XVMC(mpi
->imgfmt
)) {
578 struct xvmc_pix_fmt
*render
= mpi
->priv
; //same as data[2]
579 avctx
->draw_horiz_band
= draw_slice
;
580 if(!avctx
->xvmc_acceleration
) {
581 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "[VD_FFMPEG] The mc_get_buffer should work only with XVMC acceleration!!");
584 // return -1;//!!fixme check error conditions in ffmpeg
586 if(!(mpi
->flags
& MP_IMGFLAG_DIRECT
)) {
587 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "[VD_FFMPEG] Only buffers allocated by vo_xvmc allowed.\n");
590 // return -1;//!!fixme check error conditions in ffmpeg
592 if(mp_msg_test(MSGT_DECVIDEO
, MSGL_DBG5
))
593 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::get_buffer (xvmc render=%p)\n", render
);
595 assert(render
->xvmc_id
== AV_XVMC_ID
);
596 render
->state
|= AV_XVMC_STATE_PREDICTION
;
600 pic
->data
[0]= mpi
->planes
[0];
601 pic
->data
[1]= mpi
->planes
[1];
602 pic
->data
[2]= mpi
->planes
[2];
603 pic
->data
[3]= mpi
->planes
[3];
606 assert(mpi
->width
>= ((width
+align
)&(~align
)));
607 assert(mpi
->height
>= ((height
+align
)&(~align
)));
608 assert(mpi
->stride
[0] >= mpi
->width
);
609 if(mpi
->imgfmt
==IMGFMT_I420
|| mpi
->imgfmt
==IMGFMT_YV12
|| mpi
->imgfmt
==IMGFMT_IYUV
){
610 const int y_size
= mpi
->stride
[0] * (mpi
->h
-1) + mpi
->w
;
611 const int c_size
= mpi
->stride
[1] * ((mpi
->h
>>1)-1) + (mpi
->w
>>1);
613 assert(mpi
->planes
[0] > mpi
->planes
[1] || mpi
->planes
[0] + y_size
<= mpi
->planes
[1]);
614 assert(mpi
->planes
[0] > mpi
->planes
[2] || mpi
->planes
[0] + y_size
<= mpi
->planes
[2]);
615 assert(mpi
->planes
[1] > mpi
->planes
[0] || mpi
->planes
[1] + c_size
<= mpi
->planes
[0]);
616 assert(mpi
->planes
[1] > mpi
->planes
[2] || mpi
->planes
[1] + c_size
<= mpi
->planes
[2]);
617 assert(mpi
->planes
[2] > mpi
->planes
[0] || mpi
->planes
[2] + c_size
<= mpi
->planes
[0]);
618 assert(mpi
->planes
[2] > mpi
->planes
[1] || mpi
->planes
[2] + c_size
<= mpi
->planes
[1]);
622 /* Note, some (many) codecs in libavcodec must have stride1==stride2 && no changes between frames
623 * lavc will check that and die with an error message, if its not true
625 pic
->linesize
[0]= mpi
->stride
[0];
626 pic
->linesize
[1]= mpi
->stride
[1];
627 pic
->linesize
[2]= mpi
->stride
[2];
628 pic
->linesize
[3]= mpi
->stride
[3];
631 //printf("%X\n", (int)mpi->planes[0]);
633 if(mpi
->flags
&MP_IMGFLAG_DIRECT
)
635 else if(mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
)
641 pic
->age
= ctx
->ip_age
[0];
643 ctx
->ip_age
[0]= ctx
->ip_age
[1]+1;
647 pic
->age
= ctx
->b_age
;
653 pic
->type
= FF_BUFFER_TYPE_USER
;
655 /* The libavcodec reordered_opaque functionality is implemented by
656 * a similar copy in avcodec_default_get_buffer() and without a
657 * workaround like this it'd stop working when a custom buffer
660 pic
->reordered_opaque
= avctx
->reordered_opaque
;
664 static void release_buffer(struct AVCodecContext
*avctx
, AVFrame
*pic
){
665 mp_image_t
*mpi
= pic
->opaque
;
666 sh_video_t
*sh
= avctx
->opaque
;
667 vd_ffmpeg_ctx
*ctx
= sh
->context
;
670 //printf("release buffer %d %d %d\n", mpi ? mpi->flags&MP_IMGFLAG_PRESERVE : -99, ctx->ip_count, ctx->b_count);
672 if(ctx
->ip_count
<= 2 && ctx
->b_count
<=1){
673 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
)
680 // Palette support: free palette buffer allocated in get_buffer
682 av_freep(&mpi
->planes
[1]);
684 if (IMGFMT_IS_XVMC(mpi
->imgfmt
)) {
685 struct xvmc_pix_fmt
*render
= (struct xvmc_pix_fmt
*)pic
->data
[2]; //same as mpi->priv
686 if(mp_msg_test(MSGT_DECVIDEO
, MSGL_DBG5
))
687 mp_msg(MSGT_DECVIDEO
, MSGL_DBG5
, "vd_ffmpeg::release_buffer (xvmc render=%p)\n", render
);
688 assert(render
!=NULL
);
689 assert(render
->xvmc_id
== AV_XVMC_ID
);
690 render
->state
&=~AV_XVMC_STATE_PREDICTION
;
693 // release mpi (in case MPI_IMGTYPE_NUMBERED is used, e.g. for VDPAU)
697 if(pic
->type
!=FF_BUFFER_TYPE_USER
){
698 avcodec_default_release_buffer(avctx
, pic
);
705 //printf("R%X %X\n", pic->linesize[0], pic->data[0]);
708 // copypaste from demux_real.c - it should match to get it working!
709 //FIXME put into some header
710 typedef struct dp_hdr_s
{
711 uint32_t chunks
; // number of chunks
712 uint32_t timestamp
; // timestamp from packet header
713 uint32_t len
; // length of actual data
714 uint32_t chunktab
; // offset to chunk offset array
717 static void swap_palette(void *pal
) {
720 for (i
= 0; i
< AVPALETTE_COUNT
; i
++)
721 p
[i
] = le2me_32(p
[i
]);
725 static struct mp_image
*decode(struct sh_video
*sh
, void *data
, int len
,
726 int flags
, double *reordered_pts
)
730 vd_ffmpeg_ctx
*ctx
= sh
->context
;
731 AVFrame
*pic
= ctx
->pic
;
732 AVCodecContext
*avctx
= ctx
->avctx
;
733 struct lavc_param
*lavc_param
= &sh
->opts
->lavc_param
;
734 mp_image_t
*mpi
=NULL
;
735 int dr1
= ctx
->do_dr1
;
738 if(len
<=0) return NULL
; // skipped frame
740 //ffmpeg interlace (mpeg2) bug have been fixed. no need of -noslices
742 avctx
->draw_horiz_band
=NULL
;
743 if(ctx
->vo_initialized
&& !(flags
&3) && !dr1
){
744 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
|
745 (ctx
->do_slices
?MP_IMGFLAG_DRAW_CALLBACK
:0),
746 sh
->disp_w
, sh
->disp_h
);
747 if(mpi
&& mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
){
748 // vd core likes slices!
749 avctx
->draw_horiz_band
=draw_slice
;
753 avctx
->hurry_up
=(flags
&3)?((flags
&2)?2:1):0;
755 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "vd_ffmpeg data: %04x, %04x, %04x, %04x\n",
756 ((int *)data
)[0], ((int *)data
)[1], ((int *)data
)[2], ((int *)data
)[3]);
757 av_init_packet(&pkt
);
760 // HACK: make PNGs decode normally instead of as CorePNG delta frames
761 pkt
.flags
= PKT_FLAG_KEY
;
762 // The avcodec opaque field stupidly supports only int64_t type
763 *(double *)&avctx
->reordered_opaque
= *reordered_pts
;
764 ret
= avcodec_decode_video2(avctx
, pic
, &got_picture
, &pkt
);
765 *reordered_pts
= *(double *)&pic
->reordered_opaque
;
768 if(ret
<0) mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error while decoding frame!\n");
769 //printf("repeat: %d\n", pic->repeat_pict);
770 //-- vstats generation
771 while(lavc_param
->vstats
){ // always one time loop
772 static FILE *fvstats
=NULL
;
774 static long long int all_len
=0;
775 static int frame_number
=0;
776 static double all_frametime
=0.0;
777 AVFrame
*pic
= avctx
->coded_frame
;
784 today
= localtime(&today2
);
785 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
786 today
->tm_min
, today
->tm_sec
);
787 fvstats
= fopen(filename
, "w");
790 lavc_param
->vstats
=0; // disable block
796 // average MB quantizer
799 int w
= ((avctx
->width
<< ctx
->lowres
)+15) >> 4;
800 int h
= ((avctx
->height
<< ctx
->lowres
)+15) >> 4;
801 int8_t *q
= pic
->qscale_table
;
802 for(y
= 0; y
< h
; y
++) {
803 for(x
= 0; x
< w
; x
++)
804 quality
+= (double)*(q
+x
);
811 all_frametime
+=sh
->frametime
;
812 fprintf(fvstats
, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
813 ++frame_number
, quality
, len
, (double)all_len
/1024);
814 fprintf(fvstats
, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
815 all_frametime
, (double)(len
*8)/sh
->frametime
/1000.0,
816 (double)(all_len
*8)/all_frametime
/1000.0);
817 switch(pic
->pict_type
){
819 fprintf(fvstats
, "type= I\n");
822 fprintf(fvstats
, "type= P\n");
825 fprintf(fvstats
, "type= S\n");
828 fprintf(fvstats
, "type= B\n");
831 fprintf(fvstats
, "type= ? (%d)\n", pic
->pict_type
);
835 ctx
->qp_stat
[(int)(quality
+0.5)]++;
836 ctx
->qp_sum
+= quality
;
837 ctx
->inv_qp_sum
+= 1.0/(double)quality
;
843 if(!got_picture
) return NULL
; // skipped image
845 if(init_vo(sh
, avctx
->pix_fmt
) < 0) return NULL
;
847 if(dr1
&& pic
->opaque
){
848 mpi
= (mp_image_t
*)pic
->opaque
;
852 mpi
=mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
,
853 avctx
->width
, avctx
->height
);
854 if(!mpi
){ // temporary!
855 mp_tmsg(MSGT_DECVIDEO
, MSGL_WARN
, "[VD_FFMPEG] Couldn't allocate image for codec.\n");
860 mpi
->planes
[0]=pic
->data
[0];
861 mpi
->planes
[1]=pic
->data
[1];
862 mpi
->planes
[2]=pic
->data
[2];
863 mpi
->planes
[3]=pic
->data
[3];
864 mpi
->stride
[0]=pic
->linesize
[0];
865 mpi
->stride
[1]=pic
->linesize
[1];
866 mpi
->stride
[2]=pic
->linesize
[2];
867 mpi
->stride
[3]=pic
->linesize
[3];
873 if(avctx
->pix_fmt
==PIX_FMT_YUV422P
&& mpi
->chroma_y_shift
==1){
874 // we have 422p but user wants 420p
880 // FIXME: this might cause problems for buffers with FF_BUFFER_HINTS_PRESERVE
882 swap_palette(mpi
->planes
[1]);
884 /* to comfirm with newer lavc style */
885 mpi
->qscale
=pic
->qscale_table
;
886 mpi
->qstride
=pic
->qstride
;
887 mpi
->pict_type
=pic
->pict_type
;
888 mpi
->qscale_type
= pic
->qscale_type
;
889 mpi
->fields
= MP_IMGFIELD_ORDERED
;
890 if(pic
->interlaced_frame
) mpi
->fields
|= MP_IMGFIELD_INTERLACED
;
891 if(pic
->top_field_first
) mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
892 if(pic
->repeat_pict
== 1) mpi
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
897 static enum PixelFormat
get_format(struct AVCodecContext
*avctx
,
898 const enum PixelFormat
*fmt
){
899 enum PixelFormat selected_format
;
901 sh_video_t
*sh
= avctx
->opaque
;
904 for(i
=0;fmt
[i
]!=PIX_FMT_NONE
;i
++){
905 imgfmt
= pixfmt2imgfmt(fmt
[i
]);
906 if(!IMGFMT_IS_XVMC(imgfmt
) && !IMGFMT_IS_VDPAU(imgfmt
)) continue;
907 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[VD_FFMPEG] Trying pixfmt=%d.\n", i
);
908 if(init_vo(sh
, fmt
[i
]) >= 0) {
912 selected_format
= fmt
[i
];
913 return selected_format
;
916 const struct vd_functions mpcodecs_vd_ffmpeg
= {