2022-02-01 09:42:25 +02:00
|
|
|
/*
|
|
|
|
* MPEGVideo decoders header
|
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
|
|
|
* Copyright (c) 2002-2004 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* mpegvideo decoder header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVCODEC_MPEGVIDEODEC_H
|
|
|
|
#define AVCODEC_MPEGVIDEODEC_H
|
|
|
|
|
|
|
|
#include "libavutil/frame.h"
|
2021-01-27 20:06:36 +02:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
|
2022-02-01 09:42:25 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "get_bits.h"
|
|
|
|
#include "mpegpicture.h"
|
|
|
|
#include "mpegvideo.h"
|
|
|
|
#include "mpegvideodata.h"
|
|
|
|
|
2022-03-22 13:02:31 +02:00
|
|
|
#define FF_MPV_QSCALE_TYPE_MPEG1 0
|
|
|
|
#define FF_MPV_QSCALE_TYPE_MPEG2 1
|
|
|
|
|
2022-02-01 09:42:25 +02:00
|
|
|
/**
|
|
|
|
* Initialize the given MpegEncContext for decoding.
|
|
|
|
* the changed fields will not depend upon
|
|
|
|
* the prior state of the MpegEncContext.
|
avcodec/mpegpicture: Make MPVPicture refcounted
Up until now, an initialized MpegEncContext had an array of
MPVPictures (way more than were ever needed) and the MPVPicture*
contained in the MPVWorkPictures as well as the input_picture
and reordered_input_picture arrays (for the encoder) pointed
into this array. Several of the pointers could point to the
same slot and because there was no reference counting involved,
one had to check for aliasing before unreferencing.
Furthermore, given that these pointers were not ownership pointers
the pointers were often simply reset without unreferencing
the slot (happened e.g. for the RV30 and RV40 decoders) or
there were moved without resetting the src pointer (happened
for the encoders where the entries in the input_picture
and reordered_input_picture arrays were not reset).
Instead actually releasing these pictures was performed by looping
over the whole array and checking which one of the entries needed
to be kept. Given that the array had way too many slots (36),
this meant that more than 30 MPVPictures have been unnecessarily
unreferenced in every ff_mpv_frame_start(); something similar
happened for the encoder.
This commit changes this by making the MPVPictures refcounted
via the RefStruct API. The MPVPictures itself are part of a pool
so that this does not entail constant allocations; instead,
the amount of allocations actually goes down, because the
earlier code used such a large array of MPVPictures (36 entries) and
allocated an AVFrame for every one of these on every
ff_mpv_common_init(). In fact, the pool is only freed when closing
the codec, so that reinitializations don't lead to new allocations
(this avoids having to sync the pool in update_thread_context).
Making MPVPictures refcounted also has another key benefit:
It makes it possible to directly share them across threads
(when using frame-threaded decoding), eliminating ugly code
with underlying av_frame_ref()'s; sharing these pictures
can't fail any more.
The pool is allocated in ff_mpv_decode_init() for decoders,
which therefore can fail now. This and the fact that the pool
is not unreferenced in ff_mpv_common_end() also necessitated
to mark several mpegvideo-decoders with the FF_CODEC_CAP_INIT_CLEANUP
flag.
*: This also means that there is no good reason any more for
ff_mpv_common_frame_size_change() to exist.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-15 21:34:43 +02:00
|
|
|
*
|
|
|
|
* Also initialize the picture pool.
|
2022-02-01 09:42:25 +02:00
|
|
|
*/
|
avcodec/mpegpicture: Make MPVPicture refcounted
Up until now, an initialized MpegEncContext had an array of
MPVPictures (way more than were ever needed) and the MPVPicture*
contained in the MPVWorkPictures as well as the input_picture
and reordered_input_picture arrays (for the encoder) pointed
into this array. Several of the pointers could point to the
same slot and because there was no reference counting involved,
one had to check for aliasing before unreferencing.
Furthermore, given that these pointers were not ownership pointers
the pointers were often simply reset without unreferencing
the slot (happened e.g. for the RV30 and RV40 decoders) or
there were moved without resetting the src pointer (happened
for the encoders where the entries in the input_picture
and reordered_input_picture arrays were not reset).
Instead actually releasing these pictures was performed by looping
over the whole array and checking which one of the entries needed
to be kept. Given that the array had way too many slots (36),
this meant that more than 30 MPVPictures have been unnecessarily
unreferenced in every ff_mpv_frame_start(); something similar
happened for the encoder.
This commit changes this by making the MPVPictures refcounted
via the RefStruct API. The MPVPictures itself are part of a pool
so that this does not entail constant allocations; instead,
the amount of allocations actually goes down, because the
earlier code used such a large array of MPVPictures (36 entries) and
allocated an AVFrame for every one of these on every
ff_mpv_common_init(). In fact, the pool is only freed when closing
the codec, so that reinitializations don't lead to new allocations
(this avoids having to sync the pool in update_thread_context).
Making MPVPictures refcounted also has another key benefit:
It makes it possible to directly share them across threads
(when using frame-threaded decoding), eliminating ugly code
with underlying av_frame_ref()'s; sharing these pictures
can't fail any more.
The pool is allocated in ff_mpv_decode_init() for decoders,
which therefore can fail now. This and the fact that the pool
is not unreferenced in ff_mpv_common_end() also necessitated
to mark several mpegvideo-decoders with the FF_CODEC_CAP_INIT_CLEANUP
flag.
*: This also means that there is no good reason any more for
ff_mpv_common_frame_size_change() to exist.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-15 21:34:43 +02:00
|
|
|
int ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx);
|
2022-02-01 09:42:25 +02:00
|
|
|
|
|
|
|
int ff_mpv_common_frame_size_change(MpegEncContext *s);
|
|
|
|
|
|
|
|
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx);
|
2024-04-28 01:41:49 +02:00
|
|
|
/**
|
|
|
|
* Ensure that the dummy frames are allocated according to pict_type if necessary.
|
|
|
|
*/
|
|
|
|
int ff_mpv_alloc_dummy_frames(MpegEncContext *s);
|
2022-10-14 05:39:59 +02:00
|
|
|
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64]);
|
2022-02-01 09:42:25 +02:00
|
|
|
void ff_mpv_report_decode_progress(MpegEncContext *s);
|
|
|
|
void ff_mpv_frame_end(MpegEncContext *s);
|
|
|
|
|
2023-10-08 01:44:47 +02:00
|
|
|
int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f,
|
|
|
|
const MPVPicture *p, int qp_type);
|
2022-02-01 09:42:25 +02:00
|
|
|
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
|
|
|
|
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h);
|
|
|
|
void ff_mpeg_flush(AVCodecContext *avctx);
|
2024-04-28 17:40:58 +02:00
|
|
|
int ff_mpv_decode_close(AVCodecContext *avctx);
|
2022-02-01 09:42:25 +02:00
|
|
|
|
2023-10-08 01:44:47 +02:00
|
|
|
void ff_print_debug_info(const MpegEncContext *s, const MPVPicture *p, AVFrame *pict);
|
2022-02-01 09:42:25 +02:00
|
|
|
|
|
|
|
static inline int mpeg_get_qscale(MpegEncContext *s)
|
|
|
|
{
|
|
|
|
int qscale = get_bits(&s->gb, 5);
|
|
|
|
if (s->q_scale_type)
|
|
|
|
return ff_mpeg2_non_linear_qscale[qscale];
|
|
|
|
else
|
|
|
|
return qscale << 1;
|
|
|
|
}
|
|
|
|
|
2021-01-27 20:06:36 +02:00
|
|
|
static inline int check_marker(void *logctx, GetBitContext *s, const char *msg)
|
|
|
|
{
|
|
|
|
int bit = get_bits1(s);
|
|
|
|
if (!bit)
|
|
|
|
av_log(logctx, AV_LOG_INFO, "Marker bit missing at %d of %d %s\n",
|
|
|
|
get_bits_count(s) - 1, s->size_in_bits, msg);
|
|
|
|
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
2022-02-01 09:42:25 +02:00
|
|
|
#endif /* AVCODEC_MPEGVIDEODEC_H */
|