annotate api-example.c @ 11498:8e889ce0d616 libavcodec

100L, revert r22560, already present
author bcoudurier
date Mon, 15 Mar 2010 23:40:51 +0000
parents b790df0d7046
children 7dd2a45249a9
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
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
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;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
118 int out_size, len;
9269
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;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
121 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
122 AVPacket avpkt;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
123
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
124 av_init_packet(&avpkt);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
125
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
126 printf("Audio decoding\n");
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 /* find the mpeg audio decoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
129 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
130 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
131 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
132 exit(1);
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
135 c= avcodec_alloc_context();
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 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
138 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
139 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
140 exit(1);
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
143 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
144
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
145 f = fopen(filename, "rb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
146 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
147 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
148 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
149 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
150 outfile = fopen(outfilename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
151 if (!outfile) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
152 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
153 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
154 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
155
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
156 /* decode until eof */
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
157 avpkt.data = inbuf;
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
158 for(;;) {
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
159 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
160 if (avpkt.size == 0)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
161 break;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
162
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
163 avpkt.data = inbuf;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
164 while (avpkt.size > 0) {
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
165 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
166 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
167 if (len < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
168 fprintf(stderr, "Error while decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
169 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
170 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
171 if (out_size > 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
172 /* 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
173 fwrite(outbuf, 1, out_size, outfile);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
174 }
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
175 avpkt.size -= len;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
176 avpkt.data += len;
9269
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 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
179
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
180 fclose(outfile);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
181 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
182 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
183
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
184 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
185 av_free(c);
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
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 * Video encoding example
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
190 */
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
191 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
192 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
193 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
194 AVCodecContext *c= NULL;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
195 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
196 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
197 AVFrame *picture;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
198 uint8_t *outbuf, *picture_buf;
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 printf("Video encoding\n");
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 /* find the mpeg1 video encoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
203 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
204 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
205 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
206 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
207 }
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 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
210 picture= avcodec_alloc_frame();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
211
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
212 /* put sample parameters */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
213 c->bit_rate = 400000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
214 /* 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
215 c->width = 352;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
216 c->height = 288;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
217 /* frames per second */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
218 c->time_base= (AVRational){1,25};
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
219 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
220 c->max_b_frames=1;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
221 c->pix_fmt = PIX_FMT_YUV420P;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
222
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
223 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
224 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
225 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
226 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
227 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
228
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
229 f = fopen(filename, "wb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
230 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
231 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
232 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
233 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
234
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
235 /* alloc image and output buffer */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
236 outbuf_size = 100000;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
237 outbuf = malloc(outbuf_size);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
238 size = c->width * c->height;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
239 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
240
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
241 picture->data[0] = picture_buf;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
242 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
243 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
244 picture->linesize[0] = c->width;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
245 picture->linesize[1] = c->width / 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
246 picture->linesize[2] = c->width / 2;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
247
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
248 /* encode 1 second of video */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
249 for(i=0;i<25;i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
250 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
251 /* prepare a dummy image */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
252 /* Y */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
253 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
254 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
255 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
256 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
257 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
258
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
259 /* Cb and Cr */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
260 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
261 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
262 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
263 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
264 }
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 /* encode the image */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
268 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
269 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
270 fwrite(outbuf, 1, out_size, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
271 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
272
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
273 /* get the delayed frames */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
274 for(; out_size; i++) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
275 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
276
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, NULL);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
278 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
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 /* 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
283 outbuf[0] = 0x00;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
284 outbuf[1] = 0x00;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
285 outbuf[2] = 0x01;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
286 outbuf[3] = 0xb7;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
287 fwrite(outbuf, 1, 4, f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
288 fclose(f);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
289 free(picture_buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
290 free(outbuf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
291
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
292 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
293 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
294 av_free(picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
295 printf("\n");
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
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 * Video decoding example
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
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
302 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
303 char *filename)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
304 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
305 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
306 int i;
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 f=fopen(filename,"w");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
309 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
310 for(i=0;i<ysize;i++)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
311 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
312 fclose(f);
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
9295
b225f51903af Mark non-exported functions in test and example programs as static.
diego
parents: 9269
diff changeset
315 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
316 {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
317 AVCodec *codec;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
318 AVCodecContext *c= NULL;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
319 int frame, got_picture, len;
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
320 FILE *f;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
321 AVFrame *picture;
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
322 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
323 char buf[1024];
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
324 AVPacket avpkt;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
325
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
326 av_init_packet(&avpkt);
9269
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 /* 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
329 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
330
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
331 printf("Video decoding\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
332
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
333 /* find the mpeg1 video decoder */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
334 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
335 if (!codec) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
336 fprintf(stderr, "codec not found\n");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
337 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
338 }
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 c= avcodec_alloc_context();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
341 picture= avcodec_alloc_frame();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
342
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
343 if(codec->capabilities&CODEC_CAP_TRUNCATED)
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
344 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
345
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
346 /* 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
347 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
348 available in the bitstream. */
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 /* open it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
351 if (avcodec_open(c, codec) < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
352 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
353 exit(1);
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
356 /* 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
357
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
358 f = fopen(filename, "rb");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
359 if (!f) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
360 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
361 exit(1);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
362 }
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 frame = 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
365 for(;;) {
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
366 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
367 if (avpkt.size == 0)
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
368 break;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
369
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
370 /* 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
371 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
372 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
373
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
374 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
375 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
376 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
377 'height' before initializing them. */
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 /* 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
380 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
381 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
382
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
383 /* 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
384 feed decoder and see if it could decode a frame */
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
385 avpkt.data = inbuf;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
386 while (avpkt.size > 0) {
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
387 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
388 if (len < 0) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
389 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
390 exit(1);
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 if (got_picture) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
393 printf("saving frame %3d\n", frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
394 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
395
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
396 /* 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
397 free it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
398 snprintf(buf, sizeof(buf), outfilename, frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
399 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
400 c->width, c->height, buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
401 frame++;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
402 }
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
403 avpkt.size -= len;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
404 avpkt.data += len;
9269
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
405 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
406 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
407
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
408 /* 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
409 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
410 chance to get the last frame of the video */
9382
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
411 avpkt.data = NULL;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
412 avpkt.size = 0;
621852d53087 Use the new avcodec_decode_* API.
stefano
parents: 9295
diff changeset
413 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
414 if (got_picture) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
415 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
416 fflush(stdout);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
417
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
418 /* 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
419 free it */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
420 snprintf(buf, sizeof(buf), outfilename, frame);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
421 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
422 c->width, c->height, buf);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
423 frame++;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
424 }
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
425
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
426 fclose(f);
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 avcodec_close(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
429 av_free(c);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
430 av_free(picture);
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
431 printf("\n");
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
434 int main(int argc, char **argv)
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 const char *filename;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
437
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
438 /* 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
439 avcodec_init();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
440
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
441 /* register all the codecs */
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
442 avcodec_register_all();
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
443
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
444 if (argc <= 1) {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
445 audio_encode_example("/tmp/test.mp2");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
446 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
447
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
448 video_encode_example("/tmp/test.mpg");
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
449 filename = "/tmp/test.mpg";
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
450 } else {
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
451 filename = argv[1];
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
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
454 // 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
455 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
456
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
457 return 0;
bd89b50b48fc Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff changeset
458 }