FFmpeg  4.4.5
avpacket.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include "libavcodec/avcodec.h"
24 #include "libavutil/error.h"
25 
26 
27 
28 static int setup_side_data_entry(AVPacket* avpkt)
29 {
30  const uint8_t *data_name = NULL;
31  int ret = 0, bytes;
32  uint8_t *extra_data = NULL;
33 
34 
35  /* get side_data_name string */
37 
38  /* Allocate a memory bloc */
39  bytes = strlen(data_name);
40 
41  if(!(extra_data = av_malloc(bytes))){
42  ret = AVERROR(ENOMEM);
43  fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
44  exit(1);
45  }
46  /* copy side_data_name to extra_data array */
47  memcpy(extra_data, data_name, bytes);
48 
49  /* create side data for AVPacket */
51  extra_data, bytes);
52  if(ret < 0){
53  fprintf(stderr,
54  "Error occurred in av_packet_add_side_data: %s\n",
55  av_err2str(ret));
56  }
57 
58  return ret;
59 }
60 
61 static int initializations(AVPacket* avpkt)
62 {
63  const static uint8_t* data = "selftest for av_packet_clone(...)";
64  int ret = 0;
65 
66  /* set values for avpkt */
67  avpkt->pts = 17;
68  avpkt->dts = 2;
69  avpkt->data = (uint8_t*)data;
70  avpkt->size = strlen(data);
71  avpkt->flags = AV_PKT_FLAG_DISCARD;
72  avpkt->duration = 100;
73  avpkt->pos = 3;
74 
75  ret = setup_side_data_entry(avpkt);
76 
77  return ret;
78 }
79 
80 int main(void)
81 {
82  AVPacket *avpkt = NULL;
83  AVPacket *avpkt_clone = NULL;
84  int ret = 0;
85 
86  /* test av_packet_alloc */
87  avpkt = av_packet_alloc();
88  if(!avpkt) {
89  av_log(NULL, AV_LOG_ERROR, "av_packet_alloc failed to allcoate AVPacket\n");
90  return 1;
91  }
92 
93  if (initializations(avpkt) < 0) {
94  printf("failed to initialize variables\n");
95  av_packet_free(&avpkt);
96  return 1;
97  }
98  /* test av_packet_clone*/
99  avpkt_clone = av_packet_clone(avpkt);
100 
101  if(!avpkt_clone) {
102  av_log(NULL, AV_LOG_ERROR,"av_packet_clone failed to clone AVPacket\n");
103  return 1;
104  }
105  /*test av_grow_packet*/
106  if(av_grow_packet(avpkt_clone, 20) < 0){
107  av_log(NULL, AV_LOG_ERROR, "av_grow_packet failed\n");
108  return 1;
109  }
110  if(av_grow_packet(avpkt_clone, INT_MAX) == 0){
111  printf( "av_grow_packet failed to return error "
112  "when \"grow_by\" parameter is too large.\n" );
113  ret = 1;
114  }
115  /* test size error check in av_new_packet*/
116  if(av_new_packet(avpkt_clone, INT_MAX) == 0){
117  printf( "av_new_packet failed to return error "
118  "when \"size\" parameter is too large.\n" );
119  ret = 1;
120  }
121  /*test size error check in av_packet_from_data*/
122  if(av_packet_from_data(avpkt_clone, avpkt_clone->data, INT_MAX) == 0){
123  printf("av_packet_from_data failed to return error "
124  "when \"size\" parameter is too large.\n" );
125  ret = 1;
126  }
127  /*clean up*/
128  av_packet_free(&avpkt_clone);
129  av_packet_free(&avpkt);
130 
131 
132  return ret;
133 }
uint8_t
Libavcodec external API header.
#define NULL
Definition: coverity.c:32
error code definitions
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition: packet.h:417
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:122
const char * av_packet_side_data_name(enum AVPacketSideDataType type)
Definition: avpacket.c:385
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:166
FF_ENABLE_DEPRECATION_WARNINGS int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:309
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
AVPacket * av_packet_clone(const AVPacket *src)
Create a new packet that references the same data as src.
Definition: avpacket.c:677
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char data[16]
Definition: mxf.c:142
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
#define av_malloc(s)
#define av_log(a,...)
int main(void)
Definition: avpacket.c:80
static int initializations(AVPacket *avpkt)
Definition: avpacket.c:61
static int setup_side_data_entry(AVPacket *avpkt)
Definition: avpacket.c:28