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

@@ -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;