FFmpeg
avio_read_callback.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Stefano Sabatini
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 /**
24  * @file libavformat AVIOContext read callback API usage example
25  * @example avio_read_callback.c
26  *
27  * Make libavformat demuxer access media content through a custom
28  * AVIOContext read callback.
29  */
30 
31 #include <libavcodec/avcodec.h>
32 #include <libavformat/avformat.h>
33 #include <libavformat/avio.h>
34 #include <libavutil/file.h>
35 
36 struct buffer_data {
37  uint8_t *ptr;
38  size_t size; ///< size left in the buffer
39 };
40 
41 static int read_packet(void *opaque, uint8_t *buf, int buf_size)
42 {
43  struct buffer_data *bd = (struct buffer_data *)opaque;
44  buf_size = FFMIN(buf_size, bd->size);
45 
46  if (!buf_size)
47  return AVERROR_EOF;
48  printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
49 
50  /* copy internal buffer data to buf */
51  memcpy(buf, bd->ptr, buf_size);
52  bd->ptr += buf_size;
53  bd->size -= buf_size;
54 
55  return buf_size;
56 }
57 
58 int main(int argc, char *argv[])
59 {
61  AVIOContext *avio_ctx = NULL;
62  uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
63  size_t buffer_size, avio_ctx_buffer_size = 4096;
64  char *input_filename = NULL;
65  int ret = 0;
66  struct buffer_data bd = { 0 };
67 
68  if (argc != 2) {
69  fprintf(stderr, "usage: %s input_file\n"
70  "API example program to show how to read from a custom buffer "
71  "accessed through AVIOContext.\n", argv[0]);
72  return 1;
73  }
74  input_filename = argv[1];
75 
76  /* slurp file content into buffer */
77  ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
78  if (ret < 0)
79  goto end;
80 
81  /* fill opaque structure used by the AVIOContext read callback */
82  bd.ptr = buffer;
83  bd.size = buffer_size;
84 
85  if (!(fmt_ctx = avformat_alloc_context())) {
86  ret = AVERROR(ENOMEM);
87  goto end;
88  }
89 
90  avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
91  if (!avio_ctx_buffer) {
92  ret = AVERROR(ENOMEM);
93  goto end;
94  }
95  avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
96  0, &bd, &read_packet, NULL, NULL);
97  if (!avio_ctx) {
98  ret = AVERROR(ENOMEM);
99  goto end;
100  }
101  fmt_ctx->pb = avio_ctx;
102 
104  if (ret < 0) {
105  fprintf(stderr, "Could not open input\n");
106  goto end;
107  }
108 
110  if (ret < 0) {
111  fprintf(stderr, "Could not find stream information\n");
112  goto end;
113  }
114 
116 
117 end:
119 
120  /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
121  if (avio_ctx)
122  av_freep(&avio_ctx->buffer);
123  avio_context_free(&avio_ctx);
124 
125  av_file_unmap(buffer, buffer_size);
126 
127  if (ret < 0) {
128  fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
129  return 1;
130  }
131 
132  return 0;
133 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
avio_context_free
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition: aviobuf.c:125
avio_alloc_context
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:108
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: demux.c:362
av_file_map
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition: file.c:55
av_dump_format
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate,...
Definition: dump.c:760
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: demux.c:214
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
main
int main(int argc, char *argv[])
Definition: avio_read_callback.c:58
av_file_unmap
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:146
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
NULL
#define NULL
Definition: coverity.c:32
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1297
buffer_data::size
size_t size
size left in the buffer
Definition: avio_read_callback.c:38
buffer_data::ptr
uint8_t * ptr
Definition: avio_read_callback.c:37
avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: demux.c:2499
fmt_ctx
static AVFormatContext * fmt_ctx
Definition: decode_filter_audio.c:45
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:160
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
avio.h
printf
printf("static const uint8_t my_array[100] = {\n")
buffer_data
Definition: avio_read_callback.c:36
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
avcodec.h
ret
ret
Definition: filter_design.txt:187
avformat.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
file.h
input_filename
static const char * input_filename
Definition: ffplay.c:309
AVIOContext::buffer
unsigned char * buffer
Start of the buffer.
Definition: avio.h:225
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34