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
35
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 {
45
46 if (!buf_size)
49
50 /* copy internal buffer data to buf */
51 memcpy(buf, bd->
ptr, buf_size);
54
55 return buf_size;
56 }
57
58 int main(
int argc,
char *argv[])
59 {
63 size_t buffer_size, avio_ctx_buffer_size = 4096;
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 }
75
76 /* slurp file content into buffer */
79 goto end;
80
81 /* fill opaque structure used by the AVIOContext read callback */
83 bd.
size = buffer_size;
84
87 goto end;
88 }
89
90 avio_ctx_buffer =
av_malloc(avio_ctx_buffer_size);
91 if (!avio_ctx_buffer) {
93 goto end;
94 }
97 if (!avio_ctx) {
99 goto end;
100 }
102
105 fprintf(stderr, "Could not open input\n");
106 goto end;
107 }
108
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)
124
126
129 return 1;
130 }
131
132 return 0;
133 }