FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avio_reading.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
25  * libavformat AVIOContext API example.
26  *
27  * Make libavformat demuxer access media content through a custom
28  * AVIOContext read callback.
29  * @example avio_reading.c
30  */
31 
32 #include <libavcodec/avcodec.h>
33 #include <libavformat/avformat.h>
34 #include <libavformat/avio.h>
35 #include <libavutil/file.h>
36 
37 struct buffer_data {
39  size_t size; ///< size left in the buffer
40 };
41 
42 static int read_packet(void *opaque, uint8_t *buf, int buf_size)
43 {
44  struct buffer_data *bd = (struct buffer_data *)opaque;
45  buf_size = FFMIN(buf_size, bd->size);
46 
47  if (!buf_size)
48  return AVERROR_EOF;
49  printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
50 
51  /* copy internal buffer data to buf */
52  memcpy(buf, bd->ptr, buf_size);
53  bd->ptr += buf_size;
54  bd->size -= buf_size;
55 
56  return buf_size;
57 }
58 
59 int main(int argc, char *argv[])
60 {
62  AVIOContext *avio_ctx = NULL;
63  uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
64  size_t buffer_size, avio_ctx_buffer_size = 4096;
65  char *input_filename = NULL;
66  int ret = 0;
67  struct buffer_data bd = { 0 };
68 
69  if (argc != 2) {
70  fprintf(stderr, "usage: %s input_file\n"
71  "API example program to show how to read from a custom buffer "
72  "accessed through AVIOContext.\n", argv[0]);
73  return 1;
74  }
75  input_filename = argv[1];
76 
77  /* register codecs and formats and other lavf/lavc components*/
79 
80  /* slurp file content into buffer */
81  ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);
82  if (ret < 0)
83  goto end;
84 
85  /* fill opaque structure used by the AVIOContext read callback */
86  bd.ptr = buffer;
87  bd.size = buffer_size;
88 
89  if (!(fmt_ctx = avformat_alloc_context())) {
90  ret = AVERROR(ENOMEM);
91  goto end;
92  }
93 
94  avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
95  if (!avio_ctx_buffer) {
96  ret = AVERROR(ENOMEM);
97  goto end;
98  }
99  avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
100  0, &bd, &read_packet, NULL, NULL);
101  if (!avio_ctx) {
102  ret = AVERROR(ENOMEM);
103  goto end;
104  }
105  fmt_ctx->pb = avio_ctx;
106 
107  ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);
108  if (ret < 0) {
109  fprintf(stderr, "Could not open input\n");
110  goto end;
111  }
112 
113  ret = avformat_find_stream_info(fmt_ctx, NULL);
114  if (ret < 0) {
115  fprintf(stderr, "Could not find stream information\n");
116  goto end;
117  }
118 
119  av_dump_format(fmt_ctx, 0, input_filename, 0);
120 
121 end:
122  avformat_close_input(&fmt_ctx);
123  /* note: the internal buffer could have changed, and be != avio_ctx_buffer */
124  if (avio_ctx) {
125  av_freep(&avio_ctx->buffer);
126  av_freep(&avio_ctx);
127  }
128  av_file_unmap(buffer, buffer_size);
129 
130  if (ret < 0) {
131  fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
132  return 1;
133  }
134 
135  return 0;
136 }
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
Buffered I/O operations.
static AVFormatContext * fmt_ctx
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
Format I/O context.
Definition: avformat.h:1325
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:144
Misc file utilities.
#define AVERROR_EOF
End of file.
Definition: error.h:55
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, streams, container, programs, metadata, side data, codec and time base.
Definition: dump.c:555
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, 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:129
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:133
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:53
#define AVERROR(e)
Definition: error.h:43
size_t size
size left in the buffer
Definition: avio_reading.c:39
#define FFMIN(a, b)
Definition: common.h:96
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
uint8_t * ptr
Definition: avio_reading.c:38
static const char * input_filename
Definition: ffplay.c:311
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
Libavcodec external API header.
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
int main(int argc, char *argv[])
Definition: avio_reading.c:59
void * buf
Definition: avisynth_c.h:690
Main libavformat public API header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3497
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4350
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:515
#define av_freep(p)
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:395
GLuint buffer
Definition: opengl_enc.c:102