Skip to content

Commit

Permalink
avcodec/packet_internal: Add proper PacketList struct
Browse files Browse the repository at this point in the history
Up until now, we had a PacketList structure which is actually
a PacketListEntry; a proper PacketList did not exist
and all the related functions just passed pointers to pointers
to the head and tail elements around. All these pointers were
actually consecutive elements of their containing structs,
i.e. the users already treated them as if they were a struct.

So add a proper PacketList struct and rename the current PacketList
to PacketListEntry; also make the functions use this structure
instead of the pair of pointers.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
  • Loading branch information
mkver committed Jan 4, 2022
1 parent b74e47c commit d61240f
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 175 deletions.
36 changes: 16 additions & 20 deletions libavcodec/avpacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,12 @@ void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
}

int avpriv_packet_list_put(PacketList **packet_buffer,
PacketList **plast_pktl,
int avpriv_packet_list_put(PacketList *packet_buffer,
AVPacket *pkt,
int (*copy)(AVPacket *dst, const AVPacket *src),
int flags)
{
PacketList *pktl = av_malloc(sizeof(PacketList));
PacketListEntry *pktl = av_malloc(sizeof(*pktl));
int ret;

if (!pktl)
Expand All @@ -565,44 +564,41 @@ int avpriv_packet_list_put(PacketList **packet_buffer,

pktl->next = NULL;

if (*packet_buffer)
(*plast_pktl)->next = pktl;
if (packet_buffer->head)
packet_buffer->tail->next = pktl;
else
*packet_buffer = pktl;
packet_buffer->head = pktl;

/* Add the packet in the buffered packet list. */
*plast_pktl = pktl;
packet_buffer->tail = pktl;
return 0;
}

int avpriv_packet_list_get(PacketList **pkt_buffer,
PacketList **pkt_buffer_end,
int avpriv_packet_list_get(PacketList *pkt_buffer,
AVPacket *pkt)
{
PacketList *pktl;
if (!*pkt_buffer)
PacketListEntry *pktl = pkt_buffer->head;
if (!pktl)
return AVERROR(EAGAIN);
pktl = *pkt_buffer;
*pkt = pktl->pkt;
*pkt_buffer = pktl->next;
if (!pktl->next)
*pkt_buffer_end = NULL;
pkt_buffer->head = pktl->next;
if (!pkt_buffer->head)
pkt_buffer->tail = NULL;
av_freep(&pktl);
return 0;
}

void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
void avpriv_packet_list_free(PacketList *pkt_buf)
{
PacketList *tmp = *pkt_buf;
PacketListEntry *tmp = pkt_buf->head;

while (tmp) {
PacketList *pktl = tmp;
PacketListEntry *pktl = tmp;
tmp = pktl->next;
av_packet_unref(&pktl->pkt);
av_freep(&pktl);
}
*pkt_buf = NULL;
*pkt_buf_end = NULL;
pkt_buf->head = pkt_buf->tail = NULL;
}

int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Expand Down
25 changes: 11 additions & 14 deletions libavcodec/packet_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@

#include "packet.h"

typedef struct PacketList {
struct PacketList *next;
typedef struct PacketListEntry {
struct PacketListEntry *next;
AVPacket pkt;
} PacketListEntry;

typedef struct PacketList {
PacketListEntry *head, *tail;
} PacketList;

/**
* Append an AVPacket to the list.
*
* @param head List head element
* @param tail List tail element
* @param list A PacketList
* @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.
Expand All @@ -41,8 +44,7 @@ typedef struct PacketList {
* @return 0 on success, negative AVERROR value on failure. On failure,
the packet and the list are unchanged.
*/
int avpriv_packet_list_put(PacketList **head, PacketList **tail,
AVPacket *pkt,
int avpriv_packet_list_put(PacketList *list, AVPacket *pkt,
int (*copy)(AVPacket *dst, const AVPacket *src),
int flags);

Expand All @@ -52,22 +54,17 @@ int avpriv_packet_list_put(PacketList **head, PacketList **tail,
* @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 head A pointer to a PacketList struct
* @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(PacketList **head, PacketList **tail,
AVPacket *pkt);
int avpriv_packet_list_get(PacketList *list, 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(PacketList **head, PacketList **tail);
void avpriv_packet_list_free(PacketList *list);

int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type);

Expand Down
5 changes: 4 additions & 1 deletion libavdevice/decklink_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#define IDeckLinkProfileAttributes IDeckLinkAttributes
#endif

extern "C" {
#include "libavcodec/packet_internal.h"
}
#include "libavutil/thread.h"
#include "decklink_common_c.h"
#if CONFIG_LIBKLVANC
Expand Down Expand Up @@ -75,7 +78,7 @@ class decklink_output_callback;
class decklink_input_callback;

typedef struct AVPacketQueue {
PacketList *first_pkt, *last_pkt;
PacketList pkt_list;
int nb_packets;
unsigned long long size;
int abort_request;
Expand Down
29 changes: 14 additions & 15 deletions libavdevice/decklink_dec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,16 @@ static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)

static void avpacket_queue_flush(AVPacketQueue *q)
{
PacketList *pkt, *pkt1;
PacketListEntry *pkt, *pkt1;

pthread_mutex_lock(&q->mutex);
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
for (pkt = q->pkt_list.head; pkt != NULL; pkt = pkt1) {
pkt1 = pkt->next;
av_packet_unref(&pkt->pkt);
av_freep(&pkt);
}
q->last_pkt = NULL;
q->first_pkt = NULL;
q->pkt_list.head = NULL;
q->pkt_list.tail = NULL;
q->nb_packets = 0;
q->size = 0;
pthread_mutex_unlock(&q->mutex);
Expand All @@ -516,7 +516,7 @@ static unsigned long long avpacket_queue_size(AVPacketQueue *q)

static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
{
PacketList *pkt1;
PacketListEntry *pkt1;

// Drop Packet if queue size is > maximum queue size
if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
Expand All @@ -530,7 +530,7 @@ static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
return -1;
}

pkt1 = (PacketList *)av_malloc(sizeof(PacketList));
pkt1 = (PacketListEntry *)av_malloc(sizeof(*pkt1));
if (!pkt1) {
av_packet_unref(pkt);
return -1;
Expand All @@ -540,13 +540,13 @@ static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)

pthread_mutex_lock(&q->mutex);

if (!q->last_pkt) {
q->first_pkt = pkt1;
if (!q->pkt_list.tail) {
q->pkt_list.head = pkt1;
} else {
q->last_pkt->next = pkt1;
q->pkt_list.tail->next = pkt1;
}

q->last_pkt = pkt1;
q->pkt_list.tail = pkt1;
q->nb_packets++;
q->size += pkt1->pkt.size + sizeof(*pkt1);

Expand All @@ -558,17 +558,16 @@ static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)

static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
{
PacketList *pkt1;
int ret;

pthread_mutex_lock(&q->mutex);

for (;; ) {
pkt1 = q->first_pkt;
PacketListEntry *pkt1 = q->pkt_list.head;
if (pkt1) {
q->first_pkt = pkt1->next;
if (!q->first_pkt) {
q->last_pkt = NULL;
q->pkt_list.head = pkt1->next;
if (!q->pkt_list.head) {
q->pkt_list.tail = NULL;
}
q->nb_packets--;
q->size -= pkt1->pkt.size + sizeof(*pkt1);
Expand Down
10 changes: 5 additions & 5 deletions libavdevice/dshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static int
dshow_read_close(AVFormatContext *s)
{
struct dshow_ctx *ctx = s->priv_data;
PacketList *pktl;
PacketListEntry *pktl;

if (ctx->control) {
IMediaControl_Stop(ctx->control);
Expand Down Expand Up @@ -298,7 +298,7 @@ dshow_read_close(AVFormatContext *s)

pktl = ctx->pktl;
while (pktl) {
PacketList *next = pktl->next;
PacketListEntry *next = pktl->next;
av_packet_unref(&pktl->pkt);
av_free(pktl);
pktl = next;
Expand Down Expand Up @@ -342,7 +342,7 @@ callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, e
{
AVFormatContext *s = priv_data;
struct dshow_ctx *ctx = s->priv_data;
PacketList **ppktl, *pktl_next;
PacketListEntry **ppktl, *pktl_next;

// dump_videohdr(s, vdhdr);

Expand All @@ -351,7 +351,7 @@ callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, e
if(shall_we_drop(s, index, devtype))
goto fail;

pktl_next = av_mallocz(sizeof(PacketList));
pktl_next = av_mallocz(sizeof(*pktl_next));
if(!pktl_next)
goto fail;

Expand Down Expand Up @@ -1868,7 +1868,7 @@ static int dshow_check_event_queue(IMediaEvent *media_event)
static int dshow_read_packet(AVFormatContext *s, AVPacket *pkt)
{
struct dshow_ctx *ctx = s->priv_data;
PacketList *pktl = NULL;
PacketListEntry *pktl = NULL;

while (!ctx->eof && !pktl) {
WaitForSingleObject(ctx->mutex, INFINITE);
Expand Down
2 changes: 1 addition & 1 deletion libavdevice/dshow_capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ struct dshow_ctx {
HANDLE mutex;
HANDLE event[2]; /* event[0] is set by DirectShow
* event[1] is set by callback() */
PacketList *pktl;
PacketListEntry *pktl;

int eof;

Expand Down
12 changes: 6 additions & 6 deletions libavdevice/vfwcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct vfw_ctx {
HWND hwnd;
HANDLE mutex;
HANDLE event;
PacketList *pktl;
PacketListEntry *pktl;
unsigned int curbufsize;
unsigned int frame_num;
char *video_size; /**< A string describing video size, set by a private option. */
Expand Down Expand Up @@ -179,7 +179,7 @@ static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
{
AVFormatContext *s;
struct vfw_ctx *ctx;
PacketList **ppktl, *pktl_next;
PacketListEntry **ppktl, *pktl_next;

s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
ctx = s->priv_data;
Expand All @@ -191,7 +191,7 @@ static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)

WaitForSingleObject(ctx->mutex, INFINITE);

pktl_next = av_mallocz(sizeof(PacketList));
pktl_next = av_mallocz(sizeof(*pktl_next));
if(!pktl_next)
goto fail;

Expand Down Expand Up @@ -220,7 +220,7 @@ static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
static int vfw_read_close(AVFormatContext *s)
{
struct vfw_ctx *ctx = s->priv_data;
PacketList *pktl;
PacketListEntry *pktl;

if(ctx->hwnd) {
SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
Expand All @@ -234,7 +234,7 @@ static int vfw_read_close(AVFormatContext *s)

pktl = ctx->pktl;
while (pktl) {
PacketList *next = pktl->next;
PacketListEntry *next = pktl->next;
av_packet_unref(&pktl->pkt);
av_free(pktl);
pktl = next;
Expand Down Expand Up @@ -440,7 +440,7 @@ static int vfw_read_header(AVFormatContext *s)
static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
{
struct vfw_ctx *ctx = s->priv_data;
PacketList *pktl = NULL;
PacketListEntry *pktl = NULL;

while(!pktl) {
WaitForSingleObject(ctx->mutex, INFINITE);
Expand Down
17 changes: 8 additions & 9 deletions libavformat/aiffenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef struct AIFFOutputContext {
int64_t frames;
int64_t ssnd;
int audio_stream_idx;
PacketList *pict_list, *pict_list_end;
PacketList pict_list;
int write_id3v2;
int id3v2_version;
} AIFFOutputContext;
Expand All @@ -48,9 +48,9 @@ static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)
uint64_t pos, end, size;
ID3v2EncContext id3v2 = { 0 };
AVIOContext *pb = s->pb;
PacketList *pict_list = aiff->pict_list;
PacketListEntry *list_entry = aiff->pict_list.head;

if (!s->metadata && !s->nb_chapters && !aiff->pict_list)
if (!s->metadata && !s->nb_chapters && !list_entry)
return 0;

avio_wl32(pb, MKTAG('I', 'D', '3', ' '));
Expand All @@ -59,10 +59,10 @@ static int put_id3v2_tags(AVFormatContext *s, AIFFOutputContext *aiff)

ff_id3v2_start(&id3v2, pb, aiff->id3v2_version, ID3v2_DEFAULT_MAGIC);
ff_id3v2_write_metadata(s, &id3v2);
while (pict_list) {
if ((ret = ff_id3v2_write_apic(s, &id3v2, &pict_list->pkt)) < 0)
while (list_entry) {
if ((ret = ff_id3v2_write_apic(s, &id3v2, &list_entry->pkt)) < 0)
return ret;
pict_list = pict_list->next;
list_entry = list_entry->next;
}
ff_id3v2_finish(&id3v2, pb, s->metadata_header_padding);

Expand Down Expand Up @@ -218,8 +218,7 @@ static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
if (s->streams[pkt->stream_index]->nb_frames >= 1)
return 0;

return avpriv_packet_list_put(&aiff->pict_list, &aiff->pict_list_end,
pkt, NULL, 0);
return avpriv_packet_list_put(&aiff->pict_list, pkt, NULL, 0);
}

return 0;
Expand Down Expand Up @@ -265,7 +264,7 @@ static void aiff_deinit(AVFormatContext *s)
{
AIFFOutputContext *aiff = s->priv_data;

avpriv_packet_list_free(&aiff->pict_list, &aiff->pict_list_end);
avpriv_packet_list_free(&aiff->pict_list);
}

#define OFFSET(x) offsetof(AIFFOutputContext, x)
Expand Down
Loading

0 comments on commit d61240f

Please sign in to comment.