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.
25 #include <libavutil/common.h>
26 #include <libavutil/opt.h>
27 #include <libavutil/intreadwrite.h>
36 #include "fmt-conversion.h"
39 #include "img_format.h"
40 #include "libmpdemux/stheader.h"
41 #include "libmpdemux/demux_packet.h"
42 #include "codec-cfg.h"
43 #include "osdep/numcores.h"
45 static const vd_info_t info
= {
46 "libavcodec video codecs",
51 .print_name
= "libavcodec",
54 #include "libavcodec/avcodec.h"
56 #if AVPALETTE_SIZE > 1024
57 #error palette too large, adapt libmpcodecs/vf.c:vf_get_image
61 AVCodecContext
*avctx
;
63 enum PixelFormat pix_fmt
;
73 AVRational last_sample_aspect_ratio
;
74 enum AVDiscard skip_frame
;
79 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
80 static void release_buffer(AVCodecContext
*avctx
, AVFrame
*pic
);
81 static void draw_slice(struct AVCodecContext
*s
, const AVFrame
*src
,
82 int offset
[4], int y
, int type
, int height
);
84 static enum PixelFormat
get_format(struct AVCodecContext
*avctx
,
85 const enum PixelFormat
*pix_fmt
);
86 static void uninit(struct sh_video
*sh
);
88 const m_option_t lavc_decode_opts_conf
[] = {
89 OPT_INTRANGE("bug", lavc_param
.workaround_bugs
, 0, -1, 999999),
90 OPT_FLAG_ON("gray", lavc_param
.gray
, 0),
91 OPT_INTRANGE("idct", lavc_param
.idct_algo
, 0, 0, 99),
92 OPT_INTRANGE("ec", lavc_param
.error_concealment
, 0, 0, 99),
93 OPT_FLAG_ON("vstats", lavc_param
.vstats
, 0),
94 OPT_INTRANGE("debug", lavc_param
.debug
, 0, 0, 9999999),
95 OPT_INTRANGE("vismv", lavc_param
.vismv
, 0, 0, 9999999),
96 OPT_INTRANGE("st", lavc_param
.skip_top
, 0, 0, 999),
97 OPT_INTRANGE("sb", lavc_param
.skip_bottom
, 0, 0, 999),
98 OPT_FLAG_CONSTANTS("fast", lavc_param
.fast
, 0, 0, CODEC_FLAG2_FAST
),
99 OPT_STRING("lowres", lavc_param
.lowres_str
, 0),
100 OPT_STRING("skiploopfilter", lavc_param
.skip_loop_filter_str
, 0),
101 OPT_STRING("skipidct", lavc_param
.skip_idct_str
, 0),
102 OPT_STRING("skipframe", lavc_param
.skip_frame_str
, 0),
103 OPT_INTRANGE("threads", lavc_param
.threads
, 0, 0, 16),
104 OPT_FLAG_CONSTANTS("bitexact", lavc_param
.bitexact
, 0, 0, CODEC_FLAG_BITEXACT
),
105 OPT_STRING("o", lavc_param
.avopt
, 0),
106 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
109 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 static int init(sh_video_t
*sh
)
124 struct lavc_param
*lavc_param
= &sh
->opts
->lavc_param
;
125 AVCodecContext
*avctx
;
128 int do_vis_debug
= lavc_param
->vismv
||
129 (lavc_param
->debug
& (FF_DEBUG_VIS_MB_TYPE
| FF_DEBUG_VIS_QP
));
131 ctx
= sh
->context
= talloc_zero(NULL
, vd_ffmpeg_ctx
);
133 if (sh
->codec
->dll
) {
134 lavc_codec
= avcodec_find_decoder_by_name(sh
->codec
->dll
);
136 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
,
137 "Cannot find codec '%s' in libavcodec...\n",
142 } else if (!sh
->libav_codec_id
) {
143 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "No Libav codec ID known. "
144 "Generic lavc decoder is not applicable.\n");
147 lavc_codec
= avcodec_find_decoder(sh
->libav_codec_id
);
149 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "Libavcodec has no decoder "
155 sh
->codecname
= lavc_codec
->long_name
;
157 sh
->codecname
= lavc_codec
->name
;
159 if (sh
->opts
->vd_use_slices
160 && (lavc_codec
->capabilities
& CODEC_CAP_DRAW_HORIZ_BAND
)
164 if (lavc_codec
->capabilities
& CODEC_CAP_DR1
&& !do_vis_debug
165 && lavc_codec
->id
!= CODEC_ID_H264
166 && lavc_codec
->id
!= CODEC_ID_INTERPLAY_VIDEO
167 && lavc_codec
->id
!= CODEC_ID_ROQ
&& lavc_codec
->id
!= CODEC_ID_VP8
168 && lavc_codec
->id
!= CODEC_ID_LAGARITH
)
170 ctx
->ip_count
= ctx
->b_count
= 0;
172 ctx
->pic
= avcodec_alloc_frame();
173 ctx
->avctx
= avcodec_alloc_context3(lavc_codec
);
176 avctx
->codec_type
= AVMEDIA_TYPE_VIDEO
;
177 avctx
->codec_id
= lavc_codec
->id
;
179 if (lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
// XvMC
180 || lavc_codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
) {
182 ctx
->do_slices
= true;
183 lavc_param
->threads
= 1;
184 avctx
->get_format
= get_format
;
185 avctx
->get_buffer
= get_buffer
;
186 avctx
->release_buffer
= release_buffer
;
187 avctx
->reget_buffer
= get_buffer
;
188 avctx
->draw_horiz_band
= draw_slice
;
189 if (lavc_codec
->capabilities
& CODEC_CAP_HWACCEL
)
190 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[VD_FFMPEG] XVMC-accelerated "
192 if (lavc_codec
->capabilities
& CODEC_CAP_HWACCEL_VDPAU
)
193 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[VD_FFMPEG] VDPAU hardware "
195 avctx
->slice_flags
= SLICE_FLAG_CODED_ORDER
| SLICE_FLAG_ALLOW_FIELD
;
198 if (lavc_param
->threads
== 0) {
199 int threads
= default_thread_count();
201 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "[VD_FFMPEG] Could not determine "
202 "thread count to use, defaulting to 1.\n");
205 threads
= FFMIN(threads
, 16);
206 lavc_param
->threads
= threads
;
208 /* Our get_buffer and draw_horiz_band callbacks are not safe to call
209 * from other threads. */
210 if (lavc_param
->threads
> 1) {
212 ctx
->do_slices
= false;
213 mp_tmsg(MSGT_DECVIDEO
, MSGL_V
, "Asking decoder to use "
214 "%d threads if supported.\n", lavc_param
->threads
);
218 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
219 avctx
->get_buffer
= get_buffer
;
220 avctx
->release_buffer
= release_buffer
;
221 avctx
->reget_buffer
= get_buffer
;
224 avctx
->flags
|= lavc_param
->bitexact
;
226 avctx
->coded_width
= sh
->disp_w
;
227 avctx
->coded_height
= sh
->disp_h
;
228 avctx
->workaround_bugs
= lavc_param
->workaround_bugs
;
229 if (lavc_param
->gray
)
230 avctx
->flags
|= CODEC_FLAG_GRAY
;
231 avctx
->flags2
|= lavc_param
->fast
;
232 avctx
->codec_tag
= sh
->format
;
233 avctx
->stream_codec_tag
= sh
->video
.fccHandler
;
234 avctx
->idct_algo
= lavc_param
->idct_algo
;
235 avctx
->error_concealment
= lavc_param
->error_concealment
;
236 avctx
->debug
= lavc_param
->debug
;
237 if (lavc_param
->debug
)
238 av_log_set_level(AV_LOG_DEBUG
);
239 avctx
->debug_mv
= lavc_param
->vismv
;
240 avctx
->skip_top
= lavc_param
->skip_top
;
241 avctx
->skip_bottom
= lavc_param
->skip_bottom
;
242 if (lavc_param
->lowres_str
!= NULL
) {
243 int lowres
, lowres_w
;
244 sscanf(lavc_param
->lowres_str
, "%d,%d", &lowres
, &lowres_w
);
245 if (lowres
< 1 || lowres
> 16 ||
246 lowres_w
> 0 && avctx
->width
< lowres_w
)
248 avctx
->lowres
= lowres
;
250 avctx
->skip_loop_filter
= str2AVDiscard(lavc_param
->skip_loop_filter_str
);
251 avctx
->skip_idct
= str2AVDiscard(lavc_param
->skip_idct_str
);
252 avctx
->skip_frame
= str2AVDiscard(lavc_param
->skip_frame_str
);
254 if (lavc_param
->avopt
) {
255 if (parse_avopts(avctx
, lavc_param
->avopt
) < 0) {
256 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
,
257 "Your options /%s/ look like gibberish to me pal\n",
264 // Do this after the above avopt handling in case it changes values
265 ctx
->skip_frame
= avctx
->skip_frame
;
267 mp_dbg(MSGT_DECVIDEO
, MSGL_DBG2
,
268 "libavcodec.size: %d x %d\n", avctx
->width
, avctx
->height
);
269 switch (sh
->format
) {
270 case mmioFOURCC('S','V','Q','3'):
271 /* SVQ3 extradata can show up as sh->ImageDesc if demux_mov is used, or
272 * in the phony AVI header if demux_lavf is used. The first case is
273 * handled here; the second case falls through to the next section. */
275 avctx
->extradata_size
= (*(int *)sh
->ImageDesc
) - sizeof(int);
276 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
277 FF_INPUT_BUFFER_PADDING_SIZE
);
278 memcpy(avctx
->extradata
, ((int *)sh
->ImageDesc
) + 1,
279 avctx
->extradata_size
);
284 case mmioFOURCC('A','V','R','n'):
285 case mmioFOURCC('M','J','P','G'):
286 /* AVRn stores huffman table in AVI header */
287 /* Pegasus MJPEG stores it also in AVI header, but it uses the common
289 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(*sh
->bih
))
291 av_opt_set_int(avctx
, "extern_huff", 1, AV_OPT_SEARCH_CHILDREN
);
292 avctx
->extradata_size
= sh
->bih
->biSize
- sizeof(*sh
->bih
);
293 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
294 FF_INPUT_BUFFER_PADDING_SIZE
);
295 memcpy(avctx
->extradata
, sh
->bih
+ 1, avctx
->extradata_size
);
298 case mmioFOURCC('R','V','1','0'):
299 case mmioFOURCC('R','V','1','3'):
300 case mmioFOURCC('R','V','2','0'):
301 case mmioFOURCC('R','V','3','0'):
302 case mmioFOURCC('R','V','4','0'):
303 if (sh
->bih
->biSize
< sizeof(*sh
->bih
) + 8) {
304 // only 1 packet per frame & sub_id from fourcc
305 avctx
->extradata_size
= 8;
306 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
307 FF_INPUT_BUFFER_PADDING_SIZE
);
308 ((uint32_t *)avctx
->extradata
)[0] = 0;
309 ((uint32_t *)avctx
->extradata
)[1] =
310 sh
->format
== mmioFOURCC('R','V','1','3') ?
311 0x10003001 : 0x10000000;
313 // has extra slice header (demux_rm or rm->avi streamcopy)
314 avctx
->extradata_size
= sh
->bih
->biSize
- sizeof(*sh
->bih
);
315 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
316 FF_INPUT_BUFFER_PADDING_SIZE
);
317 memcpy(avctx
->extradata
, sh
->bih
+ 1, avctx
->extradata_size
);
322 if (!sh
->bih
|| sh
->bih
->biSize
<= sizeof(*sh
->bih
))
324 avctx
->extradata_size
= sh
->bih
->biSize
- sizeof(*sh
->bih
);
325 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
326 FF_INPUT_BUFFER_PADDING_SIZE
);
327 memcpy(avctx
->extradata
, sh
->bih
+ 1, avctx
->extradata_size
);
332 avctx
->bits_per_coded_sample
= sh
->bih
->biBitCount
;
334 avctx
->thread_count
= lavc_param
->threads
;
337 if (avcodec_open2(avctx
, lavc_codec
, NULL
) < 0) {
338 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not open codec.\n");
345 static void uninit(sh_video_t
*sh
)
347 vd_ffmpeg_ctx
*ctx
= sh
->context
;
348 AVCodecContext
*avctx
= ctx
->avctx
;
350 sh
->codecname
= NULL
;
351 if (sh
->opts
->lavc_param
.vstats
&& avctx
->coded_frame
) {
352 for (int i
= 1; i
< 32; i
++)
353 mp_msg(MSGT_DECVIDEO
, MSGL_INFO
,
354 "QP: %d, count: %d\n", i
, ctx
->qp_stat
[i
]);
355 mp_tmsg(MSGT_DECVIDEO
, MSGL_INFO
, "[VD_FFMPEG] Arithmetic mean of QP: "
356 "%2.4f, Harmonic mean of QP: %2.4f\n",
357 ctx
->qp_sum
/ avctx
->coded_frame
->coded_picture_number
,
358 1.0 / (ctx
->inv_qp_sum
/ avctx
->coded_frame
->coded_picture_number
));
362 if (avctx
->codec
&& avcodec_close(avctx
) < 0)
363 mp_tmsg(MSGT_DECVIDEO
, MSGL_ERR
, "Could not close codec.\n");
365 av_freep(&avctx
->extradata
);
366 av_freep(&avctx
->slice_offset
);
374 static void draw_slice(struct AVCodecContext
*s
,
375 const AVFrame
*src
, int offset
[4],
376 int y
, int type
, int height
)
378 sh_video_t
*sh
= s
->opaque
;
379 uint8_t *source
[MP_MAX_PLANES
] = {
380 src
->data
[0] + offset
[0], src
->data
[1] + offset
[1],
381 src
->data
[2] + offset
[2]
383 int strides
[MP_MAX_PLANES
] = {
384 src
->linesize
[0], src
->linesize
[1], src
->linesize
[2]
390 for (i
= 0; i
< MP_MAX_PLANES
; i
++) {
391 strides
[i
] = -strides
[i
];
392 source
[i
] -= strides
[i
];
395 if (y
< sh
->disp_h
) {
396 height
= FFMIN(height
, sh
->disp_h
- y
);
397 mpcodecs_draw_slice(sh
, source
, strides
, sh
->disp_w
, height
, 0, y
);
402 static int init_vo(sh_video_t
*sh
, enum PixelFormat pix_fmt
)
404 vd_ffmpeg_ctx
*ctx
= sh
->context
;
405 AVCodecContext
*avctx
= ctx
->avctx
;
406 float aspect
= av_q2d(avctx
->sample_aspect_ratio
) *
407 avctx
->width
/ avctx
->height
;
410 width
= avctx
->width
;
411 height
= avctx
->height
;
414 // if sh->ImageDesc is non-NULL, it means we decode QuickTime(tm) video.
415 // use dimensions from BIH to avoid black borders at the right and bottom.
416 if (sh
->bih
&& sh
->ImageDesc
) {
417 width
= sh
->bih
->biWidth
>> avctx
->lowres
;
418 height
= sh
->bih
->biHeight
>> avctx
->lowres
;
421 /* Reconfiguring filter/VO chain may invalidate direct rendering buffers
422 * we have allocated for libavcodec (including the VDPAU HW decoding
423 * case). Is it guaranteed that the code below only triggers in a situation
424 * with no busy direct rendering buffers for reference frames?
426 if (av_cmp_q(avctx
->sample_aspect_ratio
, ctx
->last_sample_aspect_ratio
) ||
427 width
!= sh
->disp_w
|| height
!= sh
->disp_h
||
428 pix_fmt
!= ctx
->pix_fmt
|| !ctx
->vo_initialized
) {
429 ctx
->vo_initialized
= 0;
430 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[ffmpeg] aspect_ratio: %f\n", aspect
);
432 // Do not overwrite s->aspect on the first call, so that a container
433 // aspect if available is preferred.
434 // But set it even if the sample aspect did not change, since a
435 // resolution change can cause an aspect change even if the
436 // _sample_ aspect is unchanged.
437 if (sh
->aspect
== 0 || ctx
->last_sample_aspect_ratio
.den
)
439 ctx
->last_sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
442 ctx
->pix_fmt
= pix_fmt
;
443 ctx
->best_csp
= pixfmt2imgfmt(pix_fmt
);
444 const unsigned int *supported_fmts
;
445 if (ctx
->best_csp
== IMGFMT_YV12
)
446 supported_fmts
= (const unsigned int[]){
447 IMGFMT_YV12
, IMGFMT_I420
, IMGFMT_IYUV
, 0xffffffff
449 else if (ctx
->best_csp
== IMGFMT_422P
)
450 supported_fmts
= (const unsigned int[]){
451 IMGFMT_422P
, IMGFMT_YV12
, IMGFMT_I420
, IMGFMT_IYUV
, 0xffffffff
454 supported_fmts
= (const unsigned int[]){ctx
->best_csp
, 0xffffffff};
455 if (!mpcodecs_config_vo2(sh
, sh
->disp_w
, sh
->disp_h
, supported_fmts
,
458 ctx
->vo_initialized
= 1;
463 static int get_buffer(AVCodecContext
*avctx
, AVFrame
*pic
)
465 sh_video_t
*sh
= avctx
->opaque
;
466 vd_ffmpeg_ctx
*ctx
= sh
->context
;
467 mp_image_t
*mpi
= NULL
;
468 int flags
= MP_IMGFLAG_ACCEPT_ALIGNED_STRIDE
|
469 MP_IMGFLAG_PREFER_ALIGNED_STRIDE
;
470 int type
= MP_IMGTYPE_IPB
;
471 int width
= avctx
->width
;
472 int height
= avctx
->height
;
473 // special case to handle reget_buffer without buffer hints
474 if (pic
->opaque
&& pic
->data
[0] && !pic
->buffer_hints
)
476 avcodec_align_dimensions(avctx
, &width
, &height
);
478 if (pic
->buffer_hints
) {
479 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
, "Buffer hints: %u\n",
481 type
= MP_IMGTYPE_TEMP
;
482 if (pic
->buffer_hints
& FF_BUFFER_HINTS_READABLE
)
483 flags
|= MP_IMGFLAG_READABLE
;
484 if (pic
->buffer_hints
& FF_BUFFER_HINTS_PRESERVE
) {
485 type
= MP_IMGTYPE_STATIC
;
486 flags
|= MP_IMGFLAG_PRESERVE
;
488 if (pic
->buffer_hints
& FF_BUFFER_HINTS_REUSABLE
) {
489 type
= MP_IMGTYPE_STATIC
;
490 flags
|= MP_IMGFLAG_PRESERVE
;
492 flags
|= ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0;
493 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
,
494 type
== MP_IMGTYPE_STATIC
? "using STATIC\n" : "using TEMP\n");
496 if (!pic
->reference
) {
498 flags
|= ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0;
501 flags
|= MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
502 | (ctx
->do_slices
? MP_IMGFLAG_DRAW_CALLBACK
: 0);
506 if (init_vo(sh
, avctx
->pix_fmt
) < 0) {
507 avctx
->release_buffer
= avcodec_default_release_buffer
;
508 avctx
->get_buffer
= avcodec_default_get_buffer
;
509 avctx
->reget_buffer
= avcodec_default_reget_buffer
;
511 release_buffer(avctx
, pic
);
512 return avctx
->get_buffer(avctx
, pic
);
515 if (IMGFMT_IS_HWACCEL(ctx
->best_csp
))
516 type
= MP_IMGTYPE_NUMBERED
| (0xffff << 16);
517 else if (!pic
->buffer_hints
) {
518 if (ctx
->b_count
> 1 || ctx
->ip_count
> 2) {
519 mp_tmsg(MSGT_DECVIDEO
, MSGL_WARN
, "[VD_FFMPEG] DRI failure.\n");
521 ctx
->do_dr1
= 0; //FIXME
522 avctx
->get_buffer
= avcodec_default_get_buffer
;
523 avctx
->reget_buffer
= avcodec_default_reget_buffer
;
525 release_buffer(avctx
, pic
);
526 return avctx
->get_buffer(avctx
, pic
);
529 if (avctx
->has_b_frames
|| ctx
->b_count
)
530 type
= MP_IMGTYPE_IPB
;
532 type
= MP_IMGTYPE_IP
;
533 mp_msg(MSGT_DECVIDEO
, MSGL_DBG2
,
534 type
== MP_IMGTYPE_IPB
? "using IPB\n" : "using IP\n");
537 if (ctx
->best_csp
== IMGFMT_RGB8
|| ctx
->best_csp
== IMGFMT_BGR8
)
538 flags
|= MP_IMGFLAG_RGB_PALETTE
;
539 mpi
= mpcodecs_get_image(sh
, type
, flags
, width
, height
);
543 // ok, let's see what did we get:
544 if (mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
&&
545 !(mpi
->flags
& MP_IMGFLAG_DIRECT
)) {
546 // nice, filter/vo likes draw_callback :)
547 avctx
->draw_horiz_band
= draw_slice
;
549 avctx
->draw_horiz_band
= NULL
;
550 if (IMGFMT_IS_HWACCEL(mpi
->imgfmt
))
551 avctx
->draw_horiz_band
= draw_slice
;
553 pic
->data
[0] = mpi
->planes
[0];
554 pic
->data
[1] = mpi
->planes
[1];
555 pic
->data
[2] = mpi
->planes
[2];
556 pic
->data
[3] = mpi
->planes
[3];
558 /* Note: some (many) codecs in libavcodec require
559 * linesize[1] == linesize[2] and no changes between frames.
560 * Lavc will check that and die with an error message if it's not true.
562 pic
->linesize
[0] = mpi
->stride
[0];
563 pic
->linesize
[1] = mpi
->stride
[1];
564 pic
->linesize
[2] = mpi
->stride
[2];
565 pic
->linesize
[3] = mpi
->stride
[3];
569 pic
->type
= FF_BUFFER_TYPE_USER
;
571 /* The libavcodec reordered_opaque functionality is implemented by
572 * a similar copy in avcodec_default_get_buffer() and without a
573 * workaround like this it'd stop working when a custom buffer
576 pic
->reordered_opaque
= avctx
->reordered_opaque
;
580 static void release_buffer(struct AVCodecContext
*avctx
, AVFrame
*pic
)
582 mp_image_t
*mpi
= pic
->opaque
;
583 sh_video_t
*sh
= avctx
->opaque
;
584 vd_ffmpeg_ctx
*ctx
= sh
->context
;
586 if (ctx
->ip_count
<= 2 && ctx
->b_count
<= 1) {
587 if (mpi
->flags
& MP_IMGFLAG_PRESERVE
)
594 // Palette support: free palette buffer allocated in get_buffer
596 av_freep(&mpi
->planes
[1]);
597 // release mpi (in case MPI_IMGTYPE_NUMBERED is used, e.g. for VDPAU)
601 if (pic
->type
!= FF_BUFFER_TYPE_USER
) {
602 avcodec_default_release_buffer(avctx
, pic
);
606 for (int i
= 0; i
< 4; i
++)
610 static av_unused
void swap_palette(void *pal
)
614 for (i
= 0; i
< AVPALETTE_COUNT
; i
++)
615 p
[i
] = le2me_32(p
[i
]);
618 static struct mp_image
*decode(struct sh_video
*sh
, struct demux_packet
*packet
,
619 void *data
, int len
, int flags
,
620 double *reordered_pts
)
624 vd_ffmpeg_ctx
*ctx
= sh
->context
;
625 AVFrame
*pic
= ctx
->pic
;
626 AVCodecContext
*avctx
= ctx
->avctx
;
627 struct lavc_param
*lavc_param
= &sh
->opts
->lavc_param
;
628 mp_image_t
*mpi
= NULL
;
629 int dr1
= ctx
->do_dr1
;
633 avctx
->draw_horiz_band
= NULL
;
636 avctx
->skip_frame
= AVDISCARD_ALL
;
638 avctx
->skip_frame
= AVDISCARD_NONREF
;
640 avctx
->skip_frame
= ctx
->skip_frame
;
642 av_init_packet(&pkt
);
645 /* Some codecs (ZeroCodec, some cases of PNG) may want keyframe info
647 if (packet
&& packet
->keyframe
)
648 pkt
.flags
|= AV_PKT_FLAG_KEY
;
649 if (packet
&& packet
->avpacket
) {
650 pkt
.side_data
= packet
->avpacket
->side_data
;
651 pkt
.side_data_elems
= packet
->avpacket
->side_data_elems
;
653 // The avcodec opaque field stupidly supports only int64_t type
654 union pts
{ int64_t i
; double d
; };
655 avctx
->reordered_opaque
= (union pts
){.d
= *reordered_pts
}.i
;
656 ret
= avcodec_decode_video2(avctx
, pic
, &got_picture
, &pkt
);
657 *reordered_pts
= (union pts
){.i
= pic
->reordered_opaque
}.d
;
661 mp_msg(MSGT_DECVIDEO
, MSGL_WARN
, "Error while decoding frame!\n");
662 //-- vstats generation
663 while (lavc_param
->vstats
) { // always one time loop
664 static FILE *fvstats
= NULL
;
666 static long long int all_len
= 0;
667 static int frame_number
= 0;
668 static double all_frametime
= 0.0;
669 AVFrame
*pic
= avctx
->coded_frame
;
670 double quality
= 0.0;
679 today
= localtime(&today2
);
680 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
681 today
->tm_min
, today
->tm_sec
);
682 fvstats
= fopen(filename
, "w");
685 lavc_param
->vstats
= 0; // disable block
691 // average MB quantizer
694 int w
= ((avctx
->width
<< avctx
->lowres
) + 15) >> 4;
695 int h
= ((avctx
->height
<< avctx
->lowres
) + 15) >> 4;
696 int8_t *q
= pic
->qscale_table
;
697 for (y
= 0; y
< h
; y
++) {
698 for (x
= 0; x
< w
; x
++)
699 quality
+= (double)*(q
+ x
);
706 all_frametime
+= sh
->frametime
;
707 fprintf(fvstats
, "frame= %5d q= %2.2f f_size= %6d s_size= %8.0fkB ",
708 ++frame_number
, quality
, len
, (double)all_len
/ 1024);
709 fprintf(fvstats
, "time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
710 all_frametime
, (double)(len
* 8) / sh
->frametime
/ 1000.0,
711 (double)(all_len
* 8) / all_frametime
/ 1000.0);
712 switch (pic
->pict_type
) {
713 case AV_PICTURE_TYPE_I
:
714 fprintf(fvstats
, "type= I\n");
716 case AV_PICTURE_TYPE_P
:
717 fprintf(fvstats
, "type= P\n");
719 case AV_PICTURE_TYPE_S
:
720 fprintf(fvstats
, "type= S\n");
722 case AV_PICTURE_TYPE_B
:
723 fprintf(fvstats
, "type= B\n");
726 fprintf(fvstats
, "type= ? (%d)\n", pic
->pict_type
);
730 ctx
->qp_stat
[(int)(quality
+ 0.5)]++;
731 ctx
->qp_sum
+= quality
;
732 ctx
->inv_qp_sum
+= 1.0 / (double)quality
;
739 return NULL
; // skipped image
741 if (init_vo(sh
, avctx
->pix_fmt
) < 0)
744 if (dr1
&& pic
->opaque
)
745 mpi
= (mp_image_t
*)pic
->opaque
;
748 mpi
= mpcodecs_get_image(sh
, MP_IMGTYPE_EXPORT
, MP_IMGFLAG_PRESERVE
,
749 avctx
->width
, avctx
->height
);
750 if (!mpi
) { // temporary error?
751 mp_tmsg(MSGT_DECVIDEO
, MSGL_WARN
,
752 "[VD_FFMPEG] Couldn't allocate image for codec.\n");
757 mpi
->planes
[0] = pic
->data
[0];
758 mpi
->planes
[1] = pic
->data
[1];
759 mpi
->planes
[2] = pic
->data
[2];
760 mpi
->planes
[3] = pic
->data
[3];
761 mpi
->stride
[0] = pic
->linesize
[0];
762 mpi
->stride
[1] = pic
->linesize
[1];
763 mpi
->stride
[2] = pic
->linesize
[2];
764 mpi
->stride
[3] = pic
->linesize
[3];
770 if (ctx
->best_csp
== IMGFMT_422P
&& mpi
->chroma_y_shift
== 1) {
771 // we have 422p but user wants 420p
777 // FIXME: this might cause problems for buffers with FF_BUFFER_HINTS_PRESERVE
779 swap_palette(mpi
->planes
[1]);
782 mpi
->qscale
= pic
->qscale_table
;
783 mpi
->qstride
= pic
->qstride
;
784 mpi
->pict_type
= pic
->pict_type
;
785 mpi
->qscale_type
= pic
->qscale_type
;
786 mpi
->fields
= MP_IMGFIELD_ORDERED
;
787 if (pic
->interlaced_frame
)
788 mpi
->fields
|= MP_IMGFIELD_INTERLACED
;
789 if (pic
->top_field_first
)
790 mpi
->fields
|= MP_IMGFIELD_TOP_FIRST
;
791 if (pic
->repeat_pict
== 1)
792 mpi
->fields
|= MP_IMGFIELD_REPEAT_FIRST
;
797 static enum PixelFormat
get_format(struct AVCodecContext
*avctx
,
798 const enum PixelFormat
*fmt
)
800 sh_video_t
*sh
= avctx
->opaque
;
803 for (i
= 0; fmt
[i
] != PIX_FMT_NONE
; i
++) {
804 int imgfmt
= pixfmt2imgfmt(fmt
[i
]);
805 if (!IMGFMT_IS_HWACCEL(imgfmt
))
807 mp_msg(MSGT_DECVIDEO
, MSGL_V
, "[VD_FFMPEG] Trying pixfmt=%d.\n", i
);
808 if (init_vo(sh
, fmt
[i
]) >= 0)
814 static int control(sh_video_t
*sh
, int cmd
, void *arg
, ...)
816 vd_ffmpeg_ctx
*ctx
= sh
->context
;
817 AVCodecContext
*avctx
= ctx
->avctx
;
819 case VDCTRL_QUERY_FORMAT
: {
820 int format
= (*((int *)arg
));
821 if (format
== ctx
->best_csp
)
823 // possible conversions:
828 // "converted" using pointer/stride modification
829 if (ctx
->best_csp
== IMGFMT_YV12
)
830 return CONTROL_TRUE
; // u/v swap
831 if (ctx
->best_csp
== IMGFMT_422P
&& !ctx
->do_dr1
)
832 return CONTROL_TRUE
; // half stride
835 return CONTROL_FALSE
;
837 case VDCTRL_RESYNC_STREAM
:
838 avcodec_flush_buffers(avctx
);
840 case VDCTRL_QUERY_UNSEEN_FRAMES
:;
841 int delay
= avctx
->has_b_frames
;
842 if (avctx
->active_thread_type
& FF_THREAD_FRAME
)
843 delay
+= avctx
->thread_count
- 1;
845 case VDCTRL_RESET_ASPECT
:
846 if (ctx
->vo_initialized
)
847 ctx
->vo_initialized
= false;
848 init_vo(sh
, avctx
->pix_fmt
);
851 return CONTROL_UNKNOWN
;
854 const struct vd_functions mpcodecs_vd_ffmpeg
= {