pub fn AVERROR(e: c_int) -> c_intExamples found in repository?
examples/avio_reading.rs (line 107)
61fn main() {
62 unsafe {
63 let mut avio_ctx: *mut AVIOContext = std::ptr::null_mut();
64 let mut avio_ctx_buffer: *mut u8 = std::ptr::null_mut();
65 let avio_ctx_buffer_size: usize = 4096;
66 let mut buffer: *mut u8 = std::ptr::null_mut();
67 let mut buffer_size: usize = 0;
68 let mut fmt_ctx: *mut AVFormatContext = std::ptr::null_mut();
69 let mut ret: i32 = 0;
70 let mut bd = BufferData {
71 ptr: std::ptr::null_mut(),
72 size: 0,
73 };
74
75 let args = env::args().collect::<Vec<_>>();
76 if args.len() != 2 {
77 println!(
78 "usage: {} input_file\n\
79 API example program to show how to read from a custom buffer \
80 accessed through AVIOContext.",
81 args[0]
82 );
83 std::process::exit(-1);
84 }
85
86 let input_filename = std::ffi::CString::new(args[1].clone()).unwrap();
87
88 loop {
89 // slurp file content into buffer
90 ret = av_file_map(
91 input_filename.as_ptr(),
92 &mut buffer,
93 &mut buffer_size,
94 0,
95 std::ptr::null_mut(),
96 );
97 if ret < 0 {
98 break;
99 }
100
101 // fill opaque structure used by the AVIOContext read callback
102 bd.ptr = buffer;
103 bd.size = buffer_size;
104
105 fmt_ctx = avformat_alloc_context();
106 if fmt_ctx.is_null() {
107 ret = AVERROR(ENOMEM);
108 break;
109 }
110
111 avio_ctx_buffer = av_malloc(avio_ctx_buffer_size) as *mut u8;
112 if avio_ctx_buffer.is_null() {
113 ret = AVERROR(ENOMEM);
114 break;
115 }
116 avio_ctx = avio_alloc_context(
117 avio_ctx_buffer,
118 avio_ctx_buffer_size.try_into().unwrap(),
119 0,
120 &mut bd as *mut BufferData as *mut c_void,
121 Some(read_packet),
122 None,
123 None,
124 );
125 if avio_ctx.is_null() {
126 ret = AVERROR(ENOMEM);
127 break;
128 }
129 (*fmt_ctx).pb = avio_ctx;
130
131 ret = avformat_open_input(
132 &mut fmt_ctx,
133 std::ptr::null(),
134 std::ptr::null_mut(),
135 std::ptr::null_mut(),
136 );
137 if ret < 0 {
138 println!("Could not open input!");
139 break;
140 }
141
142 ret = avformat_find_stream_info(fmt_ctx, std::ptr::null_mut());
143 if ret < 0 {
144 println!("Could not find stream information!");
145 break;
146 }
147
148 av_dump_format(fmt_ctx, 0, input_filename.as_ptr(), 0);
149
150 break;
151 }
152
153 avformat_close_input(&mut fmt_ctx);
154
155 // note: the internal buffer could have changed, and be != avio_ctx_buffer
156 if !avio_ctx.is_null() {
157 av_freep(std::mem::transmute::<*mut *mut u8, *mut c_void>(
158 &mut (*avio_ctx).buffer,
159 ));
160 }
161 avio_context_free(&mut avio_ctx);
162
163 av_file_unmap(buffer, buffer_size);
164
165 if ret < 0 {
166 println!("Error occurred: {:?}", av_err2str(ret));
167 std::process::exit(-2);
168 }
169 }
170}