Restore the old name, which was more meaningful and consistent with the
names of the other examples.
... | ... |
@@ -12,7 +12,7 @@ CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS) |
12 | 12 |
LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS) |
13 | 13 |
|
14 | 14 |
EXAMPLES= avio_reading \ |
15 |
- avcodec \ |
|
15 |
+ decoding_encoding \ |
|
16 | 16 |
demuxing_decoding \ |
17 | 17 |
filtering_video \ |
18 | 18 |
filtering_audio \ |
19 | 19 |
deleted file mode 100644 |
... | ... |
@@ -1,664 +0,0 @@ |
1 |
-/* |
|
2 |
- * Copyright (c) 2001 Fabrice Bellard |
|
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 |
- * libavcodec API use example. |
|
26 |
- * |
|
27 |
- * @example avcodec.c |
|
28 |
- * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...), |
|
29 |
- * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the |
|
30 |
- * format handling |
|
31 |
- */ |
|
32 |
- |
|
33 |
-#include <math.h> |
|
34 |
- |
|
35 |
-#include <libavutil/opt.h> |
|
36 |
-#include <libavcodec/avcodec.h> |
|
37 |
-#include <libavutil/channel_layout.h> |
|
38 |
-#include <libavutil/common.h> |
|
39 |
-#include <libavutil/imgutils.h> |
|
40 |
-#include <libavutil/mathematics.h> |
|
41 |
-#include <libavutil/samplefmt.h> |
|
42 |
- |
|
43 |
-#define INBUF_SIZE 4096 |
|
44 |
-#define AUDIO_INBUF_SIZE 20480 |
|
45 |
-#define AUDIO_REFILL_THRESH 4096 |
|
46 |
- |
|
47 |
-/* check that a given sample format is supported by the encoder */ |
|
48 |
-static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt) |
|
49 |
-{ |
|
50 |
- const enum AVSampleFormat *p = codec->sample_fmts; |
|
51 |
- |
|
52 |
- while (*p != AV_SAMPLE_FMT_NONE) { |
|
53 |
- if (*p == sample_fmt) |
|
54 |
- return 1; |
|
55 |
- p++; |
|
56 |
- } |
|
57 |
- return 0; |
|
58 |
-} |
|
59 |
- |
|
60 |
-/* just pick the highest supported samplerate */ |
|
61 |
-static int select_sample_rate(AVCodec *codec) |
|
62 |
-{ |
|
63 |
- const int *p; |
|
64 |
- int best_samplerate = 0; |
|
65 |
- |
|
66 |
- if (!codec->supported_samplerates) |
|
67 |
- return 44100; |
|
68 |
- |
|
69 |
- p = codec->supported_samplerates; |
|
70 |
- while (*p) { |
|
71 |
- best_samplerate = FFMAX(*p, best_samplerate); |
|
72 |
- p++; |
|
73 |
- } |
|
74 |
- return best_samplerate; |
|
75 |
-} |
|
76 |
- |
|
77 |
-/* select layout with the highest channel count */ |
|
78 |
-static int select_channel_layout(AVCodec *codec) |
|
79 |
-{ |
|
80 |
- const uint64_t *p; |
|
81 |
- uint64_t best_ch_layout = 0; |
|
82 |
- int best_nb_channels = 0; |
|
83 |
- |
|
84 |
- if (!codec->channel_layouts) |
|
85 |
- return AV_CH_LAYOUT_STEREO; |
|
86 |
- |
|
87 |
- p = codec->channel_layouts; |
|
88 |
- while (*p) { |
|
89 |
- int nb_channels = av_get_channel_layout_nb_channels(*p); |
|
90 |
- |
|
91 |
- if (nb_channels > best_nb_channels) { |
|
92 |
- best_ch_layout = *p; |
|
93 |
- best_nb_channels = nb_channels; |
|
94 |
- } |
|
95 |
- p++; |
|
96 |
- } |
|
97 |
- return best_ch_layout; |
|
98 |
-} |
|
99 |
- |
|
100 |
-/* |
|
101 |
- * Audio encoding example |
|
102 |
- */ |
|
103 |
-static void audio_encode_example(const char *filename) |
|
104 |
-{ |
|
105 |
- AVCodec *codec; |
|
106 |
- AVCodecContext *c= NULL; |
|
107 |
- AVFrame *frame; |
|
108 |
- AVPacket pkt; |
|
109 |
- int i, j, k, ret, got_output; |
|
110 |
- int buffer_size; |
|
111 |
- FILE *f; |
|
112 |
- uint16_t *samples; |
|
113 |
- float t, tincr; |
|
114 |
- |
|
115 |
- printf("Encode audio file %s\n", filename); |
|
116 |
- |
|
117 |
- /* find the MP2 encoder */ |
|
118 |
- codec = avcodec_find_encoder(AV_CODEC_ID_MP2); |
|
119 |
- if (!codec) { |
|
120 |
- fprintf(stderr, "Codec not found\n"); |
|
121 |
- exit(1); |
|
122 |
- } |
|
123 |
- |
|
124 |
- c = avcodec_alloc_context3(codec); |
|
125 |
- if (!c) { |
|
126 |
- fprintf(stderr, "Could not allocate audio codec context\n"); |
|
127 |
- exit(1); |
|
128 |
- } |
|
129 |
- |
|
130 |
- /* put sample parameters */ |
|
131 |
- c->bit_rate = 64000; |
|
132 |
- |
|
133 |
- /* check that the encoder supports s16 pcm input */ |
|
134 |
- c->sample_fmt = AV_SAMPLE_FMT_S16; |
|
135 |
- if (!check_sample_fmt(codec, c->sample_fmt)) { |
|
136 |
- fprintf(stderr, "Encoder does not support sample format %s", |
|
137 |
- av_get_sample_fmt_name(c->sample_fmt)); |
|
138 |
- exit(1); |
|
139 |
- } |
|
140 |
- |
|
141 |
- /* select other audio parameters supported by the encoder */ |
|
142 |
- c->sample_rate = select_sample_rate(codec); |
|
143 |
- c->channel_layout = select_channel_layout(codec); |
|
144 |
- c->channels = av_get_channel_layout_nb_channels(c->channel_layout); |
|
145 |
- |
|
146 |
- /* open it */ |
|
147 |
- if (avcodec_open2(c, codec, NULL) < 0) { |
|
148 |
- fprintf(stderr, "Could not open codec\n"); |
|
149 |
- exit(1); |
|
150 |
- } |
|
151 |
- |
|
152 |
- f = fopen(filename, "wb"); |
|
153 |
- if (!f) { |
|
154 |
- fprintf(stderr, "Could not open %s\n", filename); |
|
155 |
- exit(1); |
|
156 |
- } |
|
157 |
- |
|
158 |
- /* frame containing input raw audio */ |
|
159 |
- frame = av_frame_alloc(); |
|
160 |
- if (!frame) { |
|
161 |
- fprintf(stderr, "Could not allocate audio frame\n"); |
|
162 |
- exit(1); |
|
163 |
- } |
|
164 |
- |
|
165 |
- frame->nb_samples = c->frame_size; |
|
166 |
- frame->format = c->sample_fmt; |
|
167 |
- frame->channel_layout = c->channel_layout; |
|
168 |
- |
|
169 |
- /* the codec gives us the frame size, in samples, |
|
170 |
- * we calculate the size of the samples buffer in bytes */ |
|
171 |
- buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size, |
|
172 |
- c->sample_fmt, 0); |
|
173 |
- if (buffer_size < 0) { |
|
174 |
- fprintf(stderr, "Could not get sample buffer size\n"); |
|
175 |
- exit(1); |
|
176 |
- } |
|
177 |
- samples = av_malloc(buffer_size); |
|
178 |
- if (!samples) { |
|
179 |
- fprintf(stderr, "Could not allocate %d bytes for samples buffer\n", |
|
180 |
- buffer_size); |
|
181 |
- exit(1); |
|
182 |
- } |
|
183 |
- /* setup the data pointers in the AVFrame */ |
|
184 |
- ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, |
|
185 |
- (const uint8_t*)samples, buffer_size, 0); |
|
186 |
- if (ret < 0) { |
|
187 |
- fprintf(stderr, "Could not setup audio frame\n"); |
|
188 |
- exit(1); |
|
189 |
- } |
|
190 |
- |
|
191 |
- /* encode a single tone sound */ |
|
192 |
- t = 0; |
|
193 |
- tincr = 2 * M_PI * 440.0 / c->sample_rate; |
|
194 |
- for (i = 0; i < 200; i++) { |
|
195 |
- av_init_packet(&pkt); |
|
196 |
- pkt.data = NULL; // packet data will be allocated by the encoder |
|
197 |
- pkt.size = 0; |
|
198 |
- |
|
199 |
- for (j = 0; j < c->frame_size; j++) { |
|
200 |
- samples[2*j] = (int)(sin(t) * 10000); |
|
201 |
- |
|
202 |
- for (k = 1; k < c->channels; k++) |
|
203 |
- samples[2*j + k] = samples[2*j]; |
|
204 |
- t += tincr; |
|
205 |
- } |
|
206 |
- /* encode the samples */ |
|
207 |
- ret = avcodec_encode_audio2(c, &pkt, frame, &got_output); |
|
208 |
- if (ret < 0) { |
|
209 |
- fprintf(stderr, "Error encoding audio frame\n"); |
|
210 |
- exit(1); |
|
211 |
- } |
|
212 |
- if (got_output) { |
|
213 |
- fwrite(pkt.data, 1, pkt.size, f); |
|
214 |
- av_free_packet(&pkt); |
|
215 |
- } |
|
216 |
- } |
|
217 |
- |
|
218 |
- /* get the delayed frames */ |
|
219 |
- for (got_output = 1; got_output; i++) { |
|
220 |
- ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output); |
|
221 |
- if (ret < 0) { |
|
222 |
- fprintf(stderr, "Error encoding frame\n"); |
|
223 |
- exit(1); |
|
224 |
- } |
|
225 |
- |
|
226 |
- if (got_output) { |
|
227 |
- fwrite(pkt.data, 1, pkt.size, f); |
|
228 |
- av_free_packet(&pkt); |
|
229 |
- } |
|
230 |
- } |
|
231 |
- fclose(f); |
|
232 |
- |
|
233 |
- av_freep(&samples); |
|
234 |
- av_frame_free(&frame); |
|
235 |
- avcodec_close(c); |
|
236 |
- av_free(c); |
|
237 |
-} |
|
238 |
- |
|
239 |
-/* |
|
240 |
- * Audio decoding. |
|
241 |
- */ |
|
242 |
-static void audio_decode_example(const char *outfilename, const char *filename) |
|
243 |
-{ |
|
244 |
- AVCodec *codec; |
|
245 |
- AVCodecContext *c= NULL; |
|
246 |
- int len; |
|
247 |
- FILE *f, *outfile; |
|
248 |
- uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
|
249 |
- AVPacket avpkt; |
|
250 |
- AVFrame *decoded_frame = NULL; |
|
251 |
- |
|
252 |
- av_init_packet(&avpkt); |
|
253 |
- |
|
254 |
- printf("Decode audio file %s to %s\n", filename, outfilename); |
|
255 |
- |
|
256 |
- /* find the mpeg audio decoder */ |
|
257 |
- codec = avcodec_find_decoder(AV_CODEC_ID_MP2); |
|
258 |
- if (!codec) { |
|
259 |
- fprintf(stderr, "Codec not found\n"); |
|
260 |
- exit(1); |
|
261 |
- } |
|
262 |
- |
|
263 |
- c = avcodec_alloc_context3(codec); |
|
264 |
- if (!c) { |
|
265 |
- fprintf(stderr, "Could not allocate audio codec context\n"); |
|
266 |
- exit(1); |
|
267 |
- } |
|
268 |
- |
|
269 |
- /* open it */ |
|
270 |
- if (avcodec_open2(c, codec, NULL) < 0) { |
|
271 |
- fprintf(stderr, "Could not open codec\n"); |
|
272 |
- exit(1); |
|
273 |
- } |
|
274 |
- |
|
275 |
- f = fopen(filename, "rb"); |
|
276 |
- if (!f) { |
|
277 |
- fprintf(stderr, "Could not open %s\n", filename); |
|
278 |
- exit(1); |
|
279 |
- } |
|
280 |
- outfile = fopen(outfilename, "wb"); |
|
281 |
- if (!outfile) { |
|
282 |
- av_free(c); |
|
283 |
- exit(1); |
|
284 |
- } |
|
285 |
- |
|
286 |
- /* decode until eof */ |
|
287 |
- avpkt.data = inbuf; |
|
288 |
- avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); |
|
289 |
- |
|
290 |
- while (avpkt.size > 0) { |
|
291 |
- int got_frame = 0; |
|
292 |
- |
|
293 |
- if (!decoded_frame) { |
|
294 |
- if (!(decoded_frame = av_frame_alloc())) { |
|
295 |
- fprintf(stderr, "Could not allocate audio frame\n"); |
|
296 |
- exit(1); |
|
297 |
- } |
|
298 |
- } |
|
299 |
- |
|
300 |
- len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt); |
|
301 |
- if (len < 0) { |
|
302 |
- fprintf(stderr, "Error while decoding\n"); |
|
303 |
- exit(1); |
|
304 |
- } |
|
305 |
- if (got_frame) { |
|
306 |
- /* if a frame has been decoded, output it */ |
|
307 |
- int data_size = av_samples_get_buffer_size(NULL, c->channels, |
|
308 |
- decoded_frame->nb_samples, |
|
309 |
- c->sample_fmt, 1); |
|
310 |
- if (data_size < 0) { |
|
311 |
- /* This should not occur, checking just for paranoia */ |
|
312 |
- fprintf(stderr, "Failed to calculate data size\n"); |
|
313 |
- exit(1); |
|
314 |
- } |
|
315 |
- fwrite(decoded_frame->data[0], 1, data_size, outfile); |
|
316 |
- } |
|
317 |
- avpkt.size -= len; |
|
318 |
- avpkt.data += len; |
|
319 |
- avpkt.dts = |
|
320 |
- avpkt.pts = AV_NOPTS_VALUE; |
|
321 |
- if (avpkt.size < AUDIO_REFILL_THRESH) { |
|
322 |
- /* Refill the input buffer, to avoid trying to decode |
|
323 |
- * incomplete frames. Instead of this, one could also use |
|
324 |
- * a parser, or use a proper container format through |
|
325 |
- * libavformat. */ |
|
326 |
- memmove(inbuf, avpkt.data, avpkt.size); |
|
327 |
- avpkt.data = inbuf; |
|
328 |
- len = fread(avpkt.data + avpkt.size, 1, |
|
329 |
- AUDIO_INBUF_SIZE - avpkt.size, f); |
|
330 |
- if (len > 0) |
|
331 |
- avpkt.size += len; |
|
332 |
- } |
|
333 |
- } |
|
334 |
- |
|
335 |
- fclose(outfile); |
|
336 |
- fclose(f); |
|
337 |
- |
|
338 |
- avcodec_close(c); |
|
339 |
- av_free(c); |
|
340 |
- av_frame_free(&decoded_frame); |
|
341 |
-} |
|
342 |
- |
|
343 |
-/* |
|
344 |
- * Video encoding example |
|
345 |
- */ |
|
346 |
-static void video_encode_example(const char *filename, int codec_id) |
|
347 |
-{ |
|
348 |
- AVCodec *codec; |
|
349 |
- AVCodecContext *c= NULL; |
|
350 |
- int i, ret, x, y, got_output; |
|
351 |
- FILE *f; |
|
352 |
- AVFrame *frame; |
|
353 |
- AVPacket pkt; |
|
354 |
- uint8_t endcode[] = { 0, 0, 1, 0xb7 }; |
|
355 |
- |
|
356 |
- printf("Encode video file %s\n", filename); |
|
357 |
- |
|
358 |
- /* find the mpeg1 video encoder */ |
|
359 |
- codec = avcodec_find_encoder(codec_id); |
|
360 |
- if (!codec) { |
|
361 |
- fprintf(stderr, "Codec not found\n"); |
|
362 |
- exit(1); |
|
363 |
- } |
|
364 |
- |
|
365 |
- c = avcodec_alloc_context3(codec); |
|
366 |
- if (!c) { |
|
367 |
- fprintf(stderr, "Could not allocate video codec context\n"); |
|
368 |
- exit(1); |
|
369 |
- } |
|
370 |
- |
|
371 |
- /* put sample parameters */ |
|
372 |
- c->bit_rate = 400000; |
|
373 |
- /* resolution must be a multiple of two */ |
|
374 |
- c->width = 352; |
|
375 |
- c->height = 288; |
|
376 |
- /* frames per second */ |
|
377 |
- c->time_base = (AVRational){1,25}; |
|
378 |
- /* emit one intra frame every ten frames |
|
379 |
- * check frame pict_type before passing frame |
|
380 |
- * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I |
|
381 |
- * then gop_size is ignored and the output of encoder |
|
382 |
- * will always be I frame irrespective to gop_size |
|
383 |
- */ |
|
384 |
- c->gop_size = 10; |
|
385 |
- c->max_b_frames = 1; |
|
386 |
- c->pix_fmt = AV_PIX_FMT_YUV420P; |
|
387 |
- |
|
388 |
- if (codec_id == AV_CODEC_ID_H264) |
|
389 |
- av_opt_set(c->priv_data, "preset", "slow", 0); |
|
390 |
- |
|
391 |
- /* open it */ |
|
392 |
- if (avcodec_open2(c, codec, NULL) < 0) { |
|
393 |
- fprintf(stderr, "Could not open codec\n"); |
|
394 |
- exit(1); |
|
395 |
- } |
|
396 |
- |
|
397 |
- f = fopen(filename, "wb"); |
|
398 |
- if (!f) { |
|
399 |
- fprintf(stderr, "Could not open %s\n", filename); |
|
400 |
- exit(1); |
|
401 |
- } |
|
402 |
- |
|
403 |
- frame = av_frame_alloc(); |
|
404 |
- if (!frame) { |
|
405 |
- fprintf(stderr, "Could not allocate video frame\n"); |
|
406 |
- exit(1); |
|
407 |
- } |
|
408 |
- frame->format = c->pix_fmt; |
|
409 |
- frame->width = c->width; |
|
410 |
- frame->height = c->height; |
|
411 |
- |
|
412 |
- /* the image can be allocated by any means and av_image_alloc() is |
|
413 |
- * just the most convenient way if av_malloc() is to be used */ |
|
414 |
- ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, |
|
415 |
- c->pix_fmt, 32); |
|
416 |
- if (ret < 0) { |
|
417 |
- fprintf(stderr, "Could not allocate raw picture buffer\n"); |
|
418 |
- exit(1); |
|
419 |
- } |
|
420 |
- |
|
421 |
- /* encode 1 second of video */ |
|
422 |
- for (i = 0; i < 25; i++) { |
|
423 |
- av_init_packet(&pkt); |
|
424 |
- pkt.data = NULL; // packet data will be allocated by the encoder |
|
425 |
- pkt.size = 0; |
|
426 |
- |
|
427 |
- fflush(stdout); |
|
428 |
- /* prepare a dummy image */ |
|
429 |
- /* Y */ |
|
430 |
- for (y = 0; y < c->height; y++) { |
|
431 |
- for (x = 0; x < c->width; x++) { |
|
432 |
- frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3; |
|
433 |
- } |
|
434 |
- } |
|
435 |
- |
|
436 |
- /* Cb and Cr */ |
|
437 |
- for (y = 0; y < c->height/2; y++) { |
|
438 |
- for (x = 0; x < c->width/2; x++) { |
|
439 |
- frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2; |
|
440 |
- frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5; |
|
441 |
- } |
|
442 |
- } |
|
443 |
- |
|
444 |
- frame->pts = i; |
|
445 |
- |
|
446 |
- /* encode the image */ |
|
447 |
- ret = avcodec_encode_video2(c, &pkt, frame, &got_output); |
|
448 |
- if (ret < 0) { |
|
449 |
- fprintf(stderr, "Error encoding frame\n"); |
|
450 |
- exit(1); |
|
451 |
- } |
|
452 |
- |
|
453 |
- if (got_output) { |
|
454 |
- printf("Write frame %3d (size=%5d)\n", i, pkt.size); |
|
455 |
- fwrite(pkt.data, 1, pkt.size, f); |
|
456 |
- av_free_packet(&pkt); |
|
457 |
- } |
|
458 |
- } |
|
459 |
- |
|
460 |
- /* get the delayed frames */ |
|
461 |
- for (got_output = 1; got_output; i++) { |
|
462 |
- fflush(stdout); |
|
463 |
- |
|
464 |
- ret = avcodec_encode_video2(c, &pkt, NULL, &got_output); |
|
465 |
- if (ret < 0) { |
|
466 |
- fprintf(stderr, "Error encoding frame\n"); |
|
467 |
- exit(1); |
|
468 |
- } |
|
469 |
- |
|
470 |
- if (got_output) { |
|
471 |
- printf("Write frame %3d (size=%5d)\n", i, pkt.size); |
|
472 |
- fwrite(pkt.data, 1, pkt.size, f); |
|
473 |
- av_free_packet(&pkt); |
|
474 |
- } |
|
475 |
- } |
|
476 |
- |
|
477 |
- /* add sequence end code to have a real mpeg file */ |
|
478 |
- fwrite(endcode, 1, sizeof(endcode), f); |
|
479 |
- fclose(f); |
|
480 |
- |
|
481 |
- avcodec_close(c); |
|
482 |
- av_free(c); |
|
483 |
- av_freep(&frame->data[0]); |
|
484 |
- av_frame_free(&frame); |
|
485 |
- printf("\n"); |
|
486 |
-} |
|
487 |
- |
|
488 |
-/* |
|
489 |
- * Video decoding example |
|
490 |
- */ |
|
491 |
- |
|
492 |
-static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, |
|
493 |
- char *filename) |
|
494 |
-{ |
|
495 |
- FILE *f; |
|
496 |
- int i; |
|
497 |
- |
|
498 |
- f = fopen(filename,"w"); |
|
499 |
- fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255); |
|
500 |
- for (i = 0; i < ysize; i++) |
|
501 |
- fwrite(buf + i * wrap, 1, xsize, f); |
|
502 |
- fclose(f); |
|
503 |
-} |
|
504 |
- |
|
505 |
-static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, |
|
506 |
- AVFrame *frame, int *frame_count, AVPacket *pkt, int last) |
|
507 |
-{ |
|
508 |
- int len, got_frame; |
|
509 |
- char buf[1024]; |
|
510 |
- |
|
511 |
- len = avcodec_decode_video2(avctx, frame, &got_frame, pkt); |
|
512 |
- if (len < 0) { |
|
513 |
- fprintf(stderr, "Error while decoding frame %d\n", *frame_count); |
|
514 |
- return len; |
|
515 |
- } |
|
516 |
- if (got_frame) { |
|
517 |
- printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count); |
|
518 |
- fflush(stdout); |
|
519 |
- |
|
520 |
- /* the picture is allocated by the decoder, no need to free it */ |
|
521 |
- snprintf(buf, sizeof(buf), outfilename, *frame_count); |
|
522 |
- pgm_save(frame->data[0], frame->linesize[0], |
|
523 |
- avctx->width, avctx->height, buf); |
|
524 |
- (*frame_count)++; |
|
525 |
- } |
|
526 |
- if (pkt->data) { |
|
527 |
- pkt->size -= len; |
|
528 |
- pkt->data += len; |
|
529 |
- } |
|
530 |
- return 0; |
|
531 |
-} |
|
532 |
- |
|
533 |
-static void video_decode_example(const char *outfilename, const char *filename) |
|
534 |
-{ |
|
535 |
- AVCodec *codec; |
|
536 |
- AVCodecContext *c= NULL; |
|
537 |
- int frame_count; |
|
538 |
- FILE *f; |
|
539 |
- AVFrame *frame; |
|
540 |
- uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
|
541 |
- AVPacket avpkt; |
|
542 |
- |
|
543 |
- av_init_packet(&avpkt); |
|
544 |
- |
|
545 |
- /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ |
|
546 |
- memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
|
547 |
- |
|
548 |
- printf("Decode video file %s to %s\n", filename, outfilename); |
|
549 |
- |
|
550 |
- /* find the mpeg1 video decoder */ |
|
551 |
- codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO); |
|
552 |
- if (!codec) { |
|
553 |
- fprintf(stderr, "Codec not found\n"); |
|
554 |
- exit(1); |
|
555 |
- } |
|
556 |
- |
|
557 |
- c = avcodec_alloc_context3(codec); |
|
558 |
- if (!c) { |
|
559 |
- fprintf(stderr, "Could not allocate video codec context\n"); |
|
560 |
- exit(1); |
|
561 |
- } |
|
562 |
- |
|
563 |
- if(codec->capabilities&CODEC_CAP_TRUNCATED) |
|
564 |
- c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */ |
|
565 |
- |
|
566 |
- /* For some codecs, such as msmpeg4 and mpeg4, width and height |
|
567 |
- MUST be initialized there because this information is not |
|
568 |
- available in the bitstream. */ |
|
569 |
- |
|
570 |
- /* open it */ |
|
571 |
- if (avcodec_open2(c, codec, NULL) < 0) { |
|
572 |
- fprintf(stderr, "Could not open codec\n"); |
|
573 |
- exit(1); |
|
574 |
- } |
|
575 |
- |
|
576 |
- f = fopen(filename, "rb"); |
|
577 |
- if (!f) { |
|
578 |
- fprintf(stderr, "Could not open %s\n", filename); |
|
579 |
- exit(1); |
|
580 |
- } |
|
581 |
- |
|
582 |
- frame = av_frame_alloc(); |
|
583 |
- if (!frame) { |
|
584 |
- fprintf(stderr, "Could not allocate video frame\n"); |
|
585 |
- exit(1); |
|
586 |
- } |
|
587 |
- |
|
588 |
- frame_count = 0; |
|
589 |
- for (;;) { |
|
590 |
- avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); |
|
591 |
- if (avpkt.size == 0) |
|
592 |
- break; |
|
593 |
- |
|
594 |
- /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) |
|
595 |
- and this is the only method to use them because you cannot |
|
596 |
- know the compressed data size before analysing it. |
|
597 |
- |
|
598 |
- BUT some other codecs (msmpeg4, mpeg4) are inherently frame |
|
599 |
- based, so you must call them with all the data for one |
|
600 |
- frame exactly. You must also initialize 'width' and |
|
601 |
- 'height' before initializing them. */ |
|
602 |
- |
|
603 |
- /* NOTE2: some codecs allow the raw parameters (frame size, |
|
604 |
- sample rate) to be changed at any frame. We handle this, so |
|
605 |
- you should also take care of it */ |
|
606 |
- |
|
607 |
- /* here, we use a stream based decoder (mpeg1video), so we |
|
608 |
- feed decoder and see if it could decode a frame */ |
|
609 |
- avpkt.data = inbuf; |
|
610 |
- while (avpkt.size > 0) |
|
611 |
- if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0) |
|
612 |
- exit(1); |
|
613 |
- } |
|
614 |
- |
|
615 |
- /* some codecs, such as MPEG, transmit the I and P frame with a |
|
616 |
- latency of one frame. You must do the following to have a |
|
617 |
- chance to get the last frame of the video */ |
|
618 |
- avpkt.data = NULL; |
|
619 |
- avpkt.size = 0; |
|
620 |
- decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1); |
|
621 |
- |
|
622 |
- fclose(f); |
|
623 |
- |
|
624 |
- avcodec_close(c); |
|
625 |
- av_free(c); |
|
626 |
- av_frame_free(&frame); |
|
627 |
- printf("\n"); |
|
628 |
-} |
|
629 |
- |
|
630 |
-int main(int argc, char **argv) |
|
631 |
-{ |
|
632 |
- const char *output_type; |
|
633 |
- |
|
634 |
- /* register all the codecs */ |
|
635 |
- avcodec_register_all(); |
|
636 |
- |
|
637 |
- if (argc < 2) { |
|
638 |
- printf("usage: %s output_type\n" |
|
639 |
- "API example program to decode/encode a media stream with libavcodec.\n" |
|
640 |
- "This program generates a synthetic stream and encodes it to a file\n" |
|
641 |
- "named test.h264, test.mp2 or test.mpg depending on output_type.\n" |
|
642 |
- "The encoded stream is then decoded and written to a raw data output.\n" |
|
643 |
- "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n", |
|
644 |
- argv[0]); |
|
645 |
- return 1; |
|
646 |
- } |
|
647 |
- output_type = argv[1]; |
|
648 |
- |
|
649 |
- if (!strcmp(output_type, "h264")) { |
|
650 |
- video_encode_example("test.h264", AV_CODEC_ID_H264); |
|
651 |
- } else if (!strcmp(output_type, "mp2")) { |
|
652 |
- audio_encode_example("test.mp2"); |
|
653 |
- audio_decode_example("test.sw", "test.mp2"); |
|
654 |
- } else if (!strcmp(output_type, "mpg")) { |
|
655 |
- video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO); |
|
656 |
- video_decode_example("test%02d.pgm", "test.mpg"); |
|
657 |
- } else { |
|
658 |
- fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n", |
|
659 |
- output_type); |
|
660 |
- return 1; |
|
661 |
- } |
|
662 |
- |
|
663 |
- return 0; |
|
664 |
-} |
665 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,664 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2001 Fabrice Bellard |
|
2 |
+ * |
|
3 |
+ * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
4 |
+ * of this software and associated documentation files (the "Software"), to deal |
|
5 |
+ * in the Software without restriction, including without limitation the rights |
|
6 |
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
7 |
+ * copies of the Software, and to permit persons to whom the Software is |
|
8 |
+ * furnished to do so, subject to the following conditions: |
|
9 |
+ * |
|
10 |
+ * The above copyright notice and this permission notice shall be included in |
|
11 |
+ * all copies or substantial portions of the Software. |
|
12 |
+ * |
|
13 |
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
14 |
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
15 |
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
16 |
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
17 |
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
18 |
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
19 |
+ * THE SOFTWARE. |
|
20 |
+ */ |
|
21 |
+ |
|
22 |
+/** |
|
23 |
+ * @file |
|
24 |
+ * libavcodec API use example. |
|
25 |
+ * |
|
26 |
+ * @example decoding_encoding.c |
|
27 |
+ * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...), |
|
28 |
+ * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the |
|
29 |
+ * format handling |
|
30 |
+ */ |
|
31 |
+ |
|
32 |
+#include <math.h> |
|
33 |
+ |
|
34 |
+#include <libavutil/opt.h> |
|
35 |
+#include <libavcodec/avcodec.h> |
|
36 |
+#include <libavutil/channel_layout.h> |
|
37 |
+#include <libavutil/common.h> |
|
38 |
+#include <libavutil/imgutils.h> |
|
39 |
+#include <libavutil/mathematics.h> |
|
40 |
+#include <libavutil/samplefmt.h> |
|
41 |
+ |
|
42 |
+#define INBUF_SIZE 4096 |
|
43 |
+#define AUDIO_INBUF_SIZE 20480 |
|
44 |
+#define AUDIO_REFILL_THRESH 4096 |
|
45 |
+ |
|
46 |
+/* check that a given sample format is supported by the encoder */ |
|
47 |
+static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt) |
|
48 |
+{ |
|
49 |
+ const enum AVSampleFormat *p = codec->sample_fmts; |
|
50 |
+ |
|
51 |
+ while (*p != AV_SAMPLE_FMT_NONE) { |
|
52 |
+ if (*p == sample_fmt) |
|
53 |
+ return 1; |
|
54 |
+ p++; |
|
55 |
+ } |
|
56 |
+ return 0; |
|
57 |
+} |
|
58 |
+ |
|
59 |
+/* just pick the highest supported samplerate */ |
|
60 |
+static int select_sample_rate(AVCodec *codec) |
|
61 |
+{ |
|
62 |
+ const int *p; |
|
63 |
+ int best_samplerate = 0; |
|
64 |
+ |
|
65 |
+ if (!codec->supported_samplerates) |
|
66 |
+ return 44100; |
|
67 |
+ |
|
68 |
+ p = codec->supported_samplerates; |
|
69 |
+ while (*p) { |
|
70 |
+ best_samplerate = FFMAX(*p, best_samplerate); |
|
71 |
+ p++; |
|
72 |
+ } |
|
73 |
+ return best_samplerate; |
|
74 |
+} |
|
75 |
+ |
|
76 |
+/* select layout with the highest channel count */ |
|
77 |
+static int select_channel_layout(AVCodec *codec) |
|
78 |
+{ |
|
79 |
+ const uint64_t *p; |
|
80 |
+ uint64_t best_ch_layout = 0; |
|
81 |
+ int best_nb_channels = 0; |
|
82 |
+ |
|
83 |
+ if (!codec->channel_layouts) |
|
84 |
+ return AV_CH_LAYOUT_STEREO; |
|
85 |
+ |
|
86 |
+ p = codec->channel_layouts; |
|
87 |
+ while (*p) { |
|
88 |
+ int nb_channels = av_get_channel_layout_nb_channels(*p); |
|
89 |
+ |
|
90 |
+ if (nb_channels > best_nb_channels) { |
|
91 |
+ best_ch_layout = *p; |
|
92 |
+ best_nb_channels = nb_channels; |
|
93 |
+ } |
|
94 |
+ p++; |
|
95 |
+ } |
|
96 |
+ return best_ch_layout; |
|
97 |
+} |
|
98 |
+ |
|
99 |
+/* |
|
100 |
+ * Audio encoding example |
|
101 |
+ */ |
|
102 |
+static void audio_encode_example(const char *filename) |
|
103 |
+{ |
|
104 |
+ AVCodec *codec; |
|
105 |
+ AVCodecContext *c= NULL; |
|
106 |
+ AVFrame *frame; |
|
107 |
+ AVPacket pkt; |
|
108 |
+ int i, j, k, ret, got_output; |
|
109 |
+ int buffer_size; |
|
110 |
+ FILE *f; |
|
111 |
+ uint16_t *samples; |
|
112 |
+ float t, tincr; |
|
113 |
+ |
|
114 |
+ printf("Encode audio file %s\n", filename); |
|
115 |
+ |
|
116 |
+ /* find the MP2 encoder */ |
|
117 |
+ codec = avcodec_find_encoder(AV_CODEC_ID_MP2); |
|
118 |
+ if (!codec) { |
|
119 |
+ fprintf(stderr, "Codec not found\n"); |
|
120 |
+ exit(1); |
|
121 |
+ } |
|
122 |
+ |
|
123 |
+ c = avcodec_alloc_context3(codec); |
|
124 |
+ if (!c) { |
|
125 |
+ fprintf(stderr, "Could not allocate audio codec context\n"); |
|
126 |
+ exit(1); |
|
127 |
+ } |
|
128 |
+ |
|
129 |
+ /* put sample parameters */ |
|
130 |
+ c->bit_rate = 64000; |
|
131 |
+ |
|
132 |
+ /* check that the encoder supports s16 pcm input */ |
|
133 |
+ c->sample_fmt = AV_SAMPLE_FMT_S16; |
|
134 |
+ if (!check_sample_fmt(codec, c->sample_fmt)) { |
|
135 |
+ fprintf(stderr, "Encoder does not support sample format %s", |
|
136 |
+ av_get_sample_fmt_name(c->sample_fmt)); |
|
137 |
+ exit(1); |
|
138 |
+ } |
|
139 |
+ |
|
140 |
+ /* select other audio parameters supported by the encoder */ |
|
141 |
+ c->sample_rate = select_sample_rate(codec); |
|
142 |
+ c->channel_layout = select_channel_layout(codec); |
|
143 |
+ c->channels = av_get_channel_layout_nb_channels(c->channel_layout); |
|
144 |
+ |
|
145 |
+ /* open it */ |
|
146 |
+ if (avcodec_open2(c, codec, NULL) < 0) { |
|
147 |
+ fprintf(stderr, "Could not open codec\n"); |
|
148 |
+ exit(1); |
|
149 |
+ } |
|
150 |
+ |
|
151 |
+ f = fopen(filename, "wb"); |
|
152 |
+ if (!f) { |
|
153 |
+ fprintf(stderr, "Could not open %s\n", filename); |
|
154 |
+ exit(1); |
|
155 |
+ } |
|
156 |
+ |
|
157 |
+ /* frame containing input raw audio */ |
|
158 |
+ frame = av_frame_alloc(); |
|
159 |
+ if (!frame) { |
|
160 |
+ fprintf(stderr, "Could not allocate audio frame\n"); |
|
161 |
+ exit(1); |
|
162 |
+ } |
|
163 |
+ |
|
164 |
+ frame->nb_samples = c->frame_size; |
|
165 |
+ frame->format = c->sample_fmt; |
|
166 |
+ frame->channel_layout = c->channel_layout; |
|
167 |
+ |
|
168 |
+ /* the codec gives us the frame size, in samples, |
|
169 |
+ * we calculate the size of the samples buffer in bytes */ |
|
170 |
+ buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size, |
|
171 |
+ c->sample_fmt, 0); |
|
172 |
+ if (buffer_size < 0) { |
|
173 |
+ fprintf(stderr, "Could not get sample buffer size\n"); |
|
174 |
+ exit(1); |
|
175 |
+ } |
|
176 |
+ samples = av_malloc(buffer_size); |
|
177 |
+ if (!samples) { |
|
178 |
+ fprintf(stderr, "Could not allocate %d bytes for samples buffer\n", |
|
179 |
+ buffer_size); |
|
180 |
+ exit(1); |
|
181 |
+ } |
|
182 |
+ /* setup the data pointers in the AVFrame */ |
|
183 |
+ ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt, |
|
184 |
+ (const uint8_t*)samples, buffer_size, 0); |
|
185 |
+ if (ret < 0) { |
|
186 |
+ fprintf(stderr, "Could not setup audio frame\n"); |
|
187 |
+ exit(1); |
|
188 |
+ } |
|
189 |
+ |
|
190 |
+ /* encode a single tone sound */ |
|
191 |
+ t = 0; |
|
192 |
+ tincr = 2 * M_PI * 440.0 / c->sample_rate; |
|
193 |
+ for (i = 0; i < 200; i++) { |
|
194 |
+ av_init_packet(&pkt); |
|
195 |
+ pkt.data = NULL; // packet data will be allocated by the encoder |
|
196 |
+ pkt.size = 0; |
|
197 |
+ |
|
198 |
+ for (j = 0; j < c->frame_size; j++) { |
|
199 |
+ samples[2*j] = (int)(sin(t) * 10000); |
|
200 |
+ |
|
201 |
+ for (k = 1; k < c->channels; k++) |
|
202 |
+ samples[2*j + k] = samples[2*j]; |
|
203 |
+ t += tincr; |
|
204 |
+ } |
|
205 |
+ /* encode the samples */ |
|
206 |
+ ret = avcodec_encode_audio2(c, &pkt, frame, &got_output); |
|
207 |
+ if (ret < 0) { |
|
208 |
+ fprintf(stderr, "Error encoding audio frame\n"); |
|
209 |
+ exit(1); |
|
210 |
+ } |
|
211 |
+ if (got_output) { |
|
212 |
+ fwrite(pkt.data, 1, pkt.size, f); |
|
213 |
+ av_free_packet(&pkt); |
|
214 |
+ } |
|
215 |
+ } |
|
216 |
+ |
|
217 |
+ /* get the delayed frames */ |
|
218 |
+ for (got_output = 1; got_output; i++) { |
|
219 |
+ ret = avcodec_encode_audio2(c, &pkt, NULL, &got_output); |
|
220 |
+ if (ret < 0) { |
|
221 |
+ fprintf(stderr, "Error encoding frame\n"); |
|
222 |
+ exit(1); |
|
223 |
+ } |
|
224 |
+ |
|
225 |
+ if (got_output) { |
|
226 |
+ fwrite(pkt.data, 1, pkt.size, f); |
|
227 |
+ av_free_packet(&pkt); |
|
228 |
+ } |
|
229 |
+ } |
|
230 |
+ fclose(f); |
|
231 |
+ |
|
232 |
+ av_freep(&samples); |
|
233 |
+ av_frame_free(&frame); |
|
234 |
+ avcodec_close(c); |
|
235 |
+ av_free(c); |
|
236 |
+} |
|
237 |
+ |
|
238 |
+/* |
|
239 |
+ * Audio decoding. |
|
240 |
+ */ |
|
241 |
+static void audio_decode_example(const char *outfilename, const char *filename) |
|
242 |
+{ |
|
243 |
+ AVCodec *codec; |
|
244 |
+ AVCodecContext *c= NULL; |
|
245 |
+ int len; |
|
246 |
+ FILE *f, *outfile; |
|
247 |
+ uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
|
248 |
+ AVPacket avpkt; |
|
249 |
+ AVFrame *decoded_frame = NULL; |
|
250 |
+ |
|
251 |
+ av_init_packet(&avpkt); |
|
252 |
+ |
|
253 |
+ printf("Decode audio file %s to %s\n", filename, outfilename); |
|
254 |
+ |
|
255 |
+ /* find the mpeg audio decoder */ |
|
256 |
+ codec = avcodec_find_decoder(AV_CODEC_ID_MP2); |
|
257 |
+ if (!codec) { |
|
258 |
+ fprintf(stderr, "Codec not found\n"); |
|
259 |
+ exit(1); |
|
260 |
+ } |
|
261 |
+ |
|
262 |
+ c = avcodec_alloc_context3(codec); |
|
263 |
+ if (!c) { |
|
264 |
+ fprintf(stderr, "Could not allocate audio codec context\n"); |
|
265 |
+ exit(1); |
|
266 |
+ } |
|
267 |
+ |
|
268 |
+ /* open it */ |
|
269 |
+ if (avcodec_open2(c, codec, NULL) < 0) { |
|
270 |
+ fprintf(stderr, "Could not open codec\n"); |
|
271 |
+ exit(1); |
|
272 |
+ } |
|
273 |
+ |
|
274 |
+ f = fopen(filename, "rb"); |
|
275 |
+ if (!f) { |
|
276 |
+ fprintf(stderr, "Could not open %s\n", filename); |
|
277 |
+ exit(1); |
|
278 |
+ } |
|
279 |
+ outfile = fopen(outfilename, "wb"); |
|
280 |
+ if (!outfile) { |
|
281 |
+ av_free(c); |
|
282 |
+ exit(1); |
|
283 |
+ } |
|
284 |
+ |
|
285 |
+ /* decode until eof */ |
|
286 |
+ avpkt.data = inbuf; |
|
287 |
+ avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); |
|
288 |
+ |
|
289 |
+ while (avpkt.size > 0) { |
|
290 |
+ int got_frame = 0; |
|
291 |
+ |
|
292 |
+ if (!decoded_frame) { |
|
293 |
+ if (!(decoded_frame = av_frame_alloc())) { |
|
294 |
+ fprintf(stderr, "Could not allocate audio frame\n"); |
|
295 |
+ exit(1); |
|
296 |
+ } |
|
297 |
+ } |
|
298 |
+ |
|
299 |
+ len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt); |
|
300 |
+ if (len < 0) { |
|
301 |
+ fprintf(stderr, "Error while decoding\n"); |
|
302 |
+ exit(1); |
|
303 |
+ } |
|
304 |
+ if (got_frame) { |
|
305 |
+ /* if a frame has been decoded, output it */ |
|
306 |
+ int data_size = av_samples_get_buffer_size(NULL, c->channels, |
|
307 |
+ decoded_frame->nb_samples, |
|
308 |
+ c->sample_fmt, 1); |
|
309 |
+ if (data_size < 0) { |
|
310 |
+ /* This should not occur, checking just for paranoia */ |
|
311 |
+ fprintf(stderr, "Failed to calculate data size\n"); |
|
312 |
+ exit(1); |
|
313 |
+ } |
|
314 |
+ fwrite(decoded_frame->data[0], 1, data_size, outfile); |
|
315 |
+ } |
|
316 |
+ avpkt.size -= len; |
|
317 |
+ avpkt.data += len; |
|
318 |
+ avpkt.dts = |
|
319 |
+ avpkt.pts = AV_NOPTS_VALUE; |
|
320 |
+ if (avpkt.size < AUDIO_REFILL_THRESH) { |
|
321 |
+ /* Refill the input buffer, to avoid trying to decode |
|
322 |
+ * incomplete frames. Instead of this, one could also use |
|
323 |
+ * a parser, or use a proper container format through |
|
324 |
+ * libavformat. */ |
|
325 |
+ memmove(inbuf, avpkt.data, avpkt.size); |
|
326 |
+ avpkt.data = inbuf; |
|
327 |
+ len = fread(avpkt.data + avpkt.size, 1, |
|
328 |
+ AUDIO_INBUF_SIZE - avpkt.size, f); |
|
329 |
+ if (len > 0) |
|
330 |
+ avpkt.size += len; |
|
331 |
+ } |
|
332 |
+ } |
|
333 |
+ |
|
334 |
+ fclose(outfile); |
|
335 |
+ fclose(f); |
|
336 |
+ |
|
337 |
+ avcodec_close(c); |
|
338 |
+ av_free(c); |
|
339 |
+ av_frame_free(&decoded_frame); |
|
340 |
+} |
|
341 |
+ |
|
342 |
+/* |
|
343 |
+ * Video encoding example |
|
344 |
+ */ |
|
345 |
+static void video_encode_example(const char *filename, int codec_id) |
|
346 |
+{ |
|
347 |
+ AVCodec *codec; |
|
348 |
+ AVCodecContext *c= NULL; |
|
349 |
+ int i, ret, x, y, got_output; |
|
350 |
+ FILE *f; |
|
351 |
+ AVFrame *frame; |
|
352 |
+ AVPacket pkt; |
|
353 |
+ uint8_t endcode[] = { 0, 0, 1, 0xb7 }; |
|
354 |
+ |
|
355 |
+ printf("Encode video file %s\n", filename); |
|
356 |
+ |
|
357 |
+ /* find the mpeg1 video encoder */ |
|
358 |
+ codec = avcodec_find_encoder(codec_id); |
|
359 |
+ if (!codec) { |
|
360 |
+ fprintf(stderr, "Codec not found\n"); |
|
361 |
+ exit(1); |
|
362 |
+ } |
|
363 |
+ |
|
364 |
+ c = avcodec_alloc_context3(codec); |
|
365 |
+ if (!c) { |
|
366 |
+ fprintf(stderr, "Could not allocate video codec context\n"); |
|
367 |
+ exit(1); |
|
368 |
+ } |
|
369 |
+ |
|
370 |
+ /* put sample parameters */ |
|
371 |
+ c->bit_rate = 400000; |
|
372 |
+ /* resolution must be a multiple of two */ |
|
373 |
+ c->width = 352; |
|
374 |
+ c->height = 288; |
|
375 |
+ /* frames per second */ |
|
376 |
+ c->time_base = (AVRational){1,25}; |
|
377 |
+ /* emit one intra frame every ten frames |
|
378 |
+ * check frame pict_type before passing frame |
|
379 |
+ * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I |
|
380 |
+ * then gop_size is ignored and the output of encoder |
|
381 |
+ * will always be I frame irrespective to gop_size |
|
382 |
+ */ |
|
383 |
+ c->gop_size = 10; |
|
384 |
+ c->max_b_frames = 1; |
|
385 |
+ c->pix_fmt = AV_PIX_FMT_YUV420P; |
|
386 |
+ |
|
387 |
+ if (codec_id == AV_CODEC_ID_H264) |
|
388 |
+ av_opt_set(c->priv_data, "preset", "slow", 0); |
|
389 |
+ |
|
390 |
+ /* open it */ |
|
391 |
+ if (avcodec_open2(c, codec, NULL) < 0) { |
|
392 |
+ fprintf(stderr, "Could not open codec\n"); |
|
393 |
+ exit(1); |
|
394 |
+ } |
|
395 |
+ |
|
396 |
+ f = fopen(filename, "wb"); |
|
397 |
+ if (!f) { |
|
398 |
+ fprintf(stderr, "Could not open %s\n", filename); |
|
399 |
+ exit(1); |
|
400 |
+ } |
|
401 |
+ |
|
402 |
+ frame = av_frame_alloc(); |
|
403 |
+ if (!frame) { |
|
404 |
+ fprintf(stderr, "Could not allocate video frame\n"); |
|
405 |
+ exit(1); |
|
406 |
+ } |
|
407 |
+ frame->format = c->pix_fmt; |
|
408 |
+ frame->width = c->width; |
|
409 |
+ frame->height = c->height; |
|
410 |
+ |
|
411 |
+ /* the image can be allocated by any means and av_image_alloc() is |
|
412 |
+ * just the most convenient way if av_malloc() is to be used */ |
|
413 |
+ ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, |
|
414 |
+ c->pix_fmt, 32); |
|
415 |
+ if (ret < 0) { |
|
416 |
+ fprintf(stderr, "Could not allocate raw picture buffer\n"); |
|
417 |
+ exit(1); |
|
418 |
+ } |
|
419 |
+ |
|
420 |
+ /* encode 1 second of video */ |
|
421 |
+ for (i = 0; i < 25; i++) { |
|
422 |
+ av_init_packet(&pkt); |
|
423 |
+ pkt.data = NULL; // packet data will be allocated by the encoder |
|
424 |
+ pkt.size = 0; |
|
425 |
+ |
|
426 |
+ fflush(stdout); |
|
427 |
+ /* prepare a dummy image */ |
|
428 |
+ /* Y */ |
|
429 |
+ for (y = 0; y < c->height; y++) { |
|
430 |
+ for (x = 0; x < c->width; x++) { |
|
431 |
+ frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3; |
|
432 |
+ } |
|
433 |
+ } |
|
434 |
+ |
|
435 |
+ /* Cb and Cr */ |
|
436 |
+ for (y = 0; y < c->height/2; y++) { |
|
437 |
+ for (x = 0; x < c->width/2; x++) { |
|
438 |
+ frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2; |
|
439 |
+ frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5; |
|
440 |
+ } |
|
441 |
+ } |
|
442 |
+ |
|
443 |
+ frame->pts = i; |
|
444 |
+ |
|
445 |
+ /* encode the image */ |
|
446 |
+ ret = avcodec_encode_video2(c, &pkt, frame, &got_output); |
|
447 |
+ if (ret < 0) { |
|
448 |
+ fprintf(stderr, "Error encoding frame\n"); |
|
449 |
+ exit(1); |
|
450 |
+ } |
|
451 |
+ |
|
452 |
+ if (got_output) { |
|
453 |
+ printf("Write frame %3d (size=%5d)\n", i, pkt.size); |
|
454 |
+ fwrite(pkt.data, 1, pkt.size, f); |
|
455 |
+ av_free_packet(&pkt); |
|
456 |
+ } |
|
457 |
+ } |
|
458 |
+ |
|
459 |
+ /* get the delayed frames */ |
|
460 |
+ for (got_output = 1; got_output; i++) { |
|
461 |
+ fflush(stdout); |
|
462 |
+ |
|
463 |
+ ret = avcodec_encode_video2(c, &pkt, NULL, &got_output); |
|
464 |
+ if (ret < 0) { |
|
465 |
+ fprintf(stderr, "Error encoding frame\n"); |
|
466 |
+ exit(1); |
|
467 |
+ } |
|
468 |
+ |
|
469 |
+ if (got_output) { |
|
470 |
+ printf("Write frame %3d (size=%5d)\n", i, pkt.size); |
|
471 |
+ fwrite(pkt.data, 1, pkt.size, f); |
|
472 |
+ av_free_packet(&pkt); |
|
473 |
+ } |
|
474 |
+ } |
|
475 |
+ |
|
476 |
+ /* add sequence end code to have a real mpeg file */ |
|
477 |
+ fwrite(endcode, 1, sizeof(endcode), f); |
|
478 |
+ fclose(f); |
|
479 |
+ |
|
480 |
+ avcodec_close(c); |
|
481 |
+ av_free(c); |
|
482 |
+ av_freep(&frame->data[0]); |
|
483 |
+ av_frame_free(&frame); |
|
484 |
+ printf("\n"); |
|
485 |
+} |
|
486 |
+ |
|
487 |
+/* |
|
488 |
+ * Video decoding example |
|
489 |
+ */ |
|
490 |
+ |
|
491 |
+static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, |
|
492 |
+ char *filename) |
|
493 |
+{ |
|
494 |
+ FILE *f; |
|
495 |
+ int i; |
|
496 |
+ |
|
497 |
+ f = fopen(filename,"w"); |
|
498 |
+ fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255); |
|
499 |
+ for (i = 0; i < ysize; i++) |
|
500 |
+ fwrite(buf + i * wrap, 1, xsize, f); |
|
501 |
+ fclose(f); |
|
502 |
+} |
|
503 |
+ |
|
504 |
+static int decode_write_frame(const char *outfilename, AVCodecContext *avctx, |
|
505 |
+ AVFrame *frame, int *frame_count, AVPacket *pkt, int last) |
|
506 |
+{ |
|
507 |
+ int len, got_frame; |
|
508 |
+ char buf[1024]; |
|
509 |
+ |
|
510 |
+ len = avcodec_decode_video2(avctx, frame, &got_frame, pkt); |
|
511 |
+ if (len < 0) { |
|
512 |
+ fprintf(stderr, "Error while decoding frame %d\n", *frame_count); |
|
513 |
+ return len; |
|
514 |
+ } |
|
515 |
+ if (got_frame) { |
|
516 |
+ printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count); |
|
517 |
+ fflush(stdout); |
|
518 |
+ |
|
519 |
+ /* the picture is allocated by the decoder, no need to free it */ |
|
520 |
+ snprintf(buf, sizeof(buf), outfilename, *frame_count); |
|
521 |
+ pgm_save(frame->data[0], frame->linesize[0], |
|
522 |
+ avctx->width, avctx->height, buf); |
|
523 |
+ (*frame_count)++; |
|
524 |
+ } |
|
525 |
+ if (pkt->data) { |
|
526 |
+ pkt->size -= len; |
|
527 |
+ pkt->data += len; |
|
528 |
+ } |
|
529 |
+ return 0; |
|
530 |
+} |
|
531 |
+ |
|
532 |
+static void video_decode_example(const char *outfilename, const char *filename) |
|
533 |
+{ |
|
534 |
+ AVCodec *codec; |
|
535 |
+ AVCodecContext *c= NULL; |
|
536 |
+ int frame_count; |
|
537 |
+ FILE *f; |
|
538 |
+ AVFrame *frame; |
|
539 |
+ uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
|
540 |
+ AVPacket avpkt; |
|
541 |
+ |
|
542 |
+ av_init_packet(&avpkt); |
|
543 |
+ |
|
544 |
+ /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ |
|
545 |
+ memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
|
546 |
+ |
|
547 |
+ printf("Decode video file %s to %s\n", filename, outfilename); |
|
548 |
+ |
|
549 |
+ /* find the mpeg1 video decoder */ |
|
550 |
+ codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO); |
|
551 |
+ if (!codec) { |
|
552 |
+ fprintf(stderr, "Codec not found\n"); |
|
553 |
+ exit(1); |
|
554 |
+ } |
|
555 |
+ |
|
556 |
+ c = avcodec_alloc_context3(codec); |
|
557 |
+ if (!c) { |
|
558 |
+ fprintf(stderr, "Could not allocate video codec context\n"); |
|
559 |
+ exit(1); |
|
560 |
+ } |
|
561 |
+ |
|
562 |
+ if(codec->capabilities&CODEC_CAP_TRUNCATED) |
|
563 |
+ c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */ |
|
564 |
+ |
|
565 |
+ /* For some codecs, such as msmpeg4 and mpeg4, width and height |
|
566 |
+ MUST be initialized there because this information is not |
|
567 |
+ available in the bitstream. */ |
|
568 |
+ |
|
569 |
+ /* open it */ |
|
570 |
+ if (avcodec_open2(c, codec, NULL) < 0) { |
|
571 |
+ fprintf(stderr, "Could not open codec\n"); |
|
572 |
+ exit(1); |
|
573 |
+ } |
|
574 |
+ |
|
575 |
+ f = fopen(filename, "rb"); |
|
576 |
+ if (!f) { |
|
577 |
+ fprintf(stderr, "Could not open %s\n", filename); |
|
578 |
+ exit(1); |
|
579 |
+ } |
|
580 |
+ |
|
581 |
+ frame = av_frame_alloc(); |
|
582 |
+ if (!frame) { |
|
583 |
+ fprintf(stderr, "Could not allocate video frame\n"); |
|
584 |
+ exit(1); |
|
585 |
+ } |
|
586 |
+ |
|
587 |
+ frame_count = 0; |
|
588 |
+ for (;;) { |
|
589 |
+ avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); |
|
590 |
+ if (avpkt.size == 0) |
|
591 |
+ break; |
|
592 |
+ |
|
593 |
+ /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) |
|
594 |
+ and this is the only method to use them because you cannot |
|
595 |
+ know the compressed data size before analysing it. |
|
596 |
+ |
|
597 |
+ BUT some other codecs (msmpeg4, mpeg4) are inherently frame |
|
598 |
+ based, so you must call them with all the data for one |
|
599 |
+ frame exactly. You must also initialize 'width' and |
|
600 |
+ 'height' before initializing them. */ |
|
601 |
+ |
|
602 |
+ /* NOTE2: some codecs allow the raw parameters (frame size, |
|
603 |
+ sample rate) to be changed at any frame. We handle this, so |
|
604 |
+ you should also take care of it */ |
|
605 |
+ |
|
606 |
+ /* here, we use a stream based decoder (mpeg1video), so we |
|
607 |
+ feed decoder and see if it could decode a frame */ |
|
608 |
+ avpkt.data = inbuf; |
|
609 |
+ while (avpkt.size > 0) |
|
610 |
+ if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0) |
|
611 |
+ exit(1); |
|
612 |
+ } |
|
613 |
+ |
|
614 |
+ /* some codecs, such as MPEG, transmit the I and P frame with a |
|
615 |
+ latency of one frame. You must do the following to have a |
|
616 |
+ chance to get the last frame of the video */ |
|
617 |
+ avpkt.data = NULL; |
|
618 |
+ avpkt.size = 0; |
|
619 |
+ decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1); |
|
620 |
+ |
|
621 |
+ fclose(f); |
|
622 |
+ |
|
623 |
+ avcodec_close(c); |
|
624 |
+ av_free(c); |
|
625 |
+ av_frame_free(&frame); |
|
626 |
+ printf("\n"); |
|
627 |
+} |
|
628 |
+ |
|
629 |
+int main(int argc, char **argv) |
|
630 |
+{ |
|
631 |
+ const char *output_type; |
|
632 |
+ |
|
633 |
+ /* register all the codecs */ |
|
634 |
+ avcodec_register_all(); |
|
635 |
+ |
|
636 |
+ if (argc < 2) { |
|
637 |
+ printf("usage: %s output_type\n" |
|
638 |
+ "API example program to decode/encode a media stream with libavcodec.\n" |
|
639 |
+ "This program generates a synthetic stream and encodes it to a file\n" |
|
640 |
+ "named test.h264, test.mp2 or test.mpg depending on output_type.\n" |
|
641 |
+ "The encoded stream is then decoded and written to a raw data output.\n" |
|
642 |
+ "output_type must be choosen between 'h264', 'mp2', 'mpg'.\n", |
|
643 |
+ argv[0]); |
|
644 |
+ return 1; |
|
645 |
+ } |
|
646 |
+ output_type = argv[1]; |
|
647 |
+ |
|
648 |
+ if (!strcmp(output_type, "h264")) { |
|
649 |
+ video_encode_example("test.h264", AV_CODEC_ID_H264); |
|
650 |
+ } else if (!strcmp(output_type, "mp2")) { |
|
651 |
+ audio_encode_example("test.mp2"); |
|
652 |
+ audio_decode_example("test.sw", "test.mp2"); |
|
653 |
+ } else if (!strcmp(output_type, "mpg")) { |
|
654 |
+ video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO); |
|
655 |
+ video_decode_example("test%02d.pgm", "test.mpg"); |
|
656 |
+ } else { |
|
657 |
+ fprintf(stderr, "Invalid output type '%s', choose between 'h264', 'mp2', or 'mpg'\n", |
|
658 |
+ output_type); |
|
659 |
+ return 1; |
|
660 |
+ } |
|
661 |
+ |
|
662 |
+ return 0; |
|
663 |
+} |