annotate vocenc.c @ 1535:a3fbb2b1fccf libavformat

split voc.c into vocdec.c and vocenc.c
author aurel
date Tue, 21 Nov 2006 13:08:04 +0000
parents voc.c@0899bfe4105c
children 6dc55143f99f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
1 /*
1535
a3fbb2b1fccf split voc.c into vocdec.c and vocenc.c
aurel
parents: 1358
diff changeset
2 * Creative Voice File muxer.
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
4 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1172
diff changeset
5 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1172
diff changeset
6 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1172
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1172
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
11 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1172
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
15 * Lesser General Public License for more details.
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
16 *
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1172
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
20 */
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
21
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
22 #include "avformat.h"
1172
6a5e58d2114b move common stuff from avienc.c and wav.c to new file riff.c
mru
parents: 1169
diff changeset
23 #include "riff.h" /* for CodecTag */
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
24 #include "voc.h"
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
25
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
26
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
27 static const unsigned char voc_magic[] = "Creative Voice File\x1A";
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
28
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
29 static const CodecTag voc_codec_tags[] = {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
30 {CODEC_ID_PCM_U8, 0x00},
960
6c30a916e27d add Creative 8 bits ADPCM schemes support
aurel
parents: 938
diff changeset
31 {CODEC_ID_ADPCM_SBPRO_4, 0x01},
6c30a916e27d add Creative 8 bits ADPCM schemes support
aurel
parents: 938
diff changeset
32 {CODEC_ID_ADPCM_SBPRO_3, 0x02},
6c30a916e27d add Creative 8 bits ADPCM schemes support
aurel
parents: 938
diff changeset
33 {CODEC_ID_ADPCM_SBPRO_2, 0x03},
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
34 {CODEC_ID_PCM_S16LE, 0x04},
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
35 {CODEC_ID_PCM_ALAW, 0x06},
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
36 {CODEC_ID_PCM_MULAW, 0x07},
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
37 {CODEC_ID_ADPCM_CT, 0x0200},
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
38 {0, 0},
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
39 };
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
40
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
41
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
42 typedef struct voc_enc_context {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
43 int param_written;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
44 } voc_enc_context_t;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
45
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
46 static int voc_write_header(AVFormatContext *s)
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
47 {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
48 ByteIOContext *pb = &s->pb;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
49 const int header_size = 26;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
50 const int version = 0x0114;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
51
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
52 if (s->nb_streams != 1
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
53 || s->streams[0]->codec->codec_type != CODEC_TYPE_AUDIO)
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
54 return AVERROR_NOTSUPP;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
55
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
56 put_buffer(pb, voc_magic, sizeof(voc_magic) - 1);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
57 put_le16(pb, header_size);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
58 put_le16(pb, version);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
59 put_le16(pb, ~version + 0x1234);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
60
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
61 return 0;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
62 }
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
63
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
64 static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
65 {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
66 voc_enc_context_t *voc = s->priv_data;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
67 AVCodecContext *enc = s->streams[0]->codec;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
68 ByteIOContext *pb = &s->pb;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
69
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
70 if (!voc->param_written) {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
71 int format = codec_get_tag(voc_codec_tags, enc->codec_id);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
72
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
73 if (format > 0xFF) {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
74 put_byte(pb, VOC_TYPE_NEW_VOICE_DATA);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
75 put_le24(pb, pkt->size + 12);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
76 put_le32(pb, enc->sample_rate);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
77 put_byte(pb, enc->bits_per_sample);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
78 put_byte(pb, enc->channels);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
79 put_le16(pb, format);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
80 put_le32(pb, 0);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
81 } else {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
82 if (s->streams[0]->codec->channels > 1) {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
83 put_byte(pb, VOC_TYPE_EXTENDED);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
84 put_le24(pb, 4);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
85 put_le16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
86 put_byte(pb, format);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
87 put_byte(pb, enc->channels - 1);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
88 }
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
89 put_byte(pb, VOC_TYPE_VOICE_DATA);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
90 put_le24(pb, pkt->size + 2);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
91 put_byte(pb, 256 - 1000000 / enc->sample_rate);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
92 put_byte(pb, format);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
93 }
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
94 voc->param_written = 1;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
95 } else {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
96 put_byte(pb, VOC_TYPE_VOICE_DATA_CONT);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
97 put_le24(pb, pkt->size);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
98 }
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
99
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
100 put_buffer(pb, pkt->data, pkt->size);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
101 return 0;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
102 }
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
103
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
104 static int voc_write_trailer(AVFormatContext *s)
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
105 {
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
106 put_byte(&s->pb, 0);
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
107 return 0;
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
108 }
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
109
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
110 AVOutputFormat voc_muxer = {
937
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
111 "voc",
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
112 "Creative Voice File format",
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
113 "audio/x-voc",
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
114 "voc",
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
115 sizeof(voc_enc_context_t),
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
116 CODEC_ID_PCM_U8,
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
117 CODEC_ID_NONE,
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
118 voc_write_header,
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
119 voc_write_packet,
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
120 voc_write_trailer,
a887adfe9dc5 add a Creative VOC (de)muxer
aurel
parents:
diff changeset
121 };