Add Cineform HD Decoder
[libav.git] / libavformat / voc_packet.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "avformat.h"
20 #include "internal.h"
21 #include "voc.h"
22
23 int
24 ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
25 {
26 VocDecContext *voc = s->priv_data;
27 AVCodecParameters *par = st->codecpar;
28 AVIOContext *pb = s->pb;
29 VocType type;
30 int size, tmp_codec=-1;
31 int sample_rate = 0;
32 int channels = 1;
33
34 while (!voc->remaining_size) {
35 type = avio_r8(pb);
36 if (type == VOC_TYPE_EOF)
37 return AVERROR(EIO);
38 voc->remaining_size = avio_rl24(pb);
39 if (!voc->remaining_size) {
40 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
41 return AVERROR(EIO);
42 voc->remaining_size = avio_size(pb) - avio_tell(pb);
43 }
44 max_size -= 4;
45
46 switch (type) {
47 case VOC_TYPE_VOICE_DATA:
48 if (!par->sample_rate) {
49 par->sample_rate = 1000000 / (256 - avio_r8(pb));
50 if (sample_rate)
51 par->sample_rate = sample_rate;
52 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
53 par->channels = channels;
54 par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
55 } else
56 avio_skip(pb, 1);
57 tmp_codec = avio_r8(pb);
58 voc->remaining_size -= 2;
59 max_size -= 2;
60 channels = 1;
61 break;
62
63 case VOC_TYPE_VOICE_DATA_CONT:
64 break;
65
66 case VOC_TYPE_EXTENDED:
67 sample_rate = avio_rl16(pb);
68 avio_r8(pb);
69 channels = avio_r8(pb) + 1;
70 sample_rate = 256000000 / (channels * (65536 - sample_rate));
71 voc->remaining_size = 0;
72 max_size -= 4;
73 break;
74
75 case VOC_TYPE_NEW_VOICE_DATA:
76 if (!par->sample_rate) {
77 par->sample_rate = avio_rl32(pb);
78 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
79 par->bits_per_coded_sample = avio_r8(pb);
80 par->channels = avio_r8(pb);
81 } else
82 avio_skip(pb, 6);
83 tmp_codec = avio_rl16(pb);
84 avio_skip(pb, 4);
85 voc->remaining_size -= 12;
86 max_size -= 12;
87 break;
88
89 default:
90 avio_skip(pb, voc->remaining_size);
91 max_size -= voc->remaining_size;
92 voc->remaining_size = 0;
93 break;
94 }
95 }
96
97 if (tmp_codec >= 0) {
98 tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
99 if (par->codec_id == AV_CODEC_ID_NONE)
100 par->codec_id = tmp_codec;
101 else if (par->codec_id != tmp_codec)
102 av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
103 if (par->codec_id == AV_CODEC_ID_NONE) {
104 if (s->audio_codec_id == AV_CODEC_ID_NONE) {
105 av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
106 return AVERROR(EINVAL);
107 }
108 av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
109 }
110 }
111
112 par->bit_rate = par->sample_rate * par->bits_per_coded_sample;
113
114 if (max_size <= 0)
115 max_size = 2048;
116 size = FFMIN(voc->remaining_size, max_size);
117 voc->remaining_size -= size;
118 return av_get_packet(pb, pkt, size);
119 }