annotate amr.c @ 168:808dabf5a88e libavformat

* First round of AV sync fixes. * -r now does the right thing. * frame rate and frame sizes now share same abbv. table and you can do -r ntsc as well as -s ntsc
author romansh
date Sat, 12 Jul 2003 07:37:05 +0000
parents 7f3e6d83abd3
children 59c2e84817a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
1 /*
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
2 * amr file format
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
3 * Copyright (c) 2001 ffmpeg project
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
4 *
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
9 *
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
13 * Lesser General Public License for more details.
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
14 *
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
18 */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
19
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
20 /*
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
21 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
22
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
23 Only amr narrowband (not amr-wb) is supported for now.
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
24
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
25 */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
26 #include "avformat.h"
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
27 #include "avi.h"
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
28
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
29 static const unsigned char AMR_header [] = "#!AMR\n";
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
30
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
31 /* AMR_FILE header */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
32 static int put_amr_header(ByteIOContext *pb, AVCodecContext *enc)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
33 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
34 put_tag(pb, AMR_header); /* magic number */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
35 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
36 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
37
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
38 static int amr_write_header(AVFormatContext *s)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
39 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
40 ByteIOContext *pb = &s->pb;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
41
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
42 s->priv_data = NULL;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
43
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
44 /* format header */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
45 if (put_amr_header(pb, &s->streams[0]->codec) < 0) {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
46 return -1;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
47 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
48
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
49 put_flush_packet(pb);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
50
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
51 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
52 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
53
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
54 static int amr_write_packet(AVFormatContext *s, int stream_index_ptr,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
55 uint8_t *buf, int size, int force_pts)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
56 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
57 put_buffer(&s->pb, buf, size);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
58 put_flush_packet(&s->pb);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
59 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
60 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
61
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
62 static int amr_write_trailer(AVFormatContext *s)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
63 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
64 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
65 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
66
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
67 static int amr_probe(AVProbeData *p)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
68 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
69 /* check file header */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
70 if (p->buf_size < 6)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
71 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
72 if(memcmp(p->buf,AMR_header,6)==0)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
73 return AVPROBE_SCORE_MAX;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
74 else
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
75 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
76 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
77
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
78 /* amr input */
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
79 static int amr_read_header(AVFormatContext *s,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
80 AVFormatParameters *ap)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
81 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
82 ByteIOContext *pb = &s->pb;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
83 AVStream *st;
154
7f3e6d83abd3 amr typefix patch by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 148
diff changeset
84 uint8_t header[6];
148
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
85
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
86 get_buffer(pb, header, 6);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
87
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
88 if(memcmp(header,AMR_header,6)!=0)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
89 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
90 return -1;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
91 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
92
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
93 st = av_new_stream(s, 0);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
94 if (!st)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
95 return AVERROR_NOMEM;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
96
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
97 st->codec.codec_type = CODEC_TYPE_AUDIO;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
98 st->codec.codec_tag = CODEC_ID_AMR_NB;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
99 st->codec.codec_id = CODEC_ID_AMR_NB;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
100 st->codec.channels = 1;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
101 st->codec.sample_rate = 8000;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
102 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
103 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
104
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
105 #define MAX_SIZE 32
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
106
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
107 static int amr_read_packet(AVFormatContext *s,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
108 AVPacket *pkt)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
109 {
154
7f3e6d83abd3 amr typefix patch by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 148
diff changeset
110 static uint16_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
7f3e6d83abd3 amr typefix patch by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 148
diff changeset
111 uint8_t toc, q, ft;
148
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
112 int read;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
113 int size;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
114
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
115 if (url_feof(&s->pb))
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
116 return -EIO;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
117
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
118 toc=0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
119
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
120 toc=get_byte(&s->pb);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
121
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
122 q = (toc >> 2) & 0x01;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
123 ft = (toc >> 3) & 0x0F;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
124
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
125 size=packed_size[ft];
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
126 //printf("amr_read_packet size=%d\n",size);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
127
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
128 if (av_new_packet(pkt, size+1))
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
129 return -EIO;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
130 pkt->stream_index = 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
131
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
132 pkt->data[0]=toc;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
133
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
134 read = get_buffer(&s->pb, pkt->data+1, size);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
135
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
136 if (read != size)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
137 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
138 av_free_packet(pkt);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
139 return -EIO;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
140 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
141
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
142 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
143 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
144
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
145 static int amr_read_close(AVFormatContext *s)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
146 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
147 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
148 }
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
149
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
150 static AVInputFormat amr_iformat = {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
151 "amr",
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
152 "3gpp amr file format",
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
153 0,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
154 amr_probe,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
155 amr_read_header,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
156 amr_read_packet,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
157 amr_read_close,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
158 };
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
159
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
160 static AVOutputFormat amr_oformat = {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
161 "amr",
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
162 "3gpp amr file format",
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
163 "audio/amr",
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
164 "amr",
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
165 0,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
166 CODEC_ID_AMR_NB,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
167 CODEC_ID_NONE,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
168 amr_write_header,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
169 amr_write_packet,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
170 amr_write_trailer,
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
171 };
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
172
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
173 int amr_init(void)
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
174 {
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
175 av_register_input_format(&amr_iformat);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
176 av_register_output_format(&amr_oformat);
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
177 return 0;
31d7c9a8678c * amr format - by Johannes Carlsson
kabi
parents:
diff changeset
178 }