#include "libavutil/thread.h"
#include "avcodec.h"
#include "decode.h"
-#include "hwaccel.h"
+#include "hwconfig.h"
#include "libavutil/opt.h"
#include "mpegvideo.h"
#include "thread.h"
#include "frame_thread_encoder.h"
#include "internal.h"
+#include "put_bits.h"
#include "raw.h"
#include "bytestream.h"
#include "version.h"
int av_codec_is_encoder(const AVCodec *codec)
{
- return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
+ return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
}
int av_codec_is_decoder(const AVCodec *codec)
int codec_init_ok = 0;
AVDictionary *tmp = NULL;
const AVPixFmtDescriptor *pixdesc;
+ AVCodecInternal *avci;
if (avcodec_is_open(avctx))
return 0;
- if ((!codec && !avctx->codec)) {
+ if (!codec && !avctx->codec) {
av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
return AVERROR(EINVAL);
}
- if ((codec && avctx->codec && codec != avctx->codec)) {
+ if (codec && avctx->codec && codec != avctx->codec) {
av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
"but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
return AVERROR(EINVAL);
ff_lock_avcodec(avctx, codec);
- avctx->internal = av_mallocz(sizeof(*avctx->internal));
- if (!avctx->internal) {
+ avci = av_mallocz(sizeof(*avci));
+ if (!avci) {
ret = AVERROR(ENOMEM);
goto end;
}
-
- avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
- if (!avctx->internal->pool) {
- ret = AVERROR(ENOMEM);
- goto free_and_end;
- }
-
- avctx->internal->to_free = av_frame_alloc();
- if (!avctx->internal->to_free) {
- ret = AVERROR(ENOMEM);
- goto free_and_end;
- }
-
- avctx->internal->compat_decode_frame = av_frame_alloc();
- if (!avctx->internal->compat_decode_frame) {
- ret = AVERROR(ENOMEM);
- goto free_and_end;
- }
-
- avctx->internal->buffer_frame = av_frame_alloc();
- if (!avctx->internal->buffer_frame) {
- ret = AVERROR(ENOMEM);
- goto free_and_end;
- }
-
- avctx->internal->buffer_pkt = av_packet_alloc();
- if (!avctx->internal->buffer_pkt) {
- ret = AVERROR(ENOMEM);
- goto free_and_end;
- }
-
- avctx->internal->ds.in_pkt = av_packet_alloc();
- if (!avctx->internal->ds.in_pkt) {
+ avctx->internal = avci;
+
+ avci->to_free = av_frame_alloc();
+ avci->compat_decode_frame = av_frame_alloc();
+ avci->compat_encode_packet = av_packet_alloc();
+ avci->buffer_frame = av_frame_alloc();
+ avci->buffer_pkt = av_packet_alloc();
+ avci->es.in_frame = av_frame_alloc();
+ avci->ds.in_pkt = av_packet_alloc();
+ avci->last_pkt_props = av_packet_alloc();
+ if (!avci->compat_decode_frame || !avci->compat_encode_packet ||
+ !avci->buffer_frame || !avci->buffer_pkt ||
+ !avci->es.in_frame || !avci->ds.in_pkt ||
+ !avci->to_free || !avci->last_pkt_props) {
ret = AVERROR(ENOMEM);
goto free_and_end;
}
- avctx->internal->last_pkt_props = av_packet_alloc();
- if (!avctx->internal->last_pkt_props) {
- ret = AVERROR(ENOMEM);
- goto free_and_end;
- }
-
- avctx->internal->skip_samples_multiplier = 1;
+ avci->skip_samples_multiplier = 1;
if (codec->priv_data_size > 0) {
if (!avctx->priv_data) {
}
if (HAVE_THREADS
- && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
+ && !(avci->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
ret = ff_thread_init(avctx);
if (ret < 0) {
goto free_and_end;
&& avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
av_log(avctx, AV_LOG_WARNING,
"gray decoding requested but not enabled at configuration time\n");
+ if (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS) {
+ avctx->export_side_data |= AV_CODEC_EXPORT_DATA_MVS;
+ }
if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
- || avctx->internal->frame_thread_encoder)) {
+ || avci->frame_thread_encoder)) {
ret = avctx->codec->init(avctx);
if (ret < 0) {
goto free_and_end;
(avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
avctx->codec->close(avctx);
- if (HAVE_THREADS && avctx->internal->thread_ctx)
+ if (HAVE_THREADS && avci->thread_ctx)
ff_thread_free(avctx);
if (codec->priv_class && codec->priv_data_size)
av_dict_free(&tmp);
av_freep(&avctx->priv_data);
av_freep(&avctx->subtitle_header);
- if (avctx->internal) {
- av_frame_free(&avctx->internal->to_free);
- av_frame_free(&avctx->internal->compat_decode_frame);
- av_frame_free(&avctx->internal->buffer_frame);
- av_packet_free(&avctx->internal->buffer_pkt);
- av_packet_free(&avctx->internal->last_pkt_props);
+ if (avci) {
+ av_frame_free(&avci->to_free);
+ av_frame_free(&avci->compat_decode_frame);
+ av_frame_free(&avci->buffer_frame);
+ av_packet_free(&avci->compat_encode_packet);
+ av_packet_free(&avci->buffer_pkt);
+ av_packet_free(&avci->last_pkt_props);
+
+ av_packet_free(&avci->ds.in_pkt);
+ av_frame_free(&avci->es.in_frame);
+ av_bsf_free(&avci->bsf);
+
+ av_buffer_unref(&avci->pool);
+ }
+ av_freep(&avci);
+ avctx->internal = NULL;
+ avctx->codec = NULL;
+ goto end;
+}
- av_packet_free(&avctx->internal->ds.in_pkt);
- ff_decode_bsfs_uninit(avctx);
+void avcodec_flush_buffers(AVCodecContext *avctx)
+{
+ AVCodecInternal *avci = avctx->internal;
+
+ 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;
+ }
- av_freep(&avctx->internal->pool);
+ // We haven't implemented flushing for frame-threaded encoders.
+ av_assert0(!(caps & AV_CODEC_CAP_FRAME_THREADS));
}
- av_freep(&avctx->internal);
- avctx->codec = NULL;
- goto end;
+
+ 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->compat_encode_packet);
+ av_packet_unref(avci->buffer_pkt);
+
+ av_frame_unref(avci->es.in_frame);
+ av_packet_unref(avci->ds.in_pkt);
+
+ if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
+ ff_thread_flush(avctx);
+ else if (avctx->codec->flush)
+ avctx->codec->flush(avctx);
+
+ avctx->pts_correction_last_pts =
+ avctx->pts_correction_last_dts = INT64_MIN;
+
+ if (av_codec_is_decoder(avctx->codec))
+ av_bsf_flush(avci->bsf);
+
+ if (!avctx->refcounted_frames)
+ av_frame_unref(avci->to_free);
}
void avsubtitle_free(AVSubtitle *sub)
return 0;
if (avcodec_is_open(avctx)) {
- FramePool *pool = avctx->internal->pool;
if (CONFIG_FRAME_THREAD_ENCODER &&
avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
ff_frame_thread_encoder_free(avctx);
av_frame_free(&avctx->internal->to_free);
av_frame_free(&avctx->internal->compat_decode_frame);
av_frame_free(&avctx->internal->buffer_frame);
+ av_packet_free(&avctx->internal->compat_encode_packet);
av_packet_free(&avctx->internal->buffer_pkt);
av_packet_free(&avctx->internal->last_pkt_props);
av_packet_free(&avctx->internal->ds.in_pkt);
+ av_frame_free(&avctx->internal->es.in_frame);
- for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
- av_buffer_pool_uninit(&pool->pools[i]);
- av_freep(&avctx->internal->pool);
+ av_buffer_unref(&avctx->internal->pool);
if (avctx->hwaccel && avctx->hwaccel->uninit)
avctx->hwaccel->uninit(avctx);
av_freep(&avctx->internal->hwaccel_priv_data);
- ff_decode_bsfs_uninit(avctx);
+ av_bsf_free(&avctx->internal->bsf);
av_freep(&avctx->internal);
}
switch (codec_id) {
case AV_CODEC_ID_8SVX_EXP:
case AV_CODEC_ID_8SVX_FIB:
+ case AV_CODEC_ID_ADPCM_ARGO:
case AV_CODEC_ID_ADPCM_CT:
case AV_CODEC_ID_ADPCM_IMA_APC:
+ case AV_CODEC_ID_ADPCM_IMA_APM:
case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
case AV_CODEC_ID_ADPCM_IMA_OKI:
case AV_CODEC_ID_ADPCM_IMA_WS:
+ case AV_CODEC_ID_ADPCM_IMA_SSI:
case AV_CODEC_ID_ADPCM_G722:
case AV_CODEC_ID_ADPCM_YAMAHA:
case AV_CODEC_ID_ADPCM_AICA:
case AV_CODEC_ID_PCM_S8:
case AV_CODEC_ID_PCM_S8_PLANAR:
case AV_CODEC_ID_PCM_U8:
- case AV_CODEC_ID_PCM_ZORK:
case AV_CODEC_ID_SDX2_DPCM:
+ case AV_CODEC_ID_DERF_DPCM:
return 8;
case AV_CODEC_ID_PCM_S16BE:
case AV_CODEC_ID_PCM_S16BE_PLANAR:
AVPacketSideData *tmp;
AVCPBProperties *props;
size_t size;
+ int i;
+
+ for (i = 0; i < avctx->nb_coded_side_data; i++)
+ if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
+ return (AVCPBProperties *)avctx->coded_side_data[i].data;
props = av_cpb_properties_alloc(&size);
if (!props)
return 0;
}
-int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
+static unsigned bcd2uint(uint8_t bcd)
+{
+ unsigned low = bcd & 0xf;
+ unsigned high = bcd >> 4;
+ if (low > 9 || high > 9)
+ return 0;
+ return low + 10*high;
+}
+
+int ff_alloc_timecode_sei(const AVFrame *frame, size_t prefix_len,
void **data, size_t *sei_size)
{
- AVFrameSideData *side_data = NULL;
+ AVFrameSideData *sd = NULL;
uint8_t *sei_data;
+ PutBitContext pb;
+ uint32_t *tc;
+ int m;
if (frame)
- side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
+ sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
- if (!side_data) {
+ if (!sd) {
*data = NULL;
return 0;
}
+ tc = (uint32_t*)sd->data;
+ m = tc[0] & 3;
- *sei_size = side_data->size + 11;
+ *sei_size = sizeof(uint32_t) * 4;
*data = av_mallocz(*sei_size + prefix_len);
if (!*data)
return AVERROR(ENOMEM);
sei_data = (uint8_t*)*data + prefix_len;
- // country code
- sei_data[0] = 181;
- sei_data[1] = 0;
- sei_data[2] = 49;
-
- /**
- * 'GA94' is standard in North America for ATSC, but hard coding
- * this style may not be the right thing to do -- other formats
- * do exist. This information is not available in the side_data
- * so we are going with this right now.
- */
- AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
- sei_data[7] = 3;
- sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
- sei_data[9] = 0;
-
- memcpy(sei_data + 10, side_data->data, side_data->size);
-
- sei_data[side_data->size+10] = 255;
+ init_put_bits(&pb, sei_data, *sei_size);
+ put_bits(&pb, 2, m); // num_clock_ts
+
+ for (int j = 1; j <= m; j++) {
+ uint32_t tcsmpte = tc[j];
+ unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
+ unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
+ unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
+ unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
+ unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
+
+ put_bits(&pb, 1, 1); // clock_timestamp_flag
+ put_bits(&pb, 1, 1); // units_field_based_flag
+ put_bits(&pb, 5, 0); // counting_type
+ put_bits(&pb, 1, 1); // full_timestamp_flag
+ put_bits(&pb, 1, 0); // discontinuity_flag
+ put_bits(&pb, 1, drop);
+ put_bits(&pb, 9, ff);
+ put_bits(&pb, 6, ss);
+ put_bits(&pb, 6, mm);
+ put_bits(&pb, 5, hh);
+ put_bits(&pb, 5, 0);
+ }
+ flush_put_bits(&pb);
return 0;
}