annotate api-example.c @ 9379:d31c367da415 libavcodec

Make sure mpeg2 has its height rounded up to 32 as that is needed for interlaced stuff. This might have been exploitable when emu edge was not set though note this bug has been introduced just a few days ago.
author michael
date Fri, 10 Apr 2009 00:09:07 +0000
parents b225f51903af
children 621852d53087
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
1 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
2 * copyright (c) 2001 Fabrice Bellard
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
3 *
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
4 * This file is part of FFmpeg.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
5 *
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
9 * version 2.1 of the License, or (at your option) any later version.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
10 *
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
11 * FFmpeg is distributed in the hope that it will be useful,
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
14 * Lesser General Public License for more details.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
15 *
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
17 * License along with FFmpeg; if not, write to the Free Software
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
19 */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
20
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
21 /**
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
22 * @file libavcodec/apiexample.c
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
23 * avcodec API use example.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
24 *
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
25 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
26 * not file formats (avi, vob, etc...). See library 'libavformat' for the
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
27 * format handling
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
28 */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
29
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
30 #include <stdlib.h>
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
31 #include <stdio.h>
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
32 #include <string.h>
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
33
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
34 #ifdef HAVE_AV_CONFIG_H
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
35 #undef HAVE_AV_CONFIG_H
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
36 #endif
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
37
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
38 #include "avcodec.h"
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
39 #include "libavutil/mathematics.h"
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
40
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
41 #define INBUF_SIZE 4096
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
42
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
43 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
44 * Audio encoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
45 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
46 static void audio_encode_example(const char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
47 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
48 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
49 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
50 int frame_size, i, j, out_size, outbuf_size;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
51 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
52 short *samples;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
53 float t, tincr;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
54 uint8_t *outbuf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
55
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
56 printf("Audio encoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
57
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
58 /* find the MP2 encoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
59 codec = avcodec_find_encoder(CODEC_ID_MP2);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
60 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
61 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
62 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
63 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
64
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
65 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
66
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
67 /* put sample parameters */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
68 c->bit_rate = 64000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
69 c->sample_rate = 44100;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
70 c->channels = 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
71
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
72 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
73 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
74 fprintf(stderr, "could not open codec\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
75 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
76 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
77
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
78 /* the codec gives us the frame size, in samples */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
79 frame_size = c->frame_size;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
80 samples = malloc(frame_size * 2 * c->channels);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
81 outbuf_size = 10000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
82 outbuf = malloc(outbuf_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
83
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
84 f = fopen(filename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
85 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
86 fprintf(stderr, "could not open %s\n", filename);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
87 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
88 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
89
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
90 /* encode a single tone sound */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
91 t = 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
92 tincr = 2 * M_PI * 440.0 / c->sample_rate;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
93 for(i=0;i<200;i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
94 for(j=0;j<frame_size;j++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
95 samples[2*j] = (int)(sin(t) * 10000);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
96 samples[2*j+1] = samples[2*j];
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
97 t += tincr;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
98 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
99 /* encode the samples */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
100 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
101 fwrite(outbuf, 1, out_size, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
102 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
103 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
104 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
105 free(samples);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
106
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
107 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
108 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
109 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
110
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
111 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
112 * Audio decoding.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
113 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
114 static void audio_decode_example(const char *outfilename, const char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
115 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
116 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
117 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
118 int out_size, size, len;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
119 FILE *f, *outfile;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
120 uint8_t *outbuf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
121 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
122
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
123 printf("Audio decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
124
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
125 /* find the mpeg audio decoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
126 codec = avcodec_find_decoder(CODEC_ID_MP2);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
127 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
128 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
129 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
130 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
131
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
132 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
133
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
134 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
135 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
136 fprintf(stderr, "could not open codec\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
137 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
138 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
139
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
140 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
141
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
142 f = fopen(filename, "rb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
143 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
144 fprintf(stderr, "could not open %s\n", filename);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
145 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
146 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
147 outfile = fopen(outfilename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
148 if (!outfile) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
149 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
150 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
151 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
152
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
153 /* decode until eof */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
154 inbuf_ptr = inbuf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
155 for(;;) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
156 size = fread(inbuf, 1, INBUF_SIZE, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
157 if (size == 0)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
158 break;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
159
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
160 inbuf_ptr = inbuf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
161 while (size > 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
162 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
163 len = avcodec_decode_audio2(c, (short *)outbuf, &out_size,
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
164 inbuf_ptr, size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
165 if (len < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
166 fprintf(stderr, "Error while decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
167 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
168 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
169 if (out_size > 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
170 /* if a frame has been decoded, output it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
171 fwrite(outbuf, 1, out_size, outfile);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
172 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
173 size -= len;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
174 inbuf_ptr += len;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
175 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
176 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
177
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
178 fclose(outfile);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
179 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
180 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
181
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
182 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
183 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
184 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
185
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
186 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
187 * Video encoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
188 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
189 static void video_encode_example(const char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
190 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
191 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
192 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
193 int i, out_size, size, x, y, outbuf_size;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
194 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
195 AVFrame *picture;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
196 uint8_t *outbuf, *picture_buf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
197
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
198 printf("Video encoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
199
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
200 /* find the mpeg1 video encoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
201 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
202 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
203 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
204 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
205 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
206
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
207 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
208 picture= avcodec_alloc_frame();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
209
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
210 /* put sample parameters */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
211 c->bit_rate = 400000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
212 /* resolution must be a multiple of two */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
213 c->width = 352;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
214 c->height = 288;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
215 /* frames per second */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
216 c->time_base= (AVRational){1,25};
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
217 c->gop_size = 10; /* emit one intra frame every ten frames */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
218 c->max_b_frames=1;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
219 c->pix_fmt = PIX_FMT_YUV420P;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
220
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
221 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
222 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
223 fprintf(stderr, "could not open codec\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
224 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
225 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
226
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
227 f = fopen(filename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
228 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
229 fprintf(stderr, "could not open %s\n", filename);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
230 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
231 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
232
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
233 /* alloc image and output buffer */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
234 outbuf_size = 100000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
235 outbuf = malloc(outbuf_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
236 size = c->width * c->height;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
237 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
238
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
239 picture->data[0] = picture_buf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
240 picture->data[1] = picture->data[0] + size;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
241 picture->data[2] = picture->data[1] + size / 4;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
242 picture->linesize[0] = c->width;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
243 picture->linesize[1] = c->width / 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
244 picture->linesize[2] = c->width / 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
245
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
246 /* encode 1 second of video */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
247 for(i=0;i<25;i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
248 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
249 /* prepare a dummy image */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
250 /* Y */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
251 for(y=0;y<c->height;y++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
252 for(x=0;x<c->width;x++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
253 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
254 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
255 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
256
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
257 /* Cb and Cr */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
258 for(y=0;y<c->height/2;y++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
259 for(x=0;x<c->width/2;x++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
260 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
261 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
262 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
263 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
264
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
265 /* encode the image */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
266 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
267 printf("encoding frame %3d (size=%5d)\n", i, out_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
268 fwrite(outbuf, 1, out_size, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
269 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
270
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
271 /* get the delayed frames */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
272 for(; out_size; i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
273 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
274
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
275 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
276 printf("write frame %3d (size=%5d)\n", i, out_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
277 fwrite(outbuf, 1, out_size, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
278 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
279
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
280 /* add sequence end code to have a real mpeg file */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
281 outbuf[0] = 0x00;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
282 outbuf[1] = 0x00;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
283 outbuf[2] = 0x01;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
284 outbuf[3] = 0xb7;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
285 fwrite(outbuf, 1, 4, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
286 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
287 free(picture_buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
288 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
289
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
290 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
291 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
292 av_free(picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
293 printf("\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
294 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
295
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
296 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
297 * Video decoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
298 */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
299
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
300 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
301 char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
302 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
303 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
304 int i;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
305
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
306 f=fopen(filename,"w");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
307 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
308 for(i=0;i<ysize;i++)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
309 fwrite(buf + i * wrap,1,xsize,f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
310 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
311 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
312
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
313 static void video_decode_example(const char *outfilename, const char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
314 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
315 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
316 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
317 int frame, size, got_picture, len;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
318 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
319 AVFrame *picture;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
320 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
321 char buf[1024];
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
322
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
323 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
324 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
325
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
326 printf("Video decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
327
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
328 /* find the mpeg1 video decoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
329 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
330 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
331 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
332 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
333 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
334
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
335 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
336 picture= avcodec_alloc_frame();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
337
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
338 if(codec->capabilities&CODEC_CAP_TRUNCATED)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
339 c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
340
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
341 /* For some codecs, such as msmpeg4 and mpeg4, width and height
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
342 MUST be initialized there because this information is not
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
343 available in the bitstream. */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
344
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
345 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
346 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
347 fprintf(stderr, "could not open codec\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
348 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
349 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
350
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
351 /* the codec gives us the frame size, in samples */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
352
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
353 f = fopen(filename, "rb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
354 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
355 fprintf(stderr, "could not open %s\n", filename);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
356 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
357 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
358
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
359 frame = 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
360 for(;;) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
361 size = fread(inbuf, 1, INBUF_SIZE, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
362 if (size == 0)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
363 break;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
364
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
365 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
366 and this is the only method to use them because you cannot
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
367 know the compressed data size before analysing it.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
368
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
369 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
370 based, so you must call them with all the data for one
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
371 frame exactly. You must also initialize 'width' and
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
372 'height' before initializing them. */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
373
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
374 /* NOTE2: some codecs allow the raw parameters (frame size,
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
375 sample rate) to be changed at any frame. We handle this, so
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
376 you should also take care of it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
377
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
378 /* here, we use a stream based decoder (mpeg1video), so we
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
379 feed decoder and see if it could decode a frame */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
380 inbuf_ptr = inbuf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
381 while (size > 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
382 len = avcodec_decode_video(c, picture, &got_picture,
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
383 inbuf_ptr, size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
384 if (len < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
385 fprintf(stderr, "Error while decoding frame %d\n", frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
386 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
387 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
388 if (got_picture) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
389 printf("saving frame %3d\n", frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
390 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
391
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
392 /* the picture is allocated by the decoder. no need to
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
393 free it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
394 snprintf(buf, sizeof(buf), outfilename, frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
395 pgm_save(picture->data[0], picture->linesize[0],
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
396 c->width, c->height, buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
397 frame++;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
398 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
399 size -= len;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
400 inbuf_ptr += len;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
401 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
402 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
403
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
404 /* some codecs, such as MPEG, transmit the I and P frame with a
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
405 latency of one frame. You must do the following to have a
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
406 chance to get the last frame of the video */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
407 len = avcodec_decode_video(c, picture, &got_picture,
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
408 NULL, 0);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
409 if (got_picture) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
410 printf("saving last frame %3d\n", frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
411 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
412
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
413 /* the picture is allocated by the decoder. no need to
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
414 free it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
415 snprintf(buf, sizeof(buf), outfilename, frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
416 pgm_save(picture->data[0], picture->linesize[0],
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
417 c->width, c->height, buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
418 frame++;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
419 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
420
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
421 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
422
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
423 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
424 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
425 av_free(picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
426 printf("\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
427 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
428
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
429 int main(int argc, char **argv)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
430 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
431 const char *filename;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
432
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
433 /* must be called before using avcodec lib */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
434 avcodec_init();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
435
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
436 /* register all the codecs */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
437 avcodec_register_all();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
438
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
439 if (argc <= 1) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
440 audio_encode_example("/tmp/test.mp2");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
441 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
442
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
443 video_encode_example("/tmp/test.mpg");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
444 filename = "/tmp/test.mpg";
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
445 } else {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
446 filename = argv[1];
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
447 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
448
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
449 // audio_decode_example("/tmp/test.sw", filename);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
450 video_decode_example("/tmp/test%d.pgm", filename);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
451
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
452 return 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
453 }