2 * ACT file format demuxer
3 * Copyright (c) 2007-2008 Vladimir Voroshilov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
24 #include "avio_internal.h"
29 #define CHUNK_SIZE 512
30 #define RIFF_TAG MKTAG('R','I','F','F')
31 #define WAVE_TAG MKTAG('W','A','V','E')
34 int bytes_left_in_chunk
;
35 uint8_t audio_buffer
[22];///< temporary buffer for ACT frame
36 char second_packet
; ///< 1 - if temporary buffer contains valid (second) G.729 packet
39 static int probe(const AVProbeData
*p
)
43 if ((AV_RL32(&p
->buf
[0]) != RIFF_TAG
) ||
44 (AV_RL32(&p
->buf
[8]) != WAVE_TAG
) ||
45 (AV_RL32(&p
->buf
[16]) != 16))
48 //We can't be sure that this is ACT and not regular WAV
59 for(i
=264; i
<512; i
++)
63 return AVPROBE_SCORE_MAX
;
66 static int read_header(AVFormatContext
*s
)
68 ACTContext
* ctx
= s
->priv_data
;
69 AVIOContext
*pb
= s
->pb
;
76 st
= avformat_new_stream(s
, NULL
);
78 return AVERROR(ENOMEM
);
82 ret
= ff_get_wav_header(s
, pb
, st
->codecpar
, size
, 0);
87 8000Hz (Fine-rec) file format has 10 bytes long
88 packets with 10ms of sound data in them
90 if (st
->codecpar
->sample_rate
!= 8000) {
91 av_log(s
, AV_LOG_ERROR
, "Sample rate %d is not supported.\n", st
->codecpar
->sample_rate
);
92 return AVERROR_INVALIDDATA
;
95 st
->codecpar
->frame_size
=80;
96 st
->codecpar
->ch_layout
.nb_channels
= 1;
97 avpriv_set_pts_info(st
, 64, 1, 100);
99 st
->codecpar
->codec_id
=AV_CODEC_ID_G729
;
101 avio_seek(pb
, 257, SEEK_SET
);
106 st
->duration
= av_rescale(1000*(min
*60+sec
)+msec
, st
->codecpar
->sample_rate
, 1000 * st
->codecpar
->frame_size
);
108 ctx
->bytes_left_in_chunk
=CHUNK_SIZE
;
110 avio_seek(pb
, 512, SEEK_SET
);
116 static int read_packet(AVFormatContext
*s
,
119 ACTContext
*ctx
= s
->priv_data
;
120 AVIOContext
*pb
= s
->pb
;
122 int frame_size
=s
->streams
[0]->codecpar
->sample_rate
==8000?10:22;
125 if(s
->streams
[0]->codecpar
->sample_rate
==8000)
126 ret
=av_new_packet(pkt
, 10);
128 ret
=av_new_packet(pkt
, 11);
133 if(s
->streams
[0]->codecpar
->sample_rate
==4400 && !ctx
->second_packet
)
135 ret
= ffio_read_size(pb
, ctx
->audio_buffer
, frame_size
);
140 pkt
->data
[0]=ctx
->audio_buffer
[11];
141 pkt
->data
[1]=ctx
->audio_buffer
[0];
142 pkt
->data
[2]=ctx
->audio_buffer
[12];
143 pkt
->data
[3]=ctx
->audio_buffer
[1];
144 pkt
->data
[4]=ctx
->audio_buffer
[13];
145 pkt
->data
[5]=ctx
->audio_buffer
[2];
146 pkt
->data
[6]=ctx
->audio_buffer
[14];
147 pkt
->data
[7]=ctx
->audio_buffer
[3];
148 pkt
->data
[8]=ctx
->audio_buffer
[15];
149 pkt
->data
[9]=ctx
->audio_buffer
[4];
150 pkt
->data
[10]=ctx
->audio_buffer
[16];
152 ctx
->second_packet
=1;
154 else if(s
->streams
[0]->codecpar
->sample_rate
==4400 && ctx
->second_packet
)
156 pkt
->data
[0]=ctx
->audio_buffer
[5];
157 pkt
->data
[1]=ctx
->audio_buffer
[17];
158 pkt
->data
[2]=ctx
->audio_buffer
[6];
159 pkt
->data
[3]=ctx
->audio_buffer
[18];
160 pkt
->data
[4]=ctx
->audio_buffer
[7];
161 pkt
->data
[5]=ctx
->audio_buffer
[19];
162 pkt
->data
[6]=ctx
->audio_buffer
[8];
163 pkt
->data
[7]=ctx
->audio_buffer
[20];
164 pkt
->data
[8]=ctx
->audio_buffer
[9];
165 pkt
->data
[9]=ctx
->audio_buffer
[21];
166 pkt
->data
[10]=ctx
->audio_buffer
[10];
168 ctx
->second_packet
=0;
172 ret
= ffio_read_size(pb
, ctx
->audio_buffer
, frame_size
);
177 pkt
->data
[0]=ctx
->audio_buffer
[5];
178 pkt
->data
[1]=ctx
->audio_buffer
[0];
179 pkt
->data
[2]=ctx
->audio_buffer
[6];
180 pkt
->data
[3]=ctx
->audio_buffer
[1];
181 pkt
->data
[4]=ctx
->audio_buffer
[7];
182 pkt
->data
[5]=ctx
->audio_buffer
[2];
183 pkt
->data
[6]=ctx
->audio_buffer
[8];
184 pkt
->data
[7]=ctx
->audio_buffer
[3];
185 pkt
->data
[8]=ctx
->audio_buffer
[9];
186 pkt
->data
[9]=ctx
->audio_buffer
[4];
189 ctx
->bytes_left_in_chunk
-= frame_size
;
191 if(ctx
->bytes_left_in_chunk
< frame_size
)
193 avio_skip(pb
, ctx
->bytes_left_in_chunk
);
194 ctx
->bytes_left_in_chunk
=CHUNK_SIZE
;
202 const FFInputFormat ff_act_demuxer
= {
204 .p
.long_name
= "ACT Voice file format",
205 .priv_data_size
= sizeof(ACTContext
),
207 .read_header
= read_header
,
208 .read_packet
= read_packet
,