FFmpeg AVPacket 剖析以及使用( 二 )

< 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)return AVERROR(EINVAL);ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);if (ret < 0)return ret;memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);return 0;}创建一个AVBufferRef实例,然后申请size长度的内存分配给AVBufferRef实例中的dataint av_buffer_realloc(AVBufferRef **pbuf, int size){AVBufferRef *buf = *pbuf;uint8_t *tmp;if (!buf) {/* allocate a new buffer with av_realloc(), so it will be reallocatable* later */uint8_t *data = http://www.kingceram.com/post/av_realloc(NULL, size);if (!data)return AVERROR(ENOMEM);buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);if (!buf) {av_freep(&data);return AVERROR(ENOMEM);}buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;*pbuf = buf;return 0;} else if (buf->size == size)return 0;if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||!av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {/* cannot realloc, allocate a new reallocable buffer and copy data */AVBufferRef *new = NULL;av_buffer_realloc(&new, size);if (!new)return AVERROR(ENOMEM);memcpy(new->data, buf->data, FFMIN(size, buf->size));buffer_replace(pbuf, &new);return 0;}tmp = av_realloc(buf->buffer->data, size);if (!tmp)return AVERROR(ENOMEM);buf->buffer->data = http://www.kingceram.com/post/buf->data = http://www.kingceram.com/post/tmp;buf->buffer->size = buf->size = size;return 0;}//释放AVBufferRef申请内存void av_buffer_unref(AVBufferRef **buf){if (!buf || !*buf)return;buffer_replace(buf, NULL);}static void buffer_replace(AVBufferRef **dst, AVBufferRef **src){AVBuffer *b;b = (*dst)->buffer;if (src) {**dst = **src;av_freep(src);} elseav_freep(dst);if (atomic_fetch_add_explicit(&b->refcount, -1, memory_order_acq_rel) == 1) {b->free(b->opaque, b->data);av_freep(&b);}}
3)
数据简单如下:
00 00 00 01 61 e1 40 01 58 2b fb 22 ff 29 7b 3f 6f 67 2f 29 fa 25 53 68 78 46 b1
在调用函数的时候打印错误如下:
I:2018-01-06 15:06:05 ms:887:nal_unit_type: 1, nal_ref_idc: 3I:2018-01-06 15:06:05 ms:888:non-existing PPS 0 referencedI:2018-01-06 15:06:05 ms:888:decode_slice_header errorI:2018-01-06 15:06:05 ms:888:no frame!
当数据如下,可以正确的解析出一帧图像
00 00 00 01 67 42 00 2a 96 35 40 f0 04 4f cb 37 01 01 01 40 00 01 c2 00 00 57 e4 01 00 00 00 01 68 ce 3c 80 00 00 00 01 06 e5 01 ef 80 00 00 03 00 00 00 01 65 b8 00 00 52 58 00 00 27 f5 d4 48 7e b4 41 07 24 60 95 2c 92 37 68 75 63 4c ad 3f b1
很显然,67是SPS,68是PPS,然后65是关键帧,开始出来图像
结构体定义
【FFmpeg AVPacket 剖析以及使用】/*** A reference to a data buffer.** The size of this struct is not a part of the public ABI and it is not meant* to be allocated directly.*/typedef struct AVBufferRef {AVBuffer *buffer;/*** The data buffer. It is considered writable if and only if* this is the only reference to the buffer, in which case* av_buffer_is_writable() returns 1.*/uint8_t *data;/*** Size of data in bytes.*/intsize;} AVBufferRef;/*** This structure stores compressed data. It is typically exported by demuxers* and then passed as input to decoders, or received as output from encoders and* then passed to muxers.** For video, it should typically contain one compressed frame. For audio it may* contain several compressed frames.** AVPacket is one of the few structs in FFmpeg, whose size is a part of public* ABI. Thus it may be allocated on stack and no new fields can be added to it* without libavcodec and libavformat major bump.** The semantics of data ownership depends on the buf or destruct (deprecated)* fields. If either is set, the packet data is dynamically allocated and is* valid indefinitely until av_free_packet() is called (which in turn calls* av_buffer_unref()/the destruct callback to free the data). If neither is set,* the packet data is typically backed by some static buffer somewhere and is* only valid for a limited time (e.g. until the next read call when demuxing).** The side data is always allocated with av_malloc() and is freed in* av_free_packet().*/typedef struct AVPacket {/*** A reference to the reference-counted buffer where the packet data is* stored.* May be NULL, then the packet data is not reference-counted.*/AVBufferRef *buf;/*** Presentation timestamp in AVStream->time_base units; the time at which* the decompressed packet will be presented to the user.* Can be AV_NOPTS_VALUE if it is not stored in the file.* pts MUST be larger or equal to dts as presentation cannot happen before* decompression, unless one wants to view hex dumps. Some formats misuse* the terms dts and pts/cts to mean something different. Such timestamps* must be converted to true pts/dts before they are stored in AVPacket.*/int64_t pts;/*** Decompression timestamp in AVStream->time_base units; the time at which* the packet is decompressed.* Can be AV_NOPTS_VALUE if it is not stored in the file.*/int64_t dts;uint8_t *data;intsize;intstream_index;/*** A combination of AV_PKT_FLAG values*/intflags;/*** Additional packet data that can be provided by the container.* Packet can contain several types of side information.*/struct {uint8_t *data;intsize;enum AVPacketSideDataType type;} *side_data;int side_data_elems;/*** Duration of this packet in AVStream->time_base units, 0 if unknown.* Equals next_pts - this_pts in presentation order.*/intduration;#if FF_API_DESTRUCT_PACKETattribute_deprecatedvoid(*destruct)(struct AVPacket *);attribute_deprecatedvoid*priv;#endifint64_t pos;///