annotate api-example.c @ 12071:126392340f69 libavcodec

Remove angular brackets from Doxygen comments; Doxygen confuses them for HTML.
author diego
date Fri, 02 Jul 2010 12:48:32 +0000
parents 01408f7d629c
children
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 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 10367
diff changeset
22 * @file
9269
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
10367
b790df0d7046 Allow building api-example from outside of libavcodec and also
jai_menon
parents: 9382
diff changeset
38 #include "libavcodec/avcodec.h"
9269
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
11765
c96e649c4e0a api-example: Try to avoid decoding incomplete frames
mstorsjo
parents: 11644
diff changeset
42 #define AUDIO_INBUF_SIZE 20480
c96e649c4e0a api-example: Try to avoid decoding incomplete frames
mstorsjo
parents: 11644
diff changeset
43 #define AUDIO_REFILL_THRESH 4096
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
44
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
45 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
46 * Audio encoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
47 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
48 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
49 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
50 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
51 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
52 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
53 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
54 short *samples;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
55 float t, tincr;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
56 uint8_t *outbuf;
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 printf("Audio encoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
59
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
60 /* find the MP2 encoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
61 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
62 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
63 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
64 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
65 }
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 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
68
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
69 /* put sample parameters */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
70 c->bit_rate = 64000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
71 c->sample_rate = 44100;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
72 c->channels = 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
73
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
74 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
75 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
76 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
77 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
78 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
79
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
80 /* 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
81 frame_size = c->frame_size;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
82 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
83 outbuf_size = 10000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
84 outbuf = malloc(outbuf_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
85
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
86 f = fopen(filename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
87 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
88 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
89 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
90 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
91
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
92 /* encode a single tone sound */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
93 t = 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
94 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
95 for(i=0;i<200;i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
96 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
97 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
98 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
99 t += tincr;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
100 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
101 /* encode the samples */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
102 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
103 fwrite(outbuf, 1, out_size, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
104 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
105 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
106 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
107 free(samples);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
108
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
109 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
110 av_free(c);
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
113 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
114 * Audio decoding.
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
115 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
116 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
117 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
118 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
119 AVCodecContext *c= NULL;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
120 int out_size, len;
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
121 FILE *f, *outfile;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
122 uint8_t *outbuf;
11765
c96e649c4e0a api-example: Try to avoid decoding incomplete frames
mstorsjo
parents: 11644
diff changeset
123 uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
124 AVPacket avpkt;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
125
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
126 av_init_packet(&avpkt);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
127
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
128 printf("Audio decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
129
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
130 /* find the mpeg audio decoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
131 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
132 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
133 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
134 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
135 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
136
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
137 c= avcodec_alloc_context();
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 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
140 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
141 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
142 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
143 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
144
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
145 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
146
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
147 f = fopen(filename, "rb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
148 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
149 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
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 outfile = fopen(outfilename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
153 if (!outfile) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
154 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
155 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
156 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
157
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
158 /* decode until eof */
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
159 avpkt.data = inbuf;
11766
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
160 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
161
11766
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
162 while (avpkt.size > 0) {
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
163 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
164 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
165 if (len < 0) {
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
166 fprintf(stderr, "Error while decoding\n");
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
167 exit(1);
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
168 }
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
169 if (out_size > 0) {
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
170 /* if a frame has been decoded, output it */
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
171 fwrite(outbuf, 1, out_size, outfile);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
172 }
11766
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
173 avpkt.size -= len;
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
174 avpkt.data += len;
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
175 if (avpkt.size < AUDIO_REFILL_THRESH) {
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
176 /* Refill the input buffer, to avoid trying to decode
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
177 * incomplete frames. Instead of this, one could also use
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
178 * a parser, or use a proper container format through
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
179 * libavformat. */
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
180 memmove(inbuf, avpkt.data, avpkt.size);
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
181 avpkt.data = inbuf;
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
182 len = fread(avpkt.data + avpkt.size, 1,
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
183 AUDIO_INBUF_SIZE - avpkt.size, f);
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
184 if (len > 0)
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
185 avpkt.size += len;
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
186 }
01408f7d629c Cosmetics: reindent after the previous commit
mstorsjo
parents: 11765
diff changeset
187 }
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
188
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
189 fclose(outfile);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
190 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
191 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
192
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
193 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
194 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
195 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
196
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 * Video encoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
199 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
200 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
201 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
202 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
203 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
204 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
205 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
206 AVFrame *picture;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
207 uint8_t *outbuf, *picture_buf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
208
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
209 printf("Video encoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
210
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
211 /* find the mpeg1 video encoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
212 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
213 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
214 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
215 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
216 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
217
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
218 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
219 picture= avcodec_alloc_frame();
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 /* put sample parameters */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
222 c->bit_rate = 400000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
223 /* 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
224 c->width = 352;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
225 c->height = 288;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
226 /* frames per second */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
227 c->time_base= (AVRational){1,25};
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
228 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
229 c->max_b_frames=1;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
230 c->pix_fmt = PIX_FMT_YUV420P;
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 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
233 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
234 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
235 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
236 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
237
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
238 f = fopen(filename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
239 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
240 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
241 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
242 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
243
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
244 /* alloc image and output buffer */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
245 outbuf_size = 100000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
246 outbuf = malloc(outbuf_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
247 size = c->width * c->height;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
248 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
249
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
250 picture->data[0] = picture_buf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
251 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
252 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
253 picture->linesize[0] = c->width;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
254 picture->linesize[1] = c->width / 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
255 picture->linesize[2] = c->width / 2;
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 /* encode 1 second of video */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
258 for(i=0;i<25;i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
259 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
260 /* prepare a dummy image */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
261 /* Y */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
262 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
263 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
264 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
265 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
266 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
267
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
268 /* Cb and Cr */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
269 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
270 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
271 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
272 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
273 }
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
276 /* encode the image */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
277 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
278 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
279 fwrite(outbuf, 1, out_size, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
280 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
281
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
282 /* get the delayed frames */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
283 for(; out_size; i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
284 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
285
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
286 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
287 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
288 fwrite(outbuf, 1, out_size, f);
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
291 /* 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
292 outbuf[0] = 0x00;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
293 outbuf[1] = 0x00;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
294 outbuf[2] = 0x01;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
295 outbuf[3] = 0xb7;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
296 fwrite(outbuf, 1, 4, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
297 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
298 free(picture_buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
299 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
300
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
301 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
302 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
303 av_free(picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
304 printf("\n");
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
307 /*
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
308 * Video decoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
309 */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
310
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
311 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
312 char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
313 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
314 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
315 int i;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
316
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
317 f=fopen(filename,"w");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
318 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
319 for(i=0;i<ysize;i++)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
320 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
321 fclose(f);
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
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
324 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
325 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
326 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
327 AVCodecContext *c= NULL;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
328 int frame, got_picture, len;
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
329 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
330 AVFrame *picture;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
331 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
332 char buf[1024];
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
333 AVPacket avpkt;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
334
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
335 av_init_packet(&avpkt);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
336
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
337 /* 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
338 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
339
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
340 printf("Video decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
341
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
342 /* find the mpeg1 video decoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
343 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
344 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
345 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
346 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
347 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
348
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
349 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
350 picture= avcodec_alloc_frame();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
351
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
352 if(codec->capabilities&CODEC_CAP_TRUNCATED)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
353 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
354
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
355 /* 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
356 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
357 available in the bitstream. */
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 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
360 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
361 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
362 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
363 }
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 /* 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
366
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
367 f = fopen(filename, "rb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
368 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
369 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
370 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
371 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
372
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
373 frame = 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
374 for(;;) {
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
375 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
376 if (avpkt.size == 0)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
377 break;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
378
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
379 /* 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
380 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
381 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
382
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
383 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
384 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
385 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
386 'height' before initializing them. */
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 /* 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
389 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
390 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
391
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
392 /* 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
393 feed decoder and see if it could decode a frame */
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
394 avpkt.data = inbuf;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
395 while (avpkt.size > 0) {
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
396 len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
397 if (len < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
398 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
399 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
400 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
401 if (got_picture) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
402 printf("saving frame %3d\n", frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
403 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
404
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
405 /* 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
406 free it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
407 snprintf(buf, sizeof(buf), outfilename, frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
408 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
409 c->width, c->height, buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
410 frame++;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
411 }
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
412 avpkt.size -= len;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
413 avpkt.data += len;
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
414 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
415 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
416
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
417 /* 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
418 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
419 chance to get the last frame of the video */
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
420 avpkt.data = NULL;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
421 avpkt.size = 0;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
422 len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
423 if (got_picture) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
424 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
425 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
426
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
427 /* 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
428 free it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
429 snprintf(buf, sizeof(buf), outfilename, frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
430 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
431 c->width, c->height, buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
432 frame++;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
433 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
434
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
435 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
436
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
437 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
438 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
439 av_free(picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
440 printf("\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
441 }
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 int main(int argc, char **argv)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
444 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
445 const char *filename;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
446
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
447 /* 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
448 avcodec_init();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
449
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
450 /* register all the codecs */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
451 avcodec_register_all();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
452
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
453 if (argc <= 1) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
454 audio_encode_example("/tmp/test.mp2");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
455 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
456
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
457 video_encode_example("/tmp/test.mpg");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
458 filename = "/tmp/test.mpg";
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
459 } else {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
460 filename = argv[1];
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
461 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
462
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
463 // 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
464 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
465
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
466 return 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
467 }