1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/packet: move AVPacketList definition and function helpers over from libavformat

And replace the flags parameter with a function callback that can be used to
copy the contents of the packet (e.g, av_packet_ref and av_packet_copy_props).

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2020-08-17 12:03:50 -03:00
parent fda5363c80
commit 8a81820624
11 changed files with 155 additions and 146 deletions

View File

@ -726,6 +726,73 @@ FF_ENABLE_DEPRECATION_WARNINGS
#endif
}
int avpriv_packet_list_put(AVPacketList **packet_buffer,
AVPacketList **plast_pktl,
AVPacket *pkt,
int (*copy)(AVPacket *dst, const AVPacket *src),
int flags)
{
AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
int ret;
if (!pktl)
return AVERROR(ENOMEM);
if (copy) {
ret = copy(&pktl->pkt, pkt);
if (ret < 0) {
av_free(pktl);
return ret;
}
} else {
ret = av_packet_make_refcounted(pkt);
if (ret < 0) {
av_free(pktl);
return ret;
}
av_packet_move_ref(&pktl->pkt, pkt);
}
if (*packet_buffer)
(*plast_pktl)->next = pktl;
else
*packet_buffer = pktl;
/* Add the packet in the buffered packet list. */
*plast_pktl = pktl;
return 0;
}
int avpriv_packet_list_get(AVPacketList **pkt_buffer,
AVPacketList **pkt_buffer_end,
AVPacket *pkt)
{
AVPacketList *pktl;
if (!*pkt_buffer)
return AVERROR(EAGAIN);
pktl = *pkt_buffer;
*pkt = pktl->pkt;
*pkt_buffer = pktl->next;
if (!pktl->next)
*pkt_buffer_end = NULL;
av_freep(&pktl);
return 0;
}
void avpriv_packet_list_free(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
{
AVPacketList *tmp = *pkt_buf;
while (tmp) {
AVPacketList *pktl = tmp;
tmp = pktl->next;
av_packet_unref(&pktl->pkt);
av_freep(&pktl);
}
*pkt_buf = NULL;
*pkt_buf_end = NULL;
}
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
{
uint8_t *side_data;

View File

@ -393,6 +393,11 @@ typedef struct AVPacket {
#endif
} AVPacket;
typedef struct AVPacketList {
AVPacket pkt;
struct AVPacketList *next;
} AVPacketList;
#define AV_PKT_FLAG_KEY 0x0001 ///< The packet contains a keyframe
#define AV_PKT_FLAG_CORRUPT 0x0002 ///< The packet content is corrupted
/**

View File

@ -23,6 +23,48 @@
#include "packet.h"
/**
* Append an AVPacket to the list.
*
* @param head List head element
* @param tail List tail element
* @param pkt The packet being appended. The data described in it will
* be made reference counted if it isn't already.
* @param copy A callback to copy the contents of the packet to the list.
May be null, in which case the packet's reference will be
moved to the list.
* @return 0 on success, negative AVERROR value on failure. On failure,
the packet and the list are unchanged.
*/
int avpriv_packet_list_put(AVPacketList **head, AVPacketList **tail,
AVPacket *pkt,
int (*copy)(AVPacket *dst, const AVPacket *src),
int flags);
/**
* Remove the oldest AVPacket in the list and return it.
*
* @note The pkt will be overwritten completely on success. The caller
* owns the packet and must unref it by itself.
*
* @param head List head element
* @param tail List tail element
* @param pkt Pointer to an AVPacket struct
* @return 0 on success, and a packet is returned. AVERROR(EAGAIN) if
* the list was empty.
*/
int avpriv_packet_list_get(AVPacketList **head, AVPacketList **tail,
AVPacket *pkt);
/**
* Wipe the list and unref all the packets in it.
*
* @param head List head element
* @param tail List tail element
*/
void avpriv_packet_list_free(AVPacketList **head, AVPacketList **tail);
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type);
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp);

View File

