2016-10-26 08:10:19 +02:00
|
|
|
/*
|
|
|
|
* generic decoding-related code
|
|
|
|
*
|
2017-04-07 06:19:56 +02:00
|
|
|
* This file is part of FFmpeg.
|
2016-10-26 08:10:19 +02:00
|
|
|
*
|
2017-04-07 06:19:56 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2016-10-26 08:10:19 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2017-04-07 06:19:56 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2016-10-26 08:10:19 +02:00
|
|
|
* 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
|
2017-04-07 06:19:56 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2016-10-26 08:10:19 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
#if CONFIG_ICONV
|
|
|
|
# include <iconv.h>
|
|
|
|
#endif
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
#include "libavutil/avassert.h"
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2017-04-07 06:19:56 +02:00
|
|
|
#include "libavutil/bprint.h"
|
2016-10-26 08:10:19 +02:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/frame.h"
|
|
|
|
#include "libavutil/hwcontext.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
2017-04-07 06:19:56 +02:00
|
|
|
#include "libavutil/internal.h"
|
2016-12-18 17:42:24 +02:00
|
|
|
#include "libavutil/intmath.h"
|
2018-07-18 20:52:33 +02:00
|
|
|
#include "libavutil/opt.h"
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
#include "decode.h"
|
2020-04-13 17:33:14 +02:00
|
|
|
#include "hwconfig.h"
|
2016-10-26 08:10:19 +02:00
|
|
|
#include "internal.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
2017-01-17 17:28:30 +02:00
|
|
|
typedef struct FramePool {
|
|
|
|
/**
|
|
|
|
* Pools for each data plane. For audio all the planes have the same size,
|
|
|
|
* so only pools[0] is used.
|
|
|
|
*/
|
|
|
|
AVBufferPool *pools[4];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pool parameters
|
|
|
|
*/
|
|
|
|
int format;
|
|
|
|
int width, height;
|
|
|
|
int stride_align[AV_NUM_DATA_POINTERS];
|
|
|
|
int linesize[4];
|
|
|
|
int planes;
|
|
|
|
int channels;
|
|
|
|
int samples;
|
|
|
|
} FramePool;
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
static int apply_param_change(AVCodecContext *avctx, const AVPacket *avpkt)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
|
|
|
int size = 0, ret;
|
|
|
|
const uint8_t *data;
|
|
|
|
uint32_t flags;
|
2017-04-07 06:19:56 +02:00
|
|
|
int64_t val;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
|
|
|
|
if (!data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
|
|
|
|
"changes, but PARAM_CHANGE side data was sent to it.\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < 4)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
flags = bytestream_get_le32(&data);
|
|
|
|
size -= 4;
|
|
|
|
|
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
|
|
|
|
if (size < 4)
|
|
|
|
goto fail;
|
2017-04-07 06:19:56 +02:00
|
|
|
val = bytestream_get_le32(&data);
|
|
|
|
if (val <= 0 || val > INT_MAX) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid channel count");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
avctx->channels = val;
|
2016-10-26 08:10:19 +02:00
|
|
|
size -= 4;
|
|
|
|
}
|
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
|
|
|
|
if (size < 8)
|
|
|
|
goto fail;
|
|
|
|
avctx->channel_layout = bytestream_get_le64(&data);
|
|
|
|
size -= 8;
|
|
|
|
}
|
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
|
|
|
|
if (size < 4)
|
|
|
|
goto fail;
|
2017-04-07 06:19:56 +02:00
|
|
|
val = bytestream_get_le32(&data);
|
|
|
|
if (val <= 0 || val > INT_MAX) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid sample rate");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
avctx->sample_rate = val;
|
2016-10-26 08:10:19 +02:00
|
|
|
size -= 4;
|
|
|
|
}
|
|
|
|
if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
|
|
|
|
if (size < 8)
|
|
|
|
goto fail;
|
|
|
|
avctx->width = bytestream_get_le32(&data);
|
|
|
|
avctx->height = bytestream_get_le32(&data);
|
|
|
|
size -= 8;
|
|
|
|
ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
fail2:
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:41:12 +02:00
|
|
|
static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
|
|
|
|
{
|
2017-04-23 01:06:47 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
2016-10-26 13:41:12 +02:00
|
|
|
av_packet_unref(avci->last_pkt_props);
|
2017-04-23 01:06:47 +02:00
|
|
|
if (pkt) {
|
|
|
|
ret = av_packet_copy_props(avci->last_pkt_props, pkt);
|
|
|
|
if (!ret)
|
2018-02-13 23:42:04 +02:00
|
|
|
avci->last_pkt_props->size = pkt->size; // HACK: Needed for ff_decode_frame_props().
|
2017-04-23 01:06:47 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2016-10-26 13:41:12 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* move the original frame to our backup */
|
|
|
|
av_frame_unref(avci->to_free);
|
|
|
|
av_frame_move_ref(avci->to_free, frame);
|
|
|
|
|
|
|
|
/* now copy everything except the AVBufferRefs back
|
|
|
|
* note that we make a COPY of the side data, so calling av_frame_free() on
|
|
|
|
* the caller's frame will work properly */
|
|
|
|
ret = av_frame_copy_props(frame, avci->to_free);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
|
|
|
|
memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
|
|
|
|
if (avci->to_free->extended_data != avci->to_free->data) {
|
2017-04-22 10:54:58 +02:00
|
|
|
int planes = avci->to_free->channels;
|
2016-10-26 08:10:19 +02:00
|
|
|
int size = planes * sizeof(*frame->extended_data);
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->extended_data = av_malloc(size);
|
|
|
|
if (!frame->extended_data) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
memcpy(frame->extended_data, avci->to_free->extended_data,
|
|
|
|
size);
|
|
|
|
} else
|
|
|
|
frame->extended_data = frame->data;
|
|
|
|
|
|
|
|
frame->format = avci->to_free->format;
|
|
|
|
frame->width = avci->to_free->width;
|
|
|
|
frame->height = avci->to_free->height;
|
|
|
|
frame->channel_layout = avci->to_free->channel_layout;
|
|
|
|
frame->nb_samples = avci->to_free->nb_samples;
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->channels = avci->to_free->channels;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-27 01:42:27 +02:00
|
|
|
int ff_decode_bsfs_init(AVCodecContext *avctx)
|
2016-11-12 18:43:55 +02:00
|
|
|
{
|
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
int ret;
|
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
if (avci->bsf)
|
2016-11-12 18:43:55 +02:00
|
|
|
return 0;
|
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
ret = av_bsf_list_parse_str(avctx->codec->bsfs, &avci->bsf);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error parsing decoder bitstream filters '%s': %s\n", avctx->codec->bsfs, av_err2str(ret));
|
|
|
|
if (ret != AVERROR(ENOMEM))
|
2018-07-18 20:52:33 +02:00
|
|
|
ret = AVERROR_BUG;
|
2020-04-25 23:48:21 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-11-12 18:43:55 +02:00
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
/* We do not currently have an API for passing the input timebase into decoders,
|
|
|
|
* but no filters used here should actually need it.
|
|
|
|
* So we make up some plausible-looking number (the MPEG 90kHz timebase) */
|
|
|
|
avci->bsf->time_base_in = (AVRational){ 1, 90000 };
|
|
|
|
ret = avcodec_parameters_from_context(avci->bsf->par_in, avctx);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2018-07-18 20:52:33 +02:00
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
ret = av_bsf_init(avci->bsf);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-11-12 18:43:55 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
ff_decode_bsfs_uninit(avctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
2016-10-26 08:10:19 +02:00
|
|
|
int ret;
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (avci->draining)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
ret = av_bsf_receive_packet(avci->bsf, pkt);
|
2016-11-12 18:43:55 +02:00
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
avci->draining = 1;
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
|
|
|
|
ret = extract_packet_props(avctx->internal, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
ret = apply_param_change(avctx, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (avctx->codec->receive_frame)
|
|
|
|
avci->compat_decode_consumed += pkt->size;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
return 0;
|
|
|
|
finish:
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
/**
|
|
|
|
* Attempt to guess proper monotonic timestamps for decoded video frames
|
|
|
|
* which might have incorrect times. Input timestamps may wrap around, in
|
|
|
|
* which case the output will as well.
|
|
|
|
*
|
|
|
|
* @param pts the pts field of the decoded AVPacket, as passed through
|
|
|
|
* AVFrame.pts
|
|
|
|
* @param dts the dts field of the decoded AVPacket
|
|
|
|
* @return one of the input values, may be AV_NOPTS_VALUE
|
|
|
|
*/
|
|
|
|
static int64_t guess_correct_pts(AVCodecContext *ctx,
|
|
|
|
int64_t reordered_pts, int64_t dts)
|
|
|
|
{
|
|
|
|
int64_t pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
if (dts != AV_NOPTS_VALUE) {
|
|
|
|
ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
|
|
|
|
ctx->pts_correction_last_dts = dts;
|
|
|
|
} else if (reordered_pts != AV_NOPTS_VALUE)
|
|
|
|
ctx->pts_correction_last_dts = reordered_pts;
|
|
|
|
|
|
|
|
if (reordered_pts != AV_NOPTS_VALUE) {
|
|
|
|
ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
|
|
|
|
ctx->pts_correction_last_pts = reordered_pts;
|
|
|
|
} else if(dts != AV_NOPTS_VALUE)
|
|
|
|
ctx->pts_correction_last_pts = dts;
|
|
|
|
|
|
|
|
if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
|
|
|
|
&& reordered_pts != AV_NOPTS_VALUE)
|
|
|
|
pts = reordered_pts;
|
|
|
|
else
|
|
|
|
pts = dts;
|
|
|
|
|
|
|
|
return pts;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
/*
|
|
|
|
* The core of the receive_frame_wrapper for the decoders implementing
|
|
|
|
* the simple API. Certain decoders might consume partial packets without
|
|
|
|
* returning any output, so this function needs to be called in a loop until it
|
|
|
|
* returns EAGAIN.
|
|
|
|
**/
|
|
|
|
static int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
DecodeSimpleContext *ds = &avci->ds;
|
|
|
|
AVPacket *pkt = ds->in_pkt;
|
2017-04-23 01:08:42 +02:00
|
|
|
// copy to ensure we do not change pkt
|
2017-10-22 05:30:12 +02:00
|
|
|
int got_frame, actual_got_frame;
|
2016-10-26 08:10:19 +02:00
|
|
|
int ret;
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (!pkt->data && !avci->draining) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
ret = ff_decode_get_packet(avctx, pkt);
|
|
|
|
if (ret < 0 && ret != AVERROR_EOF)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
// Some codecs (at least wma lossless) will crash when feeding drain packets
|
|
|
|
// after EOF was signaled.
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (avci->draining_done)
|
2016-10-26 08:10:19 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (!pkt->data &&
|
|
|
|
!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
|
|
|
|
avctx->active_thread_type & FF_THREAD_FRAME))
|
2016-10-26 08:10:19 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
got_frame = 0;
|
2017-04-07 06:19:56 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
|
2017-10-22 05:30:12 +02:00
|
|
|
ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
|
2016-10-26 08:10:19 +02:00
|
|
|
} else {
|
2017-10-22 05:30:12 +02:00
|
|
|
ret = avctx->codec->decode(avctx, frame, &got_frame, pkt);
|
2017-04-23 01:08:42 +02:00
|
|
|
|
2017-05-02 01:01:22 +02:00
|
|
|
if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
|
|
|
|
frame->pkt_dts = pkt->dts;
|
2017-04-23 01:08:42 +02:00
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
if(!avctx->has_b_frames)
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->pkt_pos = pkt->pos;
|
2017-04-07 06:19:56 +02:00
|
|
|
//FIXME these should be under if(!avctx->has_b_frames)
|
2016-10-26 08:10:19 +02:00
|
|
|
/* get_buffer is supposed to set frame parameters */
|
|
|
|
if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
|
2017-04-23 01:08:42 +02:00
|
|
|
if (!frame->sample_aspect_ratio.num) frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
|
|
|
|
if (!frame->width) frame->width = avctx->width;
|
|
|
|
if (!frame->height) frame->height = avctx->height;
|
|
|
|
if (frame->format == AV_PIX_FMT_NONE) frame->format = avctx->pix_fmt;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
emms_c();
|
2017-05-01 02:34:25 +02:00
|
|
|
actual_got_frame = got_frame;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
if (frame->flags & AV_FRAME_FLAG_DISCARD)
|
|
|
|
got_frame = 0;
|
|
|
|
if (got_frame)
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->best_effort_timestamp = guess_correct_pts(avctx,
|
|
|
|
frame->pts,
|
|
|
|
frame->pkt_dts);
|
2017-04-23 01:08:42 +02:00
|
|
|
} else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
|
2017-04-07 06:19:56 +02:00
|
|
|
uint8_t *side;
|
|
|
|
int side_size;
|
|
|
|
uint32_t discard_padding = 0;
|
|
|
|
uint8_t skip_reason = 0;
|
|
|
|
uint8_t discard_reason = 0;
|
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if (ret >= 0 && got_frame) {
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->best_effort_timestamp = guess_correct_pts(avctx,
|
|
|
|
frame->pts,
|
|
|
|
frame->pkt_dts);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (frame->format == AV_SAMPLE_FMT_NONE)
|
|
|
|
frame->format = avctx->sample_fmt;
|
2017-04-07 06:19:56 +02:00
|
|
|
if (!frame->channel_layout)
|
|
|
|
frame->channel_layout = avctx->channel_layout;
|
2017-04-22 10:54:58 +02:00
|
|
|
if (!frame->channels)
|
|
|
|
frame->channels = avctx->channels;
|
2017-04-07 06:19:56 +02:00
|
|
|
if (!frame->sample_rate)
|
|
|
|
frame->sample_rate = avctx->sample_rate;
|
|
|
|
}
|
|
|
|
|
2017-06-14 23:43:04 +02:00
|
|
|
side= av_packet_get_side_data(avci->last_pkt_props, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
|
2017-04-07 06:19:56 +02:00
|
|
|
if(side && side_size>=10) {
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples = AV_RL32(side) * avci->skip_samples_multiplier;
|
2017-04-07 06:19:56 +02:00
|
|
|
discard_padding = AV_RL32(side + 4);
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "skip %d / discard %d samples due to side data\n",
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples, (int)discard_padding);
|
2017-04-07 06:19:56 +02:00
|
|
|
skip_reason = AV_RL8(side + 8);
|
|
|
|
discard_reason = AV_RL8(side + 9);
|
|
|
|
}
|
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if ((frame->flags & AV_FRAME_FLAG_DISCARD) && got_frame &&
|
2017-04-07 06:19:56 +02:00
|
|
|
!(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples);
|
2017-04-23 01:08:42 +02:00
|
|
|
got_frame = 0;
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:31:03 +02:00
|
|
|
if (avci->skip_samples > 0 && got_frame &&
|
2017-04-07 06:19:56 +02:00
|
|
|
!(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
|
2020-01-06 12:31:03 +02:00
|
|
|
if(frame->nb_samples <= avci->skip_samples){
|
2017-04-23 01:08:42 +02:00
|
|
|
got_frame = 0;
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples -= frame->nb_samples;
|
2017-04-07 06:19:56 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples);
|
2017-04-07 06:19:56 +02:00
|
|
|
} else {
|
2020-01-06 12:31:03 +02:00
|
|
|
av_samples_copy(frame->extended_data, frame->extended_data, 0, avci->skip_samples,
|
|
|
|
frame->nb_samples - avci->skip_samples, avctx->channels, frame->format);
|
2017-04-07 06:19:56 +02:00
|
|
|
if(avctx->pkt_timebase.num && avctx->sample_rate) {
|
2020-01-06 12:31:03 +02:00
|
|
|
int64_t diff_ts = av_rescale_q(avci->skip_samples,
|
2017-04-07 06:19:56 +02:00
|
|
|
(AVRational){1, avctx->sample_rate},
|
|
|
|
avctx->pkt_timebase);
|
|
|
|
if(frame->pts!=AV_NOPTS_VALUE)
|
|
|
|
frame->pts += diff_ts;
|
|
|
|
#if FF_API_PKT_PTS
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
if(frame->pkt_pts!=AV_NOPTS_VALUE)
|
|
|
|
frame->pkt_pts += diff_ts;
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
|
|
|
if(frame->pkt_dts!=AV_NOPTS_VALUE)
|
|
|
|
frame->pkt_dts += diff_ts;
|
2017-04-22 10:54:58 +02:00
|
|
|
if (frame->pkt_duration >= diff_ts)
|
|
|
|
frame->pkt_duration -= diff_ts;
|
2017-04-07 06:19:56 +02:00
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
|
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples, frame->nb_samples);
|
|
|
|
frame->nb_samples -= avci->skip_samples;
|
|
|
|
avci->skip_samples = 0;
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame &&
|
2017-04-07 06:19:56 +02:00
|
|
|
!(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) {
|
|
|
|
if (discard_padding == frame->nb_samples) {
|
2017-04-23 01:08:42 +02:00
|
|
|
got_frame = 0;
|
2017-04-07 06:19:56 +02:00
|
|
|
} else {
|
|
|
|
if(avctx->pkt_timebase.num && avctx->sample_rate) {
|
|
|
|
int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
|
|
|
|
(AVRational){1, avctx->sample_rate},
|
|
|
|
avctx->pkt_timebase);
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->pkt_duration = diff_ts;
|
2017-04-07 06:19:56 +02:00
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
|
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
|
|
|
|
(int)discard_padding, frame->nb_samples);
|
|
|
|
frame->nb_samples -= discard_padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if ((avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL) && got_frame) {
|
2017-04-07 06:19:56 +02:00
|
|
|
AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
|
|
|
|
if (fside) {
|
2020-01-06 12:31:03 +02:00
|
|
|
AV_WL32(fside->data, avci->skip_samples);
|
2017-04-07 06:19:56 +02:00
|
|
|
AV_WL32(fside->data + 4, discard_padding);
|
|
|
|
AV_WL8(fside->data + 8, skip_reason);
|
|
|
|
AV_WL8(fside->data + 9, discard_reason);
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->skip_samples = 0;
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-23 01:08:42 +02:00
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if (avctx->codec->type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
!avci->showed_multi_packet_warning &&
|
|
|
|
ret >= 0 && ret != pkt->size && !(avctx->codec->capabilities & AV_CODEC_CAP_SUBFRAMES)) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Multiple frames in a packet.\n");
|
|
|
|
avci->showed_multi_packet_warning = 1;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (!got_frame)
|
|
|
|
av_frame_unref(frame);
|
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if (ret >= 0 && avctx->codec->type == AVMEDIA_TYPE_VIDEO && !(avctx->flags & AV_CODEC_FLAG_TRUNCATED))
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
ret = pkt->size;
|
|
|
|
|
|
|
|
#if FF_API_AVCTX_TIMEBASE
|
|
|
|
if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
|
2017-04-23 01:08:42 +02:00
|
|
|
avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
#endif
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-05-01 02:34:25 +02:00
|
|
|
/* do not stop draining when actual_got_frame != 0 or ret < 0 */
|
|
|
|
/* got_frame == 0 but actual_got_frame != 0 when frame is discarded */
|
2020-01-06 12:31:03 +02:00
|
|
|
if (avci->draining && !actual_got_frame) {
|
2017-04-28 12:19:13 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
/* prevent infinite loop if a decoder wrongly always return error on draining */
|
|
|
|
/* reasonable nb_errors_max = maximum b frames + thread count */
|
|
|
|
int nb_errors_max = 20 + (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME ?
|
|
|
|
avctx->thread_count : 1);
|
|
|
|
|
|
|
|
if (avci->nb_draining_errors++ >= nb_errors_max) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many errors when draining, this is a bug. "
|
|
|
|
"Stop draining and force EOF.\n");
|
|
|
|
avci->draining_done = 1;
|
|
|
|
ret = AVERROR_BUG;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
avci->draining_done = 1;
|
|
|
|
}
|
|
|
|
}
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
|
|
|
|
avci->compat_decode_consumed += ret;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (ret >= pkt->size || ret < 0) {
|
|
|
|
av_packet_unref(pkt);
|
2016-10-26 08:10:19 +02:00
|
|
|
} else {
|
|
|
|
int consumed = ret;
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
pkt->data += consumed;
|
|
|
|
pkt->size -= consumed;
|
2017-04-23 17:51:52 +02:00
|
|
|
avci->last_pkt_props->size -= consumed; // See extract_packet_props() comment.
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
avci->last_pkt_props->pts = AV_NOPTS_VALUE;
|
|
|
|
avci->last_pkt_props->dts = AV_NOPTS_VALUE;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (got_frame)
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
av_assert0(frame->buf[0]);
|
|
|
|
|
|
|
|
return ret < 0 ? ret : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (!frame->buf[0]) {
|
|
|
|
ret = decode_simple_internal(avctx, frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
2016-10-26 08:10:19 +02:00
|
|
|
int ret;
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
av_assert0(!frame->buf[0]);
|
|
|
|
|
|
|
|
if (avctx->codec->receive_frame)
|
|
|
|
ret = avctx->codec->receive_frame(avctx, frame);
|
|
|
|
else
|
|
|
|
ret = decode_simple_receive_frame(avctx, frame);
|
|
|
|
|
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
avci->draining_done = 1;
|
|
|
|
|
2017-10-13 18:59:17 +02:00
|
|
|
if (!ret) {
|
|
|
|
/* the only case where decode data is not set should be decoders
|
|
|
|
* that do not call ff_get_buffer() */
|
|
|
|
av_assert0((frame->private_ref && frame->private_ref->size == sizeof(FrameDecodeData)) ||
|
|
|
|
!(avctx->codec->capabilities & AV_CODEC_CAP_DR1));
|
|
|
|
|
2017-11-10 17:07:44 +02:00
|
|
|
if (frame->private_ref) {
|
|
|
|
FrameDecodeData *fdd = (FrameDecodeData*)frame->private_ref->data;
|
|
|
|
|
|
|
|
if (fdd->post_process) {
|
|
|
|
ret = fdd->post_process(avctx, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 18:59:17 +02:00
|
|
|
}
|
|
|
|
|
2017-11-11 13:59:44 +02:00
|
|
|
/* free the per-frame decode data */
|
|
|
|
av_buffer_unref(&frame->private_ref);
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
AVCodecInternal *avci = avctx->internal;
|
2017-04-23 01:08:42 +02:00
|
|
|
int ret;
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
if (avctx->internal->draining)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2017-04-23 01:08:42 +02:00
|
|
|
if (avpkt && !avpkt->size && avpkt->data)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2016-11-12 18:43:55 +02:00
|
|
|
av_packet_unref(avci->buffer_pkt);
|
|
|
|
if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
ret = av_packet_ref(avci->buffer_pkt, avpkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
ret = av_bsf_send_packet(avci->bsf, avci->buffer_pkt);
|
2016-11-12 18:43:55 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_packet_unref(avci->buffer_pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (!avci->buffer_frame->buf[0]) {
|
|
|
|
ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
|
|
|
|
return ret;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
return 0;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 17:42:24 +02:00
|
|
|
static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
/* make sure we are noisy about decoders returning invalid cropping data */
|
|
|
|
if (frame->crop_left >= INT_MAX - frame->crop_right ||
|
|
|
|
frame->crop_top >= INT_MAX - frame->crop_bottom ||
|
|
|
|
(frame->crop_left + frame->crop_right) >= frame->width ||
|
|
|
|
(frame->crop_top + frame->crop_bottom) >= frame->height) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
2017-05-08 18:11:17 +02:00
|
|
|
"Invalid cropping information set by a decoder: "
|
|
|
|
"%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
|
2016-12-18 17:42:24 +02:00
|
|
|
"(frame size %dx%d). This is a bug, please report it\n",
|
|
|
|
frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
|
|
|
|
frame->width, frame->height);
|
|
|
|
frame->crop_left = 0;
|
|
|
|
frame->crop_right = 0;
|
|
|
|
frame->crop_top = 0;
|
|
|
|
frame->crop_bottom = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!avctx->apply_cropping)
|
|
|
|
return 0;
|
|
|
|
|
2017-07-22 23:05:13 +02:00
|
|
|
return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
|
|
|
|
AV_FRAME_CROP_UNALIGNED : 0);
|
2016-12-18 17:42:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
2019-04-14 18:42:25 +02:00
|
|
|
int ret, changed;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
av_frame_unref(frame);
|
|
|
|
|
|
|
|
if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (avci->buffer_frame->buf[0]) {
|
|
|
|
av_frame_move_ref(frame, avci->buffer_frame);
|
|
|
|
} else {
|
|
|
|
ret = decode_receive_frame_internal(avctx, frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 17:42:24 +02:00
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
ret = apply_cropping(avctx, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
avctx->frame_number++;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2019-04-14 18:42:25 +02:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_DROPCHANGED) {
|
|
|
|
|
|
|
|
if (avctx->frame_number == 1) {
|
|
|
|
avci->initial_format = frame->format;
|
|
|
|
switch(avctx->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
avci->initial_width = frame->width;
|
|
|
|
avci->initial_height = frame->height;
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
avci->initial_sample_rate = frame->sample_rate ? frame->sample_rate :
|
|
|
|
avctx->sample_rate;
|
|
|
|
avci->initial_channels = frame->channels;
|
|
|
|
avci->initial_channel_layout = frame->channel_layout;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->frame_number > 1) {
|
|
|
|
changed = avci->initial_format != frame->format;
|
|
|
|
|
2019-10-04 14:22:15 +02:00
|
|
|
switch(avctx->codec_type) {
|
2019-04-14 18:42:25 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
changed |= avci->initial_width != frame->width ||
|
|
|
|
avci->initial_height != frame->height;
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
changed |= avci->initial_sample_rate != frame->sample_rate ||
|
|
|
|
avci->initial_sample_rate != avctx->sample_rate ||
|
|
|
|
avci->initial_channels != frame->channels ||
|
|
|
|
avci->initial_channel_layout != frame->channel_layout;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
avci->changed_frames_dropped++;
|
|
|
|
av_log(avctx, AV_LOG_INFO, "dropped changed frame #%d pts %"PRId64
|
|
|
|
" drop count: %d \n",
|
|
|
|
avctx->frame_number, frame->pts,
|
|
|
|
avci->changed_frames_dropped);
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return AVERROR_INPUT_CHANGED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
|
2017-04-23 01:08:42 +02:00
|
|
|
int *got_frame, const AVPacket *pkt)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
|
|
|
AVCodecInternal *avci = avctx->internal;
|
2017-04-03 21:58:15 +02:00
|
|
|
int ret = 0;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
av_assert0(avci->compat_decode_consumed == 0);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-11-09 14:21:23 +02:00
|
|
|
if (avci->draining_done && pkt && pkt->size != 0) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Got unexpected packet after EOF\n");
|
|
|
|
avcodec_flush_buffers(avctx);
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
*got_frame = 0;
|
|
|
|
avci->compat_decode = 1;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (avci->compat_decode_partial_size > 0 &&
|
|
|
|
avci->compat_decode_partial_size != pkt->size) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Got unexpected packet size after a partial decode\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto finish;
|
|
|
|
}
|
2016-10-26 13:41:12 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (!avci->compat_decode_partial_size) {
|
|
|
|
ret = avcodec_send_packet(avctx, pkt);
|
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
ret = 0;
|
|
|
|
else if (ret == AVERROR(EAGAIN)) {
|
|
|
|
/* we fully drain all the output in each decode call, so this should not
|
|
|
|
* ever happen */
|
|
|
|
ret = AVERROR_BUG;
|
|
|
|
goto finish;
|
|
|
|
} else if (ret < 0)
|
|
|
|
goto finish;
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
while (ret >= 0) {
|
|
|
|
ret = avcodec_receive_frame(avctx, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
|
|
|
|
ret = 0;
|
|
|
|
goto finish;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
if (frame != avci->compat_decode_frame) {
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!avctx->refcounted_frames) {
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
ret = unrefcount_frame(avci, frame);
|
|
|
|
if (ret < 0)
|
|
|
|
goto finish;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
*got_frame = 1;
|
|
|
|
frame = avci->compat_decode_frame;
|
|
|
|
} else {
|
|
|
|
if (!avci->compat_decode_warned) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "The deprecated avcodec_decode_* "
|
|
|
|
"API cannot return all the frames for this decoder. "
|
|
|
|
"Some frames will be dropped. Update your code to the "
|
|
|
|
"new decoding API to fix this.\n");
|
|
|
|
avci->compat_decode_warned = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
|
2016-11-12 18:43:55 +02:00
|
|
|
if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
break;
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
finish:
|
2016-11-12 18:43:55 +02:00
|
|
|
if (ret == 0) {
|
|
|
|
/* if there are any bsfs then assume full packet is always consumed */
|
|
|
|
if (avctx->codec->bsfs)
|
|
|
|
ret = pkt->size;
|
|
|
|
else
|
|
|
|
ret = FFMIN(avci->compat_decode_consumed, pkt->size);
|
|
|
|
}
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
avci->compat_decode_consumed = 0;
|
|
|
|
avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
|
|
|
|
int *got_picture_ptr,
|
2017-04-23 01:08:42 +02:00
|
|
|
const AVPacket *avpkt)
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
{
|
|
|
|
return compat_decode(avctx, picture, got_picture_ptr, avpkt);
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
|
|
|
|
AVFrame *frame,
|
|
|
|
int *got_frame_ptr,
|
2017-04-23 01:08:42 +02:00
|
|
|
const AVPacket *avpkt)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
return compat_decode(avctx, frame, got_frame_ptr, avpkt);
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
static void get_subtitle_defaults(AVSubtitle *sub)
|
|
|
|
{
|
|
|
|
memset(sub, 0, sizeof(*sub));
|
|
|
|
sub->pts = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
|
|
|
|
static int recode_subtitle(AVCodecContext *avctx,
|
|
|
|
AVPacket *outpkt, const AVPacket *inpkt)
|
|
|
|
{
|
|
|
|
#if CONFIG_ICONV
|
|
|
|
iconv_t cd = (iconv_t)-1;
|
|
|
|
int ret = 0;
|
|
|
|
char *inb, *outb;
|
|
|
|
size_t inl, outl;
|
|
|
|
AVPacket tmp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#if CONFIG_ICONV
|
|
|
|
cd = iconv_open("UTF-8", avctx->sub_charenc);
|
|
|
|
av_assert0(cd != (iconv_t)-1);
|
|
|
|
|
|
|
|
inb = inpkt->data;
|
|
|
|
inl = inpkt->size;
|
|
|
|
|
|
|
|
if (inl >= INT_MAX / UTF8_MAX_BYTES - AV_INPUT_BUFFER_PADDING_SIZE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
outpkt->buf = tmp.buf;
|
|
|
|
outpkt->data = tmp.data;
|
|
|
|
outpkt->size = tmp.size;
|
|
|
|
outb = outpkt->data;
|
|
|
|
outl = outpkt->size;
|
|
|
|
|
|
|
|
if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
|
|
|
|
iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
|
|
|
|
outl >= outpkt->size || inl != 0) {
|
|
|
|
ret = FFMIN(AVERROR(errno), -1);
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
|
|
|
|
"from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
|
|
|
|
av_packet_unref(&tmp);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
outpkt->size -= outl;
|
|
|
|
memset(outpkt->data + outpkt->size, 0, outl);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
end:
|
|
|
|
if (cd != (iconv_t)-1)
|
|
|
|
iconv_close(cd);
|
2016-10-26 08:10:19 +02:00
|
|
|
return ret;
|
2017-04-07 06:19:56 +02:00
|
|
|
#else
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int utf8_check(const uint8_t *str)
|
|
|
|
{
|
|
|
|
const uint8_t *byte;
|
|
|
|
uint32_t codepoint, min;
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
byte = str;
|
|
|
|
GET_UTF8(codepoint, *(byte++), return 0;);
|
|
|
|
min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
|
|
|
|
1 << (5 * (byte - str) - 4);
|
|
|
|
if (codepoint < min || codepoint >= 0x110000 ||
|
|
|
|
codepoint == 0xFFFE /* BOM */ ||
|
|
|
|
codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
|
|
|
|
return 0;
|
|
|
|
str = byte;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FF_API_ASS_TIMING
|
|
|
|
static void insert_ts(AVBPrint *buf, int ts)
|
|
|
|
{
|
|
|
|
if (ts == -1) {
|
|
|
|
av_bprintf(buf, "9:59:59.99,");
|
|
|
|
} else {
|
|
|
|
int h, m, s;
|
|
|
|
|
|
|
|
h = ts/360000; ts -= 360000*h;
|
|
|
|
m = ts/ 6000; ts -= 6000*m;
|
|
|
|
s = ts/ 100; ts -= 100*s;
|
|
|
|
av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int convert_sub_to_old_ass_form(AVSubtitle *sub, const AVPacket *pkt, AVRational tb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
AVBPrint buf;
|
|
|
|
|
|
|
|
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
|
|
|
|
|
|
|
|
for (i = 0; i < sub->num_rects; i++) {
|
|
|
|
char *final_dialog;
|
|
|
|
const char *dialog;
|
|
|
|
AVSubtitleRect *rect = sub->rects[i];
|
|
|
|
int ts_start, ts_duration = -1;
|
|
|
|
long int layer;
|
|
|
|
|
|
|
|
if (rect->type != SUBTITLE_ASS || !strncmp(rect->ass, "Dialogue: ", 10))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
av_bprint_clear(&buf);
|
|
|
|
|
|
|
|
/* skip ReadOrder */
|
|
|
|
dialog = strchr(rect->ass, ',');
|
|
|
|
if (!dialog)
|
|
|
|
continue;
|
|
|
|
dialog++;
|
|
|
|
|
|
|
|
/* extract Layer or Marked */
|
|
|
|
layer = strtol(dialog, (char**)&dialog, 10);
|
|
|
|
if (*dialog != ',')
|
|
|
|
continue;
|
|
|
|
dialog++;
|
|
|
|
|
|
|
|
/* rescale timing to ASS time base (ms) */
|
|
|
|
ts_start = av_rescale_q(pkt->pts, tb, av_make_q(1, 100));
|
|
|
|
if (pkt->duration != -1)
|
|
|
|
ts_duration = av_rescale_q(pkt->duration, tb, av_make_q(1, 100));
|
|
|
|
sub->end_display_time = FFMAX(sub->end_display_time, 10 * ts_duration);
|
|
|
|
|
|
|
|
/* construct ASS (standalone file form with timestamps) string */
|
|
|
|
av_bprintf(&buf, "Dialogue: %ld,", layer);
|
|
|
|
insert_ts(&buf, ts_start);
|
|
|
|
insert_ts(&buf, ts_duration == -1 ? -1 : ts_start + ts_duration);
|
|
|
|
av_bprintf(&buf, "%s\r\n", dialog);
|
|
|
|
|
|
|
|
final_dialog = av_strdup(buf.str);
|
|
|
|
if (!av_bprint_is_complete(&buf) || !final_dialog) {
|
|
|
|
av_freep(&final_dialog);
|
|
|
|
av_bprint_finalize(&buf, NULL);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_freep(&rect->ass);
|
|
|
|
rect->ass = final_dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_bprint_finalize(&buf, NULL);
|
|
|
|
return 0;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
#endif
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
|
|
|
|
int *got_sub_ptr,
|
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
2017-04-07 06:19:56 +02:00
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
if (!avpkt->data && avpkt->size) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (!avctx->codec)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
*got_sub_ptr = 0;
|
2017-04-07 06:19:56 +02:00
|
|
|
get_subtitle_defaults(sub);
|
|
|
|
|
|
|
|
if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
|
2017-10-22 05:30:12 +02:00
|
|
|
AVPacket pkt_recoded = *avpkt;
|
2017-04-07 06:19:56 +02:00
|
|
|
|
2017-10-22 05:30:12 +02:00
|
|
|
ret = recode_subtitle(avctx, &pkt_recoded, avpkt);
|
2017-04-07 06:19:56 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
*got_sub_ptr = 0;
|
|
|
|
} else {
|
2017-04-23 01:06:47 +02:00
|
|
|
ret = extract_packet_props(avctx->internal, &pkt_recoded);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-04-07 06:19:56 +02:00
|
|
|
|
|
|
|
if (avctx->pkt_timebase.num && avpkt->pts != AV_NOPTS_VALUE)
|
|
|
|
sub->pts = av_rescale_q(avpkt->pts,
|
|
|
|
avctx->pkt_timebase, AV_TIME_BASE_Q);
|
|
|
|
ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
|
|
|
|
av_assert1((ret >= 0) >= !!*got_sub_ptr &&
|
|
|
|
!!*got_sub_ptr >= !!sub->num_rects);
|
|
|
|
|
|
|
|
#if FF_API_ASS_TIMING
|
|
|
|
if (avctx->sub_text_format == FF_SUB_TEXT_FMT_ASS_WITH_TIMINGS
|
|
|
|
&& *got_sub_ptr && sub->num_rects) {
|
|
|
|
const AVRational tb = avctx->pkt_timebase.num ? avctx->pkt_timebase
|
|
|
|
: avctx->time_base;
|
|
|
|
int err = convert_sub_to_old_ass_form(sub, avpkt, tb);
|
|
|
|
if (err < 0)
|
|
|
|
ret = err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
|
|
|
|
avctx->pkt_timebase.num) {
|
|
|
|
AVRational ms = { 1, 1000 };
|
|
|
|
sub->end_display_time = av_rescale_q(avpkt->duration,
|
|
|
|
avctx->pkt_timebase, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
|
|
|
|
sub->format = 0;
|
|
|
|
else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
|
|
|
|
sub->format = 1;
|
|
|
|
|
|
|
|
for (i = 0; i < sub->num_rects; i++) {
|
2018-03-24 14:37:00 +02:00
|
|
|
if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_IGNORE &&
|
|
|
|
sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
|
2017-04-07 06:19:56 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Invalid UTF-8 in decoded subtitles text; "
|
|
|
|
"maybe missing -sub_charenc option\n");
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 05:30:12 +02:00
|
|
|
if (avpkt->data != pkt_recoded.data) { // did we recode?
|
2017-04-07 06:19:56 +02:00
|
|
|
/* prevent from destroying side data from original packet */
|
|
|
|
pkt_recoded.side_data = NULL;
|
|
|
|
pkt_recoded.side_data_elems = 0;
|
|
|
|
|
|
|
|
av_packet_unref(&pkt_recoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*got_sub_ptr)
|
|
|
|
avctx->frame_number++;
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *avctx,
|
|
|
|
const enum AVPixelFormat *fmt)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
2017-10-26 01:18:43 +02:00
|
|
|
const AVPixFmtDescriptor *desc;
|
|
|
|
const AVCodecHWConfig *config;
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
// If a device was supplied when the codec was opened, assume that the
|
|
|
|
// user wants to use it.
|
|
|
|
if (avctx->hw_device_ctx && avctx->codec->hw_configs) {
|
|
|
|
AVHWDeviceContext *device_ctx =
|
|
|
|
(AVHWDeviceContext*)avctx->hw_device_ctx->data;
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
config = &avctx->codec->hw_configs[i]->public;
|
|
|
|
if (!config)
|
|
|
|
break;
|
|
|
|
if (!(config->methods &
|
|
|
|
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
|
|
|
|
continue;
|
|
|
|
if (device_ctx->type != config->device_type)
|
|
|
|
continue;
|
|
|
|
for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
|
|
|
|
if (config->pix_fmt == fmt[n])
|
|
|
|
return fmt[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No device or other setup, so we have to choose from things which
|
|
|
|
// don't any other external information.
|
|
|
|
|
|
|
|
// If the last element of the list is a software format, choose it
|
|
|
|
// (this should be best software format if any exist).
|
|
|
|
for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
|
|
|
|
desc = av_pix_fmt_desc_get(fmt[n - 1]);
|
|
|
|
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
|
|
|
|
return fmt[n - 1];
|
|
|
|
|
|
|
|
// Finally, traverse the list in order and choose the first entry
|
|
|
|
// with no external dependencies (if there is no hardware configuration
|
|
|
|
// information available then this just picks the first entry).
|
|
|
|
for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++) {
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
config = avcodec_get_hw_config(avctx->codec, i);
|
|
|
|
if (!config)
|
|
|
|
break;
|
|
|
|
if (config->pix_fmt == fmt[n])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!config) {
|
|
|
|
// No specific config available, so the decoder must be able
|
|
|
|
// to handle this format without any additional setup.
|
|
|
|
return fmt[n];
|
|
|
|
}
|
|
|
|
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
|
|
|
|
// Usable with only internal setup.
|
|
|
|
return fmt[n];
|
|
|
|
}
|
2017-10-03 15:04:45 +02:00
|
|
|
}
|
2017-10-26 01:18:43 +02:00
|
|
|
|
|
|
|
// Nothing is usable, give up.
|
|
|
|
return AV_PIX_FMT_NONE;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 16:38:20 +02:00
|
|
|
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
|
|
|
|
enum AVHWDeviceType dev_type)
|
|
|
|
{
|
|
|
|
AVHWDeviceContext *device_ctx;
|
|
|
|
AVHWFramesContext *frames_ctx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!avctx->hwaccel)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
|
|
|
if (avctx->hw_frames_ctx)
|
|
|
|
return 0;
|
|
|
|
if (!avctx->hw_device_ctx) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
|
|
|
|
"required for hardware accelerated decoding.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
|
|
|
|
if (device_ctx->type != dev_type) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
|
|
|
|
"decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
|
|
|
|
av_hwdevice_get_type_name(device_ctx->type));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avcodec_get_hw_frames_parameters(avctx,
|
|
|
|
avctx->hw_device_ctx,
|
|
|
|
avctx->hwaccel->pix_fmt,
|
2017-11-11 23:58:12 +02:00
|
|
|
&avctx->hw_frames_ctx);
|
|
|
|
if (ret < 0)
|
2017-10-19 16:38:20 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
|
|
|
|
|
|
|
|
|
|
|
|
if (frames_ctx->initial_pool_size) {
|
|
|
|
// We guarantee 4 base work surfaces. The function above guarantees 1
|
|
|
|
// (the absolute minimum), so add the missing count.
|
|
|
|
frames_ctx->initial_pool_size += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_buffer_unref(&avctx->hw_frames_ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
|
|
|
|
AVBufferRef *device_ref,
|
|
|
|
enum AVPixelFormat hw_pix_fmt,
|
|
|
|
AVBufferRef **out_frames_ref)
|
|
|
|
{
|
|
|
|
AVBufferRef *frames_ref = NULL;
|
2017-10-26 01:18:43 +02:00
|
|
|
const AVCodecHWConfigInternal *hw_config;
|
|
|
|
const AVHWAccel *hwa;
|
|
|
|
int i, ret;
|
2017-10-19 16:38:20 +02:00
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
for (i = 0;; i++) {
|
|
|
|
hw_config = avctx->codec->hw_configs[i];
|
|
|
|
if (!hw_config)
|
|
|
|
return AVERROR(ENOENT);
|
|
|
|
if (hw_config->public.pix_fmt == hw_pix_fmt)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hwa = hw_config->hwaccel;
|
2017-10-19 16:38:20 +02:00
|
|
|
if (!hwa || !hwa->frame_params)
|
|
|
|
return AVERROR(ENOENT);
|
|
|
|
|
|
|
|
frames_ref = av_hwframe_ctx_alloc(device_ref);
|
|
|
|
if (!frames_ref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ret = hwa->frame_params(avctx, frames_ref);
|
|
|
|
if (ret >= 0) {
|
2018-01-30 00:18:00 +02:00
|
|
|
AVHWFramesContext *frames_ctx = (AVHWFramesContext*)frames_ref->data;
|
|
|
|
|
|
|
|
if (frames_ctx->initial_pool_size) {
|
|
|
|
// If the user has requested that extra output surfaces be
|
|
|
|
// available then add them here.
|
|
|
|
if (avctx->extra_hw_frames > 0)
|
|
|
|
frames_ctx->initial_pool_size += avctx->extra_hw_frames;
|
2018-01-30 00:55:11 +02:00
|
|
|
|
|
|
|
// If frame threading is enabled then an extra surface per thread
|
|
|
|
// is also required.
|
|
|
|
if (avctx->active_thread_type & FF_THREAD_FRAME)
|
|
|
|
frames_ctx->initial_pool_size += avctx->thread_count;
|
2018-01-30 00:18:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 16:38:20 +02:00
|
|
|
*out_frames_ref = frames_ref;
|
|
|
|
} else {
|
|
|
|
av_buffer_unref(&frames_ref);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
static int hwaccel_init(AVCodecContext *avctx,
|
|
|
|
const AVCodecHWConfigInternal *hw_config)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
2017-10-26 01:18:43 +02:00
|
|
|
const AVHWAccel *hwaccel;
|
|
|
|
int err;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
hwaccel = hw_config->hwaccel;
|
|
|
|
if (hwaccel->capabilities & AV_HWACCEL_CODEC_CAP_EXPERIMENTAL &&
|
2017-04-07 06:19:56 +02:00
|
|
|
avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Ignoring experimental hwaccel: %s\n",
|
2017-10-26 01:18:43 +02:00
|
|
|
hwaccel->name);
|
2017-04-07 06:19:56 +02:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
if (hwaccel->priv_data_size) {
|
|
|
|
avctx->internal->hwaccel_priv_data =
|
|
|
|
av_mallocz(hwaccel->priv_data_size);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!avctx->internal->hwaccel_priv_data)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:53:26 +02:00
|
|
|
avctx->hwaccel = hwaccel;
|
2017-10-26 01:18:43 +02:00
|
|
|
if (hwaccel->init) {
|
|
|
|
err = hwaccel->init(avctx);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed setup for format %s: "
|
|
|
|
"hwaccel initialisation returned error.\n",
|
|
|
|
av_get_pix_fmt_name(hw_config->public.pix_fmt));
|
2016-10-26 08:10:19 +02:00
|
|
|
av_freep(&avctx->internal->hwaccel_priv_data);
|
2017-06-06 18:51:08 +02:00
|
|
|
avctx->hwaccel = NULL;
|
2017-10-26 01:18:43 +02:00
|
|
|
return err;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
static void hwaccel_uninit(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
if (avctx->hwaccel && avctx->hwaccel->uninit)
|
|
|
|
avctx->hwaccel->uninit(avctx);
|
|
|
|
|
|
|
|
av_freep(&avctx->internal->hwaccel_priv_data);
|
|
|
|
|
|
|
|
avctx->hwaccel = NULL;
|
|
|
|
|
|
|
|
av_buffer_unref(&avctx->hw_frames_ctx);
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
|
|
|
|
{
|
|
|
|
const AVPixFmtDescriptor *desc;
|
|
|
|
enum AVPixelFormat *choices;
|
2017-10-26 01:18:43 +02:00
|
|
|
enum AVPixelFormat ret, user_choice;
|
|
|
|
const AVCodecHWConfigInternal *hw_config;
|
|
|
|
const AVCodecHWConfig *config;
|
|
|
|
int i, n, err;
|
|
|
|
|
|
|
|
// Find end of list.
|
|
|
|
for (n = 0; fmt[n] != AV_PIX_FMT_NONE; n++);
|
|
|
|
// Must contain at least one entry.
|
2016-10-26 08:10:19 +02:00
|
|
|
av_assert0(n >= 1);
|
2017-10-26 01:18:43 +02:00
|
|
|
// If a software format is available, it must be the last entry.
|
|
|
|
desc = av_pix_fmt_desc_get(fmt[n - 1]);
|
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
|
|
|
|
// No software format is available.
|
|
|
|
} else {
|
|
|
|
avctx->sw_pix_fmt = fmt[n - 1];
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
choices = av_malloc_array(n + 1, sizeof(*choices));
|
|
|
|
if (!choices)
|
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
|
|
|
|
|
memcpy(choices, fmt, (n + 1) * sizeof(*choices));
|
|
|
|
|
|
|
|
for (;;) {
|
2017-10-26 01:18:43 +02:00
|
|
|
// Remove the previous hwaccel, if there was one.
|
|
|
|
hwaccel_uninit(avctx);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
user_choice = avctx->get_format(avctx, choices);
|
|
|
|
if (user_choice == AV_PIX_FMT_NONE) {
|
|
|
|
// Explicitly chose nothing, give up.
|
|
|
|
ret = AV_PIX_FMT_NONE;
|
|
|
|
break;
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
desc = av_pix_fmt_desc_get(user_choice);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!desc) {
|
2017-10-26 01:18:43 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid format returned by "
|
|
|
|
"get_format() callback.\n");
|
2016-10-26 08:10:19 +02:00
|
|
|
ret = AV_PIX_FMT_NONE;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-26 01:18:43 +02:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Format %s chosen by get_format().\n",
|
|
|
|
desc->name);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (choices[i] == user_choice)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == n) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid return from get_format(): "
|
|
|
|
"%s not in possible list.\n", desc->name);
|
2018-12-07 00:17:13 +02:00
|
|
|
ret = AV_PIX_FMT_NONE;
|
2016-10-26 08:10:19 +02:00
|
|
|
break;
|
2017-10-26 01:18:43 +02:00
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
if (avctx->codec->hw_configs) {
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
hw_config = avctx->codec->hw_configs[i];
|
|
|
|
if (!hw_config)
|
|
|
|
break;
|
|
|
|
if (hw_config->public.pix_fmt == user_choice)
|
|
|
|
break;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
2017-10-26 01:18:43 +02:00
|
|
|
} else {
|
|
|
|
hw_config = NULL;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 01:18:43 +02:00
|
|
|
if (!hw_config) {
|
|
|
|
// No config available, so no extra setup required.
|
|
|
|
ret = user_choice;
|
2016-10-26 08:10:19 +02:00
|
|
|
break;
|
2017-10-26 01:18:43 +02:00
|
|
|
}
|
|
|
|
config = &hw_config->public;
|
|
|
|
|
|
|
|
if (config->methods &
|
|
|
|
AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
|
|
|
|
avctx->hw_frames_ctx) {
|
|
|
|
const AVHWFramesContext *frames_ctx =
|
|
|
|
(AVHWFramesContext*)avctx->hw_frames_ctx->data;
|
|
|
|
if (frames_ctx->format != user_choice) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
|
|
|
|
"does not match the format of the provided frames "
|
|
|
|
"context.\n", desc->name);
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
} else if (config->methods &
|
|
|
|
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
|
|
|
|
avctx->hw_device_ctx) {
|
|
|
|
const AVHWDeviceContext *device_ctx =
|
|
|
|
(AVHWDeviceContext*)avctx->hw_device_ctx->data;
|
|
|
|
if (device_ctx->type != config->device_type) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
|
|
|
|
"does not match the type of the provided device "
|
|
|
|
"context.\n", desc->name);
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
} else if (config->methods &
|
|
|
|
AV_CODEC_HW_CONFIG_METHOD_INTERNAL) {
|
|
|
|
// Internal-only setup, no additional configuration.
|
|
|
|
} else if (config->methods &
|
|
|
|
AV_CODEC_HW_CONFIG_METHOD_AD_HOC) {
|
|
|
|
// Some ad-hoc configuration we can't see and can't check.
|
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid setup for format %s: "
|
|
|
|
"missing configuration.\n", desc->name);
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
if (hw_config->hwaccel) {
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Format %s requires hwaccel "
|
|
|
|
"initialisation.\n", desc->name);
|
|
|
|
err = hwaccel_init(avctx, hw_config);
|
|
|
|
if (err < 0)
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
ret = user_choice;
|
|
|
|
break;
|
|
|
|
|
|
|
|
try_again:
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Format %s not usable, retrying "
|
|
|
|
"get_format() without it.\n", desc->name);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (choices[i] == user_choice)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (; i + 1 < n; i++)
|
|
|
|
choices[i] = choices[i + 1];
|
|
|
|
--n;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&choices);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-17 17:28:30 +02:00
|
|
|
static void frame_pool_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
FramePool *pool = (FramePool*)data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
|
|
|
|
av_buffer_pool_uninit(&pool->pools[i]);
|
|
|
|
|
|
|
|
av_freep(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static AVBufferRef *frame_pool_alloc(void)
|
|
|
|
{
|
|
|
|
FramePool *pool = av_mallocz(sizeof(*pool));
|
|
|
|
AVBufferRef *buf;
|
|
|
|
|
|
|
|
if (!pool)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buf = av_buffer_create((uint8_t*)pool, sizeof(*pool),
|
|
|
|
frame_pool_free, NULL, 0);
|
|
|
|
if (!buf) {
|
|
|
|
av_freep(&pool);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
2017-01-17 17:28:30 +02:00
|
|
|
FramePool *pool = avctx->internal->pool ?
|
|
|
|
(FramePool*)avctx->internal->pool->data : NULL;
|
|
|
|
AVBufferRef *pool_buf;
|
|
|
|
int i, ret, ch, planes;
|
|
|
|
|
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
int planar = av_sample_fmt_is_planar(frame->format);
|
|
|
|
ch = frame->channels;
|
|
|
|
planes = planar ? ch : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pool && pool->format == frame->format) {
|
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
pool->width == frame->width && pool->height == frame->height)
|
|
|
|
return 0;
|
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && pool->planes == planes &&
|
|
|
|
pool->channels == ch && frame->nb_samples == pool->samples)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool_buf = frame_pool_alloc();
|
|
|
|
if (!pool_buf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
pool = (FramePool*)pool_buf->data;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
switch (avctx->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO: {
|
|
|
|
uint8_t *data[4];
|
|
|
|
int linesize[4];
|
|
|
|
int size[4] = { 0 };
|
|
|
|
int w = frame->width;
|
|
|
|
int h = frame->height;
|
|
|
|
int tmpsize, unaligned;
|
|
|
|
|
|
|
|
avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
|
|
|
|
|
|
|
|
do {
|
|
|
|
// NOTE: do not align linesizes individually, this breaks e.g. assumptions
|
|
|
|
// that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
|
2017-04-07 06:19:56 +02:00
|
|
|
ret = av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
|
|
|
|
if (ret < 0)
|
2017-01-17 17:28:30 +02:00
|
|
|
goto fail;
|
2016-10-26 08:10:19 +02:00
|
|
|
// increase alignment of w for next try (rhs gives the lowest bit set in w)
|
|
|
|
w += w & ~(w - 1);
|
|
|
|
|
|
|
|
unaligned = 0;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
unaligned |= linesize[i] % pool->stride_align[i];
|
|
|
|
} while (unaligned);
|
|
|
|
|
|
|
|
tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
|
|
|
|
NULL, linesize);
|
2017-01-17 17:28:30 +02:00
|
|
|
if (tmpsize < 0) {
|
|
|
|
ret = tmpsize;
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 3 && data[i + 1]; i++)
|
|
|
|
size[i] = data[i + 1] - data[i];
|
|
|
|
size[i] = tmpsize - (data[i] - data[0]);
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
pool->linesize[i] = linesize[i];
|
|
|
|
if (size[i]) {
|
2017-04-07 06:19:56 +02:00
|
|
|
pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
|
|
|
|
CONFIG_MEMORY_POISONING ?
|
|
|
|
NULL :
|
|
|
|
av_buffer_allocz);
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!pool->pools[i]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pool->format = frame->format;
|
|
|
|
pool->width = frame->width;
|
|
|
|
pool->height = frame->height;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AVMEDIA_TYPE_AUDIO: {
|
|
|
|
ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
|
|
|
|
frame->nb_samples, frame->format, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
|
|
|
|
if (!pool->pools[0]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->format = frame->format;
|
|
|
|
pool->planes = planes;
|
|
|
|
pool->channels = ch;
|
|
|
|
pool->samples = frame->nb_samples;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: av_assert0(0);
|
|
|
|
}
|
2017-01-17 17:28:30 +02:00
|
|
|
|
|
|
|
av_buffer_unref(&avctx->internal->pool);
|
|
|
|
avctx->internal->pool = pool_buf;
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
return 0;
|
|
|
|
fail:
|
2017-01-17 17:28:30 +02:00
|
|
|
av_buffer_unref(&pool_buf);
|
2016-10-26 08:10:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
2017-01-17 17:28:30 +02:00
|
|
|
FramePool *pool = (FramePool*)avctx->internal->pool->data;
|
2016-10-26 08:10:19 +02:00
|
|
|
int planes = pool->planes;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
frame->linesize[0] = pool->linesize[0];
|
|
|
|
|
|
|
|
if (planes > AV_NUM_DATA_POINTERS) {
|
2017-04-07 06:19:56 +02:00
|
|
|
frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
|
2016-10-26 08:10:19 +02:00
|
|
|
frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
|
2017-04-07 06:19:56 +02:00
|
|
|
frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
|
2016-10-26 08:10:19 +02:00
|
|
|
sizeof(*frame->extended_buf));
|
|
|
|
if (!frame->extended_data || !frame->extended_buf) {
|
|
|
|
av_freep(&frame->extended_data);
|
|
|
|
av_freep(&frame->extended_buf);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
} else {
|
2016-10-26 08:10:19 +02:00
|
|
|
frame->extended_data = frame->data;
|
2017-04-07 06:19:56 +02:00
|
|
|
av_assert0(frame->nb_extended_buf == 0);
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
|
|
|
|
frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
|
|
|
|
if (!frame->buf[i])
|
|
|
|
goto fail;
|
|
|
|
frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
|
|
|
|
}
|
|
|
|
for (i = 0; i < frame->nb_extended_buf; i++) {
|
|
|
|
frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
|
|
|
|
if (!frame->extended_buf[i])
|
|
|
|
goto fail;
|
|
|
|
frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->debug & FF_DEBUG_BUFFERS)
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
|
|
|
|
{
|
2017-01-17 17:28:30 +02:00
|
|
|
FramePool *pool = (FramePool*)s->internal->pool->data;
|
2017-04-07 06:19:56 +02:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
|
2016-10-26 08:10:19 +02:00
|
|
|
int i;
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (pic->data[0] || pic->data[1] || pic->data[2] || pic->data[3]) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "pic->data[*]!=NULL in avcodec_default_get_buffer\n");
|
2016-10-26 08:10:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (!desc) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Unable to get pixel format descriptor for format %s\n",
|
|
|
|
av_get_pix_fmt_name(pic->format));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
memset(pic->data, 0, sizeof(pic->data));
|
|
|
|
pic->extended_data = pic->data;
|
|
|
|
|
|
|
|
for (i = 0; i < 4 && pool->pools[i]; i++) {
|
|
|
|
pic->linesize[i] = pool->linesize[i];
|
|
|
|
|
|
|
|
pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
|
|
|
|
if (!pic->buf[i])
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
pic->data[i] = pic->buf[i]->data;
|
|
|
|
}
|
|
|
|
for (; i < AV_NUM_DATA_POINTERS; i++) {
|
|
|
|
pic->data[i] = NULL;
|
|
|
|
pic->linesize[i] = 0;
|
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
|
avutil/pixdesc: deprecate AV_PIX_FMT_FLAG_PSEUDOPAL
PSEUDOPAL pixel formats are not paletted, but carried a palette with the
intention of allowing code to treat unpaletted formats as paletted. The
palette simply mapped the byte values to the resulting RGB values,
making it some sort of LUT for RGB conversion.
It was used for 1 byte formats only: RGB4_BYTE, BGR4_BYTE, RGB8, BGR8,
GRAY8. The first 4 are awfully obscure, used only by some ancient bitmap
formats. The last one, GRAY8, is more common, but its treatment is
grossly incorrect. It considers full range GRAY8 only, so GRAY8 coming
from typical Y video planes was not mapped to the correct RGB values.
This cannot be fixed, because AVFrame.color_range can be freely changed
at runtime, and there is nothing to ensure the pseudo palette is
updated.
Also, nothing actually used the PSEUDOPAL palette data, except xwdenc
(trivially changed in the previous commit). All other code had to treat
it as a special case, just to ignore or to propagate palette data.
In conclusion, this was just a very strange old mechnaism that has no
real justification to exist anymore (although it may have been nice and
useful in the past). Now it's an artifact that makes the API harder to
use: API users who allocate their own pixel data have to be aware that
they need to allocate the palette, or FFmpeg will crash on them in
_some_ situations. On top of this, there was no API to allocate the
pseuo palette outside of av_frame_get_buffer().
This patch not only deprecates AV_PIX_FMT_FLAG_PSEUDOPAL, but also makes
the pseudo palette optional. Nothing accesses it anymore, though if it's
set, it's propagated. It's still allocated and initialized for
compatibility with API users that rely on this feature. But new API
users do not need to allocate it. This was an explicit goal of this
patch.
Most changes replace AV_PIX_FMT_FLAG_PSEUDOPAL with FF_PSEUDOPAL. I
first tried #ifdefing all code, but it was a mess. The FF_PSEUDOPAL
macro reduces the mess, and still allows defining FF_API_PSEUDOPAL to 0.
Passes FATE with FF_API_PSEUDOPAL enabled and disabled. In addition,
FATE passes with FF_API_PSEUDOPAL set to 1, but with allocation
functions manually changed to not allocating a palette.
2018-03-29 15:18:28 +02:00
|
|
|
((desc->flags & FF_PSEUDOPAL) && pic->data[1]))
|
2017-04-07 06:19:56 +02:00
|
|
|
avpriv_set_systematic_pal2((uint32_t *)pic->data[1], pic->format);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
if (s->debug & FF_DEBUG_BUFFERS)
|
|
|
|
av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_frame_unref(pic);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-06-22 20:05:12 +02:00
|
|
|
if (avctx->hw_frames_ctx) {
|
|
|
|
ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, frame, 0);
|
|
|
|
frame->width = avctx->coded_width;
|
|
|
|
frame->height = avctx->coded_height;
|
|
|
|
return ret;
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
if ((ret = update_frame_pool(avctx, frame)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (avctx->codec_type) {
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
|
|
|
return video_get_buffer(avctx, frame);
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
return audio_get_buffer(avctx, frame);
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
static int add_metadata_from_side_data(const AVPacket *avpkt, AVFrame *frame)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
2017-04-07 06:19:56 +02:00
|
|
|
int size;
|
|
|
|
const uint8_t *side_metadata;
|
|
|
|
|
2017-04-22 10:54:58 +02:00
|
|
|
AVDictionary **frame_md = &frame->metadata;
|
2017-04-07 06:19:56 +02:00
|
|
|
|
|
|
|
side_metadata = av_packet_get_side_data(avpkt,
|
|
|
|
AV_PKT_DATA_STRINGS_METADATA, &size);
|
|
|
|
return av_packet_unpack_dictionary(side_metadata, size, frame_md);
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:42:04 +02:00
|
|
|
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
|
2017-04-07 06:19:56 +02:00
|
|
|
{
|
2017-04-23 01:06:47 +02:00
|
|
|
const AVPacket *pkt = avctx->internal->last_pkt_props;
|
2016-10-26 08:10:19 +02:00
|
|
|
int i;
|
2017-04-07 06:19:56 +02:00
|
|
|
static const struct {
|
2016-10-26 08:10:19 +02:00
|
|
|
enum AVPacketSideDataType packet;
|
|
|
|
enum AVFrameSideDataType frame;
|
|
|
|
} sd[] = {
|
2017-04-07 06:19:56 +02:00
|
|
|
{ AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
|
|
|
|
{ AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
|
|
|
|
{ AV_PKT_DATA_SPHERICAL, AV_FRAME_DATA_SPHERICAL },
|
|
|
|
{ AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
|
|
|
|
{ AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
|
|
|
|
{ AV_PKT_DATA_MASTERING_DISPLAY_METADATA, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA },
|
|
|
|
{ AV_PKT_DATA_CONTENT_LIGHT_LEVEL, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL },
|
2017-08-31 13:10:05 +02:00
|
|
|
{ AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
|
2019-09-23 22:43:02 +02:00
|
|
|
{ AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
|
2016-10-26 08:10:19 +02:00
|
|
|
};
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (pkt) {
|
|
|
|
frame->pts = pkt->pts;
|
2016-10-26 08:10:19 +02:00
|
|
|
#if FF_API_PKT_PTS
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2017-04-07 06:19:56 +02:00
|
|
|
frame->pkt_pts = pkt->pts;
|
2016-10-26 08:10:19 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->pkt_pos = pkt->pos;
|
|
|
|
frame->pkt_duration = pkt->duration;
|
|
|
|
frame->pkt_size = pkt->size;
|
2017-04-07 06:19:56 +02:00
|
|
|
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
|
|
|
|
int size;
|
|
|
|
uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
|
|
|
|
if (packet_sd) {
|
|
|
|
AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
|
|
|
|
sd[i].frame,
|
|
|
|
size);
|
|
|
|
if (!frame_sd)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
memcpy(frame_sd->data, packet_sd, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_metadata_from_side_data(pkt, frame);
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (pkt->flags & AV_PKT_FLAG_DISCARD) {
|
|
|
|
frame->flags |= AV_FRAME_FLAG_DISCARD;
|
|
|
|
} else {
|
|
|
|
frame->flags = (frame->flags & ~AV_FRAME_FLAG_DISCARD);
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
frame->reordered_opaque = avctx->reordered_opaque;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
|
|
|
|
frame->color_primaries = avctx->color_primaries;
|
|
|
|
if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
|
|
|
|
frame->color_trc = avctx->color_trc;
|
2017-04-22 10:54:58 +02:00
|
|
|
if (frame->colorspace == AVCOL_SPC_UNSPECIFIED)
|
|
|
|
frame->colorspace = avctx->colorspace;
|
|
|
|
if (frame->color_range == AVCOL_RANGE_UNSPECIFIED)
|
|
|
|
frame->color_range = avctx->color_range;
|
2017-04-07 06:19:56 +02:00
|
|
|
if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
|
|
|
|
frame->chroma_location = avctx->chroma_sample_location;
|
|
|
|
|
|
|
|
switch (avctx->codec->type) {
|
2016-10-26 08:10:19 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2017-04-07 06:19:56 +02:00
|
|
|
frame->format = avctx->pix_fmt;
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!frame->sample_aspect_ratio.num)
|
|
|
|
frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (frame->width && frame->height &&
|
|
|
|
av_image_check_sar(frame->width, frame->height,
|
2016-10-26 08:10:19 +02:00
|
|
|
frame->sample_aspect_ratio) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
|
|
|
|
frame->sample_aspect_ratio.num,
|
|
|
|
frame->sample_aspect_ratio.den);
|
|
|
|
frame->sample_aspect_ratio = (AVRational){ 0, 1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
|
|
|
if (!frame->sample_rate)
|
|
|
|
frame->sample_rate = avctx->sample_rate;
|
|
|
|
if (frame->format < 0)
|
|
|
|
frame->format = avctx->sample_fmt;
|
|
|
|
if (!frame->channel_layout) {
|
|
|
|
if (avctx->channel_layout) {
|
|
|
|
if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
|
|
|
|
avctx->channels) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
|
|
|
|
"configuration.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->channel_layout = avctx->channel_layout;
|
|
|
|
} else {
|
|
|
|
if (avctx->channels > FF_SANE_NB_CHANNELS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
|
|
|
|
avctx->channels);
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 10:54:58 +02:00
|
|
|
frame->channels = avctx->channels;
|
2016-10-26 08:10:19 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void validate_avframe_allocation(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
int i;
|
|
|
|
int num_planes = av_pix_fmt_count_planes(frame->format);
|
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
|
|
|
|
int flags = desc ? desc->flags : 0;
|
|
|
|
if (num_planes == 1 && (flags & AV_PIX_FMT_FLAG_PAL))
|
|
|
|
num_planes = 2;
|
2018-04-04 18:07:03 +02:00
|
|
|
if ((flags & FF_PSEUDOPAL) && frame->data[1])
|
|
|
|
num_planes = 2;
|
2017-04-07 06:19:56 +02:00
|
|
|
for (i = 0; i < num_planes; i++) {
|
|
|
|
av_assert0(frame->data[i]);
|
|
|
|
}
|
|
|
|
// For formats without data like hwaccel allow unused pointers to be non-NULL.
|
|
|
|
for (i = num_planes; num_planes > 0 && i < FF_ARRAY_ELEMS(frame->data); i++) {
|
|
|
|
if (frame->data[i])
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Buffer returned by get_buffer2() did not zero unused plane pointers\n");
|
|
|
|
frame->data[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2017-10-13 18:59:17 +02:00
|
|
|
static void decode_data_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
FrameDecodeData *fdd = (FrameDecodeData*)data;
|
|
|
|
|
2017-11-10 17:07:44 +02:00
|
|
|
if (fdd->post_process_opaque_free)
|
|
|
|
fdd->post_process_opaque_free(fdd->post_process_opaque);
|
|
|
|
|
2017-10-13 18:59:19 +02:00
|
|
|
if (fdd->hwaccel_priv_free)
|
|
|
|
fdd->hwaccel_priv_free(fdd->hwaccel_priv);
|
|
|
|
|
2017-10-13 18:59:17 +02:00
|
|
|
av_freep(&fdd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_attach_decode_data(AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVBufferRef *fdd_buf;
|
|
|
|
FrameDecodeData *fdd;
|
|
|
|
|
|
|
|
av_assert1(!frame->private_ref);
|
|
|
|
av_buffer_unref(&frame->private_ref);
|
|
|
|
|
|
|
|
fdd = av_mallocz(sizeof(*fdd));
|
|
|
|
if (!fdd)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
fdd_buf = av_buffer_create((uint8_t*)fdd, sizeof(*fdd), decode_data_free,
|
|
|
|
NULL, AV_BUFFER_FLAG_READONLY);
|
|
|
|
if (!fdd_buf) {
|
|
|
|
av_freep(&fdd);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->private_ref = fdd_buf;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-05 10:48:33 +02:00
|
|
|
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
|
2017-04-07 06:19:56 +02:00
|
|
|
{
|
|
|
|
const AVHWAccel *hwaccel = avctx->hwaccel;
|
|
|
|
int override_dimensions = 1;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2018-06-14 15:00:55 +02:00
|
|
|
if ((ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
|
2017-04-07 06:19:56 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
|
2020-04-05 10:48:33 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frame->width <= 0 || frame->height <= 0) {
|
|
|
|
frame->width = FFMAX(avctx->width, AV_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
|
|
|
|
frame->height = FFMAX(avctx->height, AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
|
|
|
|
override_dimensions = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame->data[0] || frame->data[1] || frame->data[2] || frame->data[3]) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "pic->data[*]!=NULL in get_buffer_internal\n");
|
2020-04-05 10:48:33 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
2019-09-25 16:18:32 +02:00
|
|
|
} else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2019-11-20 22:13:01 +02:00
|
|
|
if (frame->nb_samples * (int64_t)avctx->channels > avctx->max_samples) {
|
2019-09-25 16:18:32 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "samples per frame %d, exceeds max_samples %"PRId64"\n", frame->nb_samples, avctx->max_samples);
|
2020-04-05 10:48:33 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
2019-09-25 16:18:32 +02:00
|
|
|
}
|
2017-04-07 06:19:56 +02:00
|
|
|
}
|
2016-10-26 08:10:19 +02:00
|
|
|
ret = ff_decode_frame_props(avctx, frame);
|
|
|
|
if (ret < 0)
|
2020-04-05 10:48:33 +02:00
|
|
|
goto fail;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
|
|
|
if (hwaccel) {
|
|
|
|
if (hwaccel->alloc_frame) {
|
|
|
|
ret = hwaccel->alloc_frame(avctx, frame);
|
2020-04-05 10:48:33 +02:00
|
|
|
goto fail;
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
avctx->sw_pix_fmt = avctx->pix_fmt;
|
|
|
|
|
|
|
|
ret = avctx->get_buffer2(avctx, frame, flags);
|
2017-10-13 18:59:17 +02:00
|
|
|
if (ret < 0)
|
2020-04-05 10:48:33 +02:00
|
|
|
goto fail;
|
2017-10-13 18:59:17 +02:00
|
|
|
|
|
|
|
validate_avframe_allocation(avctx, frame);
|
|
|
|
|
|
|
|
ret = ff_attach_decode_data(frame);
|
|
|
|
if (ret < 0)
|
2020-04-05 10:48:33 +02:00
|
|
|
goto fail;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2016-12-18 17:42:24 +02:00
|
|
|
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions &&
|
|
|
|
!(avctx->codec->caps_internal & FF_CODEC_CAP_EXPORTS_CROPPING)) {
|
2016-10-26 08:10:19 +02:00
|
|
|
frame->width = avctx->width;
|
|
|
|
frame->height = avctx->height;
|
|
|
|
}
|
|
|
|
|
2020-04-05 10:48:33 +02:00
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
|
|
av_frame_unref(frame);
|
2017-04-07 06:19:56 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
|
2016-10-26 08:10:19 +02:00
|
|
|
{
|
|
|
|
AVFrame *tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
|
|
|
|
frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
|
|
|
|
av_frame_unref(frame);
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!frame->data[0])
|
|
|
|
return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
|
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
if ((flags & FF_REGET_BUFFER_FLAG_READONLY) || av_frame_is_writable(frame))
|
2016-10-26 08:10:19 +02:00
|
|
|
return ff_decode_frame_props(avctx, frame);
|
|
|
|
|
|
|
|
tmp = av_frame_alloc();
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
av_frame_move_ref(tmp, frame);
|
|
|
|
|
|
|
|
ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_copy(frame, tmp);
|
|
|
|
av_frame_free(&tmp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
|
2017-04-07 06:19:56 +02:00
|
|
|
{
|
2019-08-30 16:37:25 +02:00
|
|
|
int ret = reget_buffer_internal(avctx, frame, flags);
|
2017-04-07 06:19:56 +02:00
|
|
|
if (ret < 0)
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
void avcodec_flush_buffers(AVCodecContext *avctx)
|
|
|
|
{
|
2020-01-06 12:31:03 +02:00
|
|
|
AVCodecInternal *avci = avctx->internal;
|
|
|
|
|
avcodec: Add explicit capability flag for encoder flushing
Previously, there was no way to flush an encoder such that after
draining, the encoder could be used again. We generally suggested
that clients teardown and replace the encoder instance in these
situations. However, for at least some hardware encoders, the cost of
this tear down/replace cycle is very high, which can get in the way of
some use-cases - for example: segmented encoding with nvenc.
To help address that use case, we added support for calling
avcodec_flush_buffers() to nvenc and things worked in practice,
although it was not clearly documented as to whether this should work
or not. There was only one previous example of an encoder implementing
the flush callback (audiotoolboxenc) and it's unclear if that was
intentional or not. However, it was clear that calling
avocdec_flush_buffers() on any other encoder would leave the encoder in
an undefined state, and that's not great.
As part of cleaning this up, this change introduces a formal capability
flag for encoders that support flushing and ensures a flush call is a
no-op for any other encoder. This allows client code to check if it is
meaningful to call flush on an encoder before actually doing it.
I have not attempted to separate the steps taken inside
avcodec_flush_buffers() because it's not doing anything that's wrong
for an encoder. But I did add a sanity check to reject attempts to
flush a frame threaded encoder because I couldn't wrap my head around
whether that code path was actually safe or not. As this combination
doesn't exist today, we'll deal with it if it ever comes up.
2020-04-10 22:32:11 +02:00
|
|
|
if (av_codec_is_encoder(avctx->codec)) {
|
|
|
|
int caps = avctx->codec->capabilities;
|
|
|
|
|
|
|
|
if (!(caps & AV_CODEC_CAP_ENCODER_FLUSH)) {
|
|
|
|
// Only encoders that explicitly declare support for it can be
|
|
|
|
// flushed. Otherwise, this is a no-op.
|
|
|
|
av_log(avctx, AV_LOG_WARNING, "Ignoring attempt to flush encoder "
|
|
|
|
"that doesn't support it\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We haven't implemented flushing for frame-threaded encoders.
|
|
|
|
av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:31:03 +02:00
|
|
|
avci->draining = 0;
|
|
|
|
avci->draining_done = 0;
|
|
|
|
avci->nb_draining_errors = 0;
|
|
|
|
av_frame_unref(avci->buffer_frame);
|
|
|
|
av_frame_unref(avci->compat_decode_frame);
|
|
|
|
av_packet_unref(avci->buffer_pkt);
|
|
|
|
avci->buffer_pkt_valid = 0;
|
2016-10-26 08:10:19 +02:00
|
|
|
|
2020-01-06 12:31:03 +02:00
|
|
|
av_packet_unref(avci->ds.in_pkt);
|
decode: restructure the core decoding code
Currently, the new decoding API is pretty much just a wrapper around the
old deprecated one. This is problematic, since it interferes with making
full use of the flexibility added by the new API. The old API should
also be removed at some future point.
Reorganize the code so that the new send_packet/receive_frame functions
call the actual decoding directly and change the old deprecated
avcodec_decode_* functions into wrappers around the new API.
The new internal API for decoders is now changing as well. Before this
commit, it mirrors the public API, so the decoders need to implement
send_packet() and receive_frame() callbacks. This turns out to require
awkward constructs in both the decoders and the generic code. After this
commit, the decoders only implement the receive_frame() callback and
call a new internal function, ff_decode_get_packet() to obtain input
data, in the same manner to how the bitstream filters now work.
avcodec will now always make a reference to the input packet, which means
that non-refcounted input packets will be copied. Keeping the previous
behaviour, where this copy could sometimes be avoided, would make the
code significantly more complex and fragile for only dubious gains,
since packets are typically small and everyone who cares about
performance should use refcounted packets anyway.
2016-10-26 13:59:15 +02:00
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
|
|
|
|
ff_thread_flush(avctx);
|
|
|
|
else if (avctx->codec->flush)
|
|
|
|
avctx->codec->flush(avctx);
|
|
|
|
|
2017-04-07 06:19:56 +02:00
|
|
|
avctx->pts_correction_last_pts =
|
|
|
|
avctx->pts_correction_last_dts = INT64_MIN;
|
|
|
|
|
2020-04-25 23:48:21 +02:00
|
|
|
if (av_codec_is_decoder(avctx->codec))
|
|
|
|
av_bsf_flush(avci->bsf);
|
2016-11-12 18:43:55 +02:00
|
|
|
|
2016-10-26 08:10:19 +02:00
|
|
|
if (!avctx->refcounted_frames)
|
2020-01-06 12:31:03 +02:00
|
|
|
av_frame_unref(avci->to_free);
|
2016-10-26 08:10:19 +02:00
|
|
|
}
|
2016-11-12 18:43:55 +02:00
|
|
|
|
|
|
|
void ff_decode_bsfs_uninit(AVCodecContext *avctx)
|
|
|
|
{
|
2020-04-25 23:48:21 +02:00
|
|
|
av_bsf_free(&avctx->internal->bsf);
|
2016-11-12 18:43:55 +02:00
|
|
|
}
|