Mercurial > libavcodec.hg
annotate api-example.c @ 9269:bd89b50b48fc libavcodec
Rename apiexample.c --> api-example.c to be consistent with other example files.
author | diego |
---|---|
date | Sun, 29 Mar 2009 14:28:56 +0000 |
parents | |
children | b225f51903af |
rev | line source |
---|---|
9269
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
1 /* |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
2 * copyright (c) 2001 Fabrice Bellard |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
3 * |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
4 * This file is part of FFmpeg. |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
5 * |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
6 * FFmpeg is free software; you can redistribute it and/or |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
7 * modify it under the terms of the GNU Lesser General Public |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
8 * License as published by the Free Software Foundation; either |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
9 * version 2.1 of the License, or (at your option) any later version. |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
10 * |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
11 * FFmpeg is distributed in the hope that it will be useful, |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
14 * Lesser General Public License for more details. |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
15 * |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
16 * You should have received a copy of the GNU Lesser General Public |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
17 * License along with FFmpeg; if not, write to the Free Software |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
19 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
20 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
21 /** |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
22 * @file libavcodec/apiexample.c |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
23 * avcodec API use example. |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
24 * |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
25 * Note that this library only handles codecs (mpeg, mpeg4, etc...), |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
26 * not file formats (avi, vob, etc...). See library 'libavformat' for the |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
27 * format handling |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
28 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
29 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
30 #include <stdlib.h> |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
31 #include <stdio.h> |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
32 #include <string.h> |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
33 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
34 #ifdef HAVE_AV_CONFIG_H |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
35 #undef HAVE_AV_CONFIG_H |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
36 #endif |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
37 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
38 #include "avcodec.h" |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
39 #include "libavutil/mathematics.h" |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
40 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
41 #define INBUF_SIZE 4096 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
42 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
43 /* |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
44 * Audio encoding example |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
45 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
46 void audio_encode_example(const char *filename) |
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 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
114 void audio_decode_example(const char *outfilename, const char *filename) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
115 { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
116 AVCodec *codec; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
117 AVCodecContext *c= NULL; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
118 int out_size, size, len; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
119 FILE *f, *outfile; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
120 uint8_t *outbuf; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
121 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
122 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
123 printf("Audio decoding\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
124 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
125 /* find the mpeg audio decoder */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
126 codec = avcodec_find_decoder(CODEC_ID_MP2); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
127 if (!codec) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
128 fprintf(stderr, "codec not found\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
129 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
130 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
131 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
132 c= avcodec_alloc_context(); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
133 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
134 /* open it */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
135 if (avcodec_open(c, codec) < 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
136 fprintf(stderr, "could not open codec\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
137 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
138 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
139 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
140 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
141 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
142 f = fopen(filename, "rb"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
143 if (!f) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
144 fprintf(stderr, "could not open %s\n", filename); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
145 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
146 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
147 outfile = fopen(outfilename, "wb"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
148 if (!outfile) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
149 av_free(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
150 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
151 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
152 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
153 /* decode until eof */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
154 inbuf_ptr = inbuf; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
155 for(;;) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
156 size = fread(inbuf, 1, INBUF_SIZE, f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
157 if (size == 0) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
158 break; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
159 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
160 inbuf_ptr = inbuf; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
161 while (size > 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
162 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
163 len = avcodec_decode_audio2(c, (short *)outbuf, &out_size, |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
164 inbuf_ptr, size); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
165 if (len < 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
166 fprintf(stderr, "Error while decoding\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
167 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
168 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
169 if (out_size > 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
170 /* if a frame has been decoded, output it */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
171 fwrite(outbuf, 1, out_size, outfile); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
172 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
173 size -= len; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
174 inbuf_ptr += len; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
175 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
176 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
177 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
178 fclose(outfile); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
179 fclose(f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
180 free(outbuf); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
181 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
182 avcodec_close(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
183 av_free(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
184 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
185 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
186 /* |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
187 * Video encoding example |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
188 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
189 void video_encode_example(const char *filename) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
190 { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
191 AVCodec *codec; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
192 AVCodecContext *c= NULL; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
193 int i, out_size, size, x, y, outbuf_size; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
194 FILE *f; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
195 AVFrame *picture; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
196 uint8_t *outbuf, *picture_buf; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
197 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
198 printf("Video encoding\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
199 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
200 /* find the mpeg1 video encoder */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
201 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
202 if (!codec) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
203 fprintf(stderr, "codec not found\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
204 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
205 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
206 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
207 c= avcodec_alloc_context(); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
208 picture= avcodec_alloc_frame(); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
209 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
210 /* put sample parameters */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
211 c->bit_rate = 400000; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
212 /* resolution must be a multiple of two */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
213 c->width = 352; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
214 c->height = 288; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
215 /* frames per second */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
216 c->time_base= (AVRational){1,25}; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
217 c->gop_size = 10; /* emit one intra frame every ten frames */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
218 c->max_b_frames=1; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
219 c->pix_fmt = PIX_FMT_YUV420P; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
220 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
221 /* open it */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
222 if (avcodec_open(c, codec) < 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
223 fprintf(stderr, "could not open codec\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
224 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
225 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
226 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
227 f = fopen(filename, "wb"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
228 if (!f) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
229 fprintf(stderr, "could not open %s\n", filename); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
230 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
231 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
232 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
233 /* alloc image and output buffer */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
234 outbuf_size = 100000; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
235 outbuf = malloc(outbuf_size); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
236 size = c->width * c->height; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
237 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
238 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
239 picture->data[0] = picture_buf; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
240 picture->data[1] = picture->data[0] + size; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
241 picture->data[2] = picture->data[1] + size / 4; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
242 picture->linesize[0] = c->width; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
243 picture->linesize[1] = c->width / 2; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
244 picture->linesize[2] = c->width / 2; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
245 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
246 /* encode 1 second of video */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
247 for(i=0;i<25;i++) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
248 fflush(stdout); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
249 /* prepare a dummy image */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
250 /* Y */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
251 for(y=0;y<c->height;y++) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
252 for(x=0;x<c->width;x++) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
253 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
254 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
255 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
256 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
257 /* Cb and Cr */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
258 for(y=0;y<c->height/2;y++) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
259 for(x=0;x<c->width/2;x++) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
260 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
261 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
262 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
263 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
264 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
265 /* encode the image */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
266 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
267 printf("encoding frame %3d (size=%5d)\n", i, out_size); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
268 fwrite(outbuf, 1, out_size, f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
269 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
270 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
271 /* get the delayed frames */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
272 for(; out_size; i++) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
273 fflush(stdout); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
274 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
275 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
276 printf("write frame %3d (size=%5d)\n", i, out_size); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
277 fwrite(outbuf, 1, out_size, f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
278 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
279 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
280 /* add sequence end code to have a real mpeg file */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
281 outbuf[0] = 0x00; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
282 outbuf[1] = 0x00; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
283 outbuf[2] = 0x01; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
284 outbuf[3] = 0xb7; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
285 fwrite(outbuf, 1, 4, f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
286 fclose(f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
287 free(picture_buf); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
288 free(outbuf); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
289 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
290 avcodec_close(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
291 av_free(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
292 av_free(picture); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
293 printf("\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
294 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
295 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
296 /* |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
297 * Video decoding example |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
298 */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
299 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
300 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
301 { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
302 FILE *f; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
303 int i; |
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 f=fopen(filename,"w"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
306 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
|
307 for(i=0;i<ysize;i++) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
308 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
|
309 fclose(f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
310 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
311 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
312 void video_decode_example(const char *outfilename, const char *filename) |
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 AVCodec *codec; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
315 AVCodecContext *c= NULL; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
316 int frame, size, got_picture, len; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
317 FILE *f; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
318 AVFrame *picture; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
319 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
320 char buf[1024]; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
321 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
322 /* 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
|
323 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
|
324 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
325 printf("Video decoding\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
326 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
327 /* find the mpeg1 video decoder */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
328 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
|
329 if (!codec) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
330 fprintf(stderr, "codec not found\n"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
331 exit(1); |
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 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
334 c= avcodec_alloc_context(); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
335 picture= avcodec_alloc_frame(); |
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 if(codec->capabilities&CODEC_CAP_TRUNCATED) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
338 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
|
339 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
340 /* 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
|
341 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
|
342 available in the bitstream. */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
343 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
344 /* open it */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
345 if (avcodec_open(c, codec) < 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
346 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
|
347 exit(1); |
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 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
350 /* 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
|
351 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
352 f = fopen(filename, "rb"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
353 if (!f) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
354 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
|
355 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
356 } |
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 frame = 0; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
359 for(;;) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
360 size = fread(inbuf, 1, INBUF_SIZE, f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
361 if (size == 0) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
362 break; |
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 /* 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
|
365 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
|
366 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
|
367 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
368 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
|
369 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
|
370 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
|
371 'height' before initializing them. */ |
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 /* 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
|
374 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
|
375 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
|
376 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
377 /* 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
|
378 feed decoder and see if it could decode a frame */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
379 inbuf_ptr = inbuf; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
380 while (size > 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
381 len = avcodec_decode_video(c, picture, &got_picture, |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
382 inbuf_ptr, size); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
383 if (len < 0) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
384 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
|
385 exit(1); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
386 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
387 if (got_picture) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
388 printf("saving frame %3d\n", frame); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
389 fflush(stdout); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
390 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
391 /* 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
|
392 free it */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
393 snprintf(buf, sizeof(buf), outfilename, frame); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
394 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
|
395 c->width, c->height, buf); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
396 frame++; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
397 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
398 size -= len; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
399 inbuf_ptr += len; |
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 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
402 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
403 /* 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
|
404 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
|
405 chance to get the last frame of the video */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
406 len = avcodec_decode_video(c, picture, &got_picture, |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
407 NULL, 0); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
408 if (got_picture) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
409 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
|
410 fflush(stdout); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
411 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
412 /* 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
|
413 free it */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
414 snprintf(buf, sizeof(buf), outfilename, frame); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
415 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
|
416 c->width, c->height, buf); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
417 frame++; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
418 } |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
419 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
420 fclose(f); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
421 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
422 avcodec_close(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
423 av_free(c); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
424 av_free(picture); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
425 printf("\n"); |
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 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
428 int main(int argc, char **argv) |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
429 { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
430 const char *filename; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
431 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
432 /* 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
|
433 avcodec_init(); |
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 /* register all the codecs */ |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
436 avcodec_register_all(); |
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 if (argc <= 1) { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
439 audio_encode_example("/tmp/test.mp2"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
440 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
|
441 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
442 video_encode_example("/tmp/test.mpg"); |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
443 filename = "/tmp/test.mpg"; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
444 } else { |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
445 filename = argv[1]; |
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 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
448 // 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
|
449 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
|
450 |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
451 return 0; |
bd89b50b48fc
Rename apiexample.c --> api-example.c to be consistent with other example files.
diego
parents:
diff
changeset
|
452 } |