@ -23,6 +23,7 @@
#include "libavutil/intfloat.h"
#include "libavutil/opt.h"
#include "libavcodec/packet_internal.h"
#include "avformat.h"
#include "internal.h"
#include "aiff.h"
@ -220,8 +221,8 @@ static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
if (s->streams[pkt->stream_index]->nb_frames >= 1)
return 0;
return ff_packet_list_put(&aiff->pict_list, &aiff->pict_list_end,
pkt, FF_PACKETLIST_FLAG_REF_PACKET);
return avpriv_packet_list_put(&aiff->pict_list, &aiff->pict_list_end,
pkt, av_packet_ref, 0);
}
return 0;
@ -272,7 +273,7 @@ static void aiff_deinit(AVFormatContext *s)
{
AIFFOutputContext *aiff = s->priv_data;
ff_packet_list_free(&aiff->pict_list, &aiff->pict_list_end);
avpriv_packet_list_free(&aiff->pict_list, &aiff->pict_list_end);
}
#define OFFSET(x) offsetof(AIFFOutputContext, x)

View File

@ -2007,12 +2007,6 @@ void av_format_inject_global_side_data(AVFormatContext *s);
*/
enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx);
typedef struct AVPacketList {
AVPacket pkt;
struct AVPacketList *next;
} AVPacketList;
/**
* @defgroup lavf_core Core functions
* @ingroup libavf

View File

@ -23,6 +23,7 @@
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavcodec/flac.h"
#include "libavcodec/packet_internal.h"
#include "avformat.h"
#include "avio_internal.h"
#include "flacenc.h"
@ -305,7 +306,7 @@ static int flac_queue_flush(AVFormatContext *s)
write = 0;
while (c->queue) {
ff_packet_list_get(&c->queue, &c->queue_end, &pkt);
avpriv_packet_list_get(&c->queue, &c->queue_end, &pkt);
if (write && (ret = flac_write_audio_packet(s, &pkt)) < 0)
write = 0;
av_packet_unref(&pkt);
@ -345,7 +346,7 @@ static void flac_deinit(struct AVFormatContext *s)
{
FlacMuxerContext *c = s->priv_data;
ff_packet_list_free(&c->queue, &c->queue_end);
avpriv_packet_list_free(&c->queue, &c->queue_end);
}
static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
@ -356,7 +357,7 @@ static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
if (pkt->stream_index == c->audio_stream_idx) {
if (c->waiting_pics) {
/* buffer audio packets until we get all the pictures */
ret = ff_packet_list_put(&c->queue, &c->queue_end, pkt, FF_PACKETLIST_FLAG_REF_PACKET);
ret = avpriv_packet_list_put(&c->queue, &c->queue_end, pkt, av_packet_ref, 0);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Out of memory in packet queue; skipping attached pictures\n");
c->waiting_pics = 0;

View File

@ -730,48 +730,6 @@ int ff_unlock_avformat(void);
*/
void ff_format_set_url(AVFormatContext *s, char *url);
#define FF_PACKETLIST_FLAG_REF_PACKET (1 << 0) /**< Create a new reference for the packet instead of
transferring the ownership of the existing one to the
list. */
/**
* Append an AVPacket to the list.
*
* @param head List head element
* @param tail List tail element
* @param pkt The packet being appended. The data described in it will
* be made reference counted if it isn't already.
* @param flags Any combination of FF_PACKETLIST_FLAG_* flags
* @return 0 on success, negative AVERROR value on failure. On failure,
the list is unchanged
*/
int ff_packet_list_put(AVPacketList **head, AVPacketList **tail,
AVPacket *pkt, int flags);
/**
* Remove the oldest AVPacket in the list and return it.
* The behaviour is undefined if the packet list is empty.
*
* @note The pkt will be overwritten completely. The caller owns the
* packet and must unref it by itself.
*
* @param head List head element
* @param tail List tail element
* @param pkt Pointer to an AVPacket struct
* @return 0 on success. Success is guaranteed
* if the packet list is not empty.
*/
int ff_packet_list_get(AVPacketList **head, AVPacketList **tail,
AVPacket *pkt);
/**
* Wipe the list and unref all the packets in it.
*
* @param head List head element
* @param tail List tail element
*/
void ff_packet_list_free(AVPacketList **head, AVPacketList **tail);
void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]);
#endif /* AVFORMAT_INTERNAL_H */

View File

@ -48,6 +48,7 @@
#include "libavcodec/bytestream.h"
#include "libavcodec/flac.h"
#include "libavcodec/mpeg4audio.h"
#include "libavcodec/packet_internal.h"
#include "avformat.h"
#include "avio_internal.h"
@ -2991,7 +2992,7 @@ static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
MatroskaTrack *tracks = matroska->tracks.elem;
MatroskaTrack *track;
ff_packet_list_get(&matroska->queue, &matroska->queue_end, pkt);
avpriv_packet_list_get(&matroska->queue, &matroska->queue_end, pkt);
track = &tracks[pkt->stream_index];
if (track->has_palette) {
uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
@ -3013,7 +3014,7 @@ static int matroska_deliver_packet(MatroskaDemuxContext *matroska,
*/
static void matroska_clear_queue(MatroskaDemuxContext *matroska)
{
ff_packet_list_free(&matroska->queue, &matroska->queue_end);
avpriv_packet_list_free(&matroska->queue, &matroska->queue_end);
}
static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf,
@ -3179,7 +3180,7 @@ static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska,
track->audio.buf_timecode = AV_NOPTS_VALUE;
pkt->pos = pos;
pkt->stream_index = st->index;
ret = ff_packet_list_put(&matroska->queue, &matroska->queue_end, pkt, 0);
ret = avpriv_packet_list_put(&matroska->queue, &matroska->queue_end, pkt, NULL, 0);
if (ret < 0) {
av_packet_unref(pkt);
return AVERROR(ENOMEM);
@ -3401,7 +3402,7 @@ static int matroska_parse_webvtt(MatroskaDemuxContext *matroska,
pkt->duration = duration;
pkt->pos = pos;
err = ff_packet_list_put(&matroska->queue, &matroska->queue_end, pkt, 0);
err = avpriv_packet_list_put(&matroska->queue, &matroska->queue_end, pkt, NULL, 0);
if (err < 0) {
av_packet_unref(pkt);
return AVERROR(ENOMEM);
@ -3512,7 +3513,7 @@ FF_DISABLE_DEPRECATION_WARNINGS
FF_ENABLE_DEPRECATION_WARNINGS
#endif
res = ff_packet_list_put(&matroska->queue, &matroska->queue_end, pkt, 0);
res = avpriv_packet_list_put(&matroska->queue, &matroska->queue_end, pkt, NULL, 0);
if (res < 0) {
av_packet_unref(pkt);
return AVERROR(ENOMEM);

View File

@ -28,6 +28,7 @@
#include "libavcodec/mpegaudio.h"
#include "libavcodec/mpegaudiodata.h"
#include "libavcodec/mpegaudiodecheader.h"
#include "libavcodec/packet_internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/dict.h"
@ -387,7 +388,7 @@ static int mp3_queue_flush(AVFormatContext *s)
mp3_write_xing(s);
while (mp3->queue) {
ff_packet_list_get(&mp3->queue, &mp3->queue_end, &pkt);
avpriv_packet_list_get(&mp3->queue, &mp3->queue_end, &pkt);
if (write && (ret = mp3_write_audio_packet(s, &pkt)) < 0)
write = 0;
av_packet_unref(&pkt);
@ -522,7 +523,7 @@ static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
if (pkt->stream_index == mp3->audio_stream_idx) {
if (mp3->pics_to_write) {
/* buffer audio packets until we get all the pictures */
int ret = ff_packet_list_put(&mp3->queue, &mp3->queue_end, pkt, FF_PACKETLIST_FLAG_REF_PACKET);
int ret = avpriv_packet_list_put(&mp3->queue, &mp3->queue_end, pkt, av_packet_ref, 0);
if (ret < 0) {
av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
@ -630,7 +631,7 @@ static void mp3_deinit(struct AVFormatContext *s)
{
MP3Context *mp3 = s->priv_data;
ff_packet_list_free(&mp3->queue, &mp3->queue_end);
avpriv_packet_list_free(&mp3->queue, &mp3->queue_end);
av_freep(&mp3->xing_frame);
}

View File

@ -22,6 +22,7 @@
#include "libavutil/crc.h"
#include "libavutil/intreadwrite.h"
#include "libavcodec/packet_internal.h"
#include "apetag.h"
#include "avformat.h"
#include "avio_internal.h"
@ -93,8 +94,8 @@ static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
TTAMuxContext *tta = s->priv_data;
int ret;
ret = ff_packet_list_put(&tta->queue, &tta->queue_end, pkt,
FF_PACKETLIST_FLAG_REF_PACKET);
ret = avpriv_packet_list_put(&tta->queue, &tta->queue_end, pkt,
av_packet_ref, 0);
if (ret < 0) {
return ret;
}
@ -125,7 +126,7 @@ static void tta_queue_flush(AVFormatContext *s)
AVPacket pkt;
while (tta->queue) {
ff_packet_list_get(&tta->queue, &tta->queue_end, &pkt);
avpriv_packet_list_get(&tta->queue, &tta->queue_end, &pkt);
avio_write(s->pb, pkt.data, pkt.size);
av_packet_unref(&pkt);
}
@ -161,7 +162,7 @@ static void tta_deinit(AVFormatContext *s)
TTAMuxContext *tta = s->priv_data;
ffio_free_dyn_buf(&tta->seek_table);
ff_packet_list_free(&tta->queue, &tta->queue_end);
avpriv_packet_list_free(&tta->queue, &tta->queue_end);
}
AVOutputFormat ff_tta_muxer = {

View File

@ -37,6 +37,7 @@
#include "libavcodec/bytestream.h"
#include "libavcodec/internal.h"
#include "libavcodec/packet_internal.h"
#include "libavcodec/raw.h"
#include "avformat.h"
@ -438,40 +439,6 @@ static int init_input(AVFormatContext *s, const char *filename,
s, 0, s->format_probesize);
}
int ff_packet_list_put(AVPacketList **packet_buffer,
AVPacketList **plast_pktl,
AVPacket *pkt, int flags)
{
AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
int ret;
if (!pktl)
return AVERROR(ENOMEM);
if (flags & FF_PACKETLIST_FLAG_REF_PACKET) {
if ((ret = av_packet_ref(&pktl->pkt, pkt)) < 0) {
av_free(pktl);
return ret;
}
} else {
ret = av_packet_make_refcounted(pkt);
if (ret < 0) {
av_free(pktl);
return ret;
}
av_packet_move_ref(&pktl->pkt, pkt);
}
if (*packet_buffer)
(*plast_pktl)->next = pktl;
else
*packet_buffer = pktl;
/* Add the packet in the buffered packet list. */
*plast_pktl = pktl;
return 0;
}
int avformat_queue_attached_pictures(AVFormatContext *s)
{
int i, ret;
@ -485,10 +452,10 @@ int avformat_queue_attached_pictures(AVFormatContext *s)
continue;
}
ret = ff_packet_list_put(&s->internal->raw_packet_buffer,
ret = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
&s->internal->raw_packet_buffer_end,
&s->streams[i]->attached_pic,
FF_PACKETLIST_FLAG_REF_PACKET);
av_packet_ref, 0);
if (ret < 0)
return ret;
}
@ -841,7 +808,7 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
if ((err = probe_codec(s, st, NULL)) < 0)
return err;
if (st->request_probe <= 0) {
ff_packet_list_get(&s->internal->raw_packet_buffer,
avpriv_packet_list_get(&s->internal->raw_packet_buffer,
&s->internal->raw_packet_buffer_end, pkt);
s->internal->raw_packet_buffer_remaining_size += pkt->size;
return 0;
@ -914,9 +881,9 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
if (!pktl && st->request_probe <= 0)
return ret;
err = ff_packet_list_put(&s->internal->raw_packet_buffer,
err = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
&s->internal->raw_packet_buffer_end,
pkt, 0);
pkt, NULL, 0);
if (err < 0) {
av_packet_unref(pkt);
return err;
@ -1420,20 +1387,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
#endif
}
void ff_packet_list_free(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
{
AVPacketList *tmp = *pkt_buf;
while (tmp) {
AVPacketList *pktl = tmp;
tmp = pktl->next;
av_packet_unref(&pktl->pkt);
av_freep(&pktl);
}
*pkt_buf = NULL;
*pkt_buf_end = NULL;
}
/**
* Parse a packet, add all split parts to parse_queue.
*
@ -1530,9 +1483,9 @@ static int parse_packet(AVFormatContext *s, AVPacket *pkt,
compute_pkt_fields(s, st, st->parser, &out_pkt, next_dts, next_pts);
ret = ff_packet_list_put(&s->internal->parse_queue,
ret = avpriv_packet_list_put(&s->internal->parse_queue,
&s->internal->parse_queue_end,
&out_pkt, 0);
&out_pkt, NULL, 0);
if (ret < 0) {
av_packet_unref(&out_pkt);
goto fail;
@ -1550,21 +1503,6 @@ fail:
return ret;
}
int ff_packet_list_get(AVPacketList **pkt_buffer,
AVPacketList **pkt_buffer_end,
AVPacket *pkt)
{
AVPacketList *pktl;
av_assert0(*pkt_buffer);
pktl = *pkt_buffer;
*pkt = pktl->pkt;
*pkt_buffer = pktl->next;
if (!pktl->next)
*pkt_buffer_end = NULL;
av_freep(&pktl);
return 0;
}
static int64_t ts_to_samples(AVStream *st, int64_t ts)
{
return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
@ -1695,7 +1633,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
}
if (!got_packet && s->internal->parse_queue)
ret = ff_packet_list_get(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
ret = avpriv_packet_list_get(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
if (ret >= 0) {
AVStream *st = s->streams[pkt->stream_index];
@ -1779,7 +1717,7 @@ int av_read_frame(AVFormatContext *s, AVPacket *pkt)
if (!genpts) {
ret = s->internal->packet_buffer
? ff_packet_list_get(&s->internal->packet_buffer,
? avpriv_packet_list_get(&s->internal->packet_buffer,
&s->internal->packet_buffer_end, pkt)
: read_frame_internal(s, pkt);
if (ret < 0)
@ -1828,7 +1766,7 @@ int av_read_frame(AVFormatContext *s, AVPacket *pkt)
st = s->streams[next_pkt->stream_index];
if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
ret = ff_packet_list_get(&s->internal->packet_buffer,
ret = avpriv_packet_list_get(&s->internal->packet_buffer,
&s->internal->packet_buffer_end, pkt);
goto return_packet;
}
@ -1843,9 +1781,9 @@ int av_read_frame(AVFormatContext *s, AVPacket *pkt)
return ret;
}
ret = ff_packet_list_put(&s->internal->packet_buffer,
ret = avpriv_packet_list_put(&s->internal->packet_buffer,
&s->internal->packet_buffer_end,
pkt, 0);
pkt, NULL, 0);
if (ret < 0) {
av_packet_unref(pkt);
return ret;
@ -1873,9 +1811,9 @@ static void flush_packet_queue(AVFormatContext *s)
{
if (!s->internal)
return;
ff_packet_list_free(&s->internal->parse_queue, &s->internal->parse_queue_end);
ff_packet_list_free(&s->internal->packet_buffer, &s->internal->packet_buffer_end);
ff_packet_list_free(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
avpriv_packet_list_free(&s->internal->parse_queue, &s->internal->parse_queue_end);
avpriv_packet_list_free(&s->internal->packet_buffer, &s->internal->packet_buffer_end);
avpriv_packet_list_free(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
}
@ -3825,9 +3763,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
}
if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
ret = ff_packet_list_put(&ic->internal->packet_buffer,
ret = avpriv_packet_list_put(&ic->internal->packet_buffer,
&ic->internal->packet_buffer_end,
&pkt1, 0);
&pkt1, NULL, 0);
if (ret < 0)
goto unref_then_goto_end;