FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
act.c
Go to the documentation of this file.
1 /*
2  * ACT file format demuxer
3  * Copyright (c) 2007-2008 Vladimir Voroshilov
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avformat.h"
22 #include "riff.h"
23 #include "internal.h"
24 #include "libavcodec/get_bits.h"
25 
26 #define CHUNK_SIZE 512
27 #define RIFF_TAG MKTAG('R','I','F','F')
28 #define WAVE_TAG MKTAG('W','A','V','E')
29 
30 typedef struct{
32  uint8_t audio_buffer[22];///< temporary buffer for ACT frame
33  char second_packet; ///< 1 - if temporary buffer contains valid (second) G.729 packet
34 } ACTContext;
35 
36 static int probe(AVProbeData *p)
37 {
38  int i;
39 
40  if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
41  (AV_RL32(&p->buf[8]) != WAVE_TAG) ||
42  (AV_RL32(&p->buf[16]) != 16))
43  return 0;
44 
45  //We can't be sure that this is ACT and not regular WAV
46  if (p->buf_size<512)
47  return 0;
48 
49  for(i=44; i<256; i++)
50  if(p->buf[i])
51  return 0;
52 
53  if(p->buf[256]!=0x84)
54  return 0;
55 
56  for(i=264; i<512; i++)
57  if(p->buf[i])
58  return 0;
59 
60  return AVPROBE_SCORE_MAX;
61 }
62 
64 {
65  ACTContext* ctx = s->priv_data;
66  AVIOContext *pb = s->pb;
67  int size;
68  AVStream* st;
69 
70  int min,sec,msec;
71 
72  st = avformat_new_stream(s, NULL);
73  if (!st)
74  return AVERROR(ENOMEM);
75 
76  avio_skip(pb, 16);
77  size=avio_rl32(pb);
78  ff_get_wav_header(s, pb, st->codecpar, size, 0);
79 
80  /*
81  8000Hz (Fine-rec) file format has 10 bytes long
82  packets with 10ms of sound data in them
83  */
84  if (st->codecpar->sample_rate != 8000) {
85  av_log(s, AV_LOG_ERROR, "Sample rate %d is not supported.\n", st->codecpar->sample_rate);
86  return AVERROR_INVALIDDATA;
87  }
88 
89  st->codecpar->frame_size=80;
90  st->codecpar->channels=1;
91  avpriv_set_pts_info(st, 64, 1, 100);
92 
94 
95  avio_seek(pb, 257, SEEK_SET);
96  msec=avio_rl16(pb);
97  sec=avio_r8(pb);
98  min=avio_rl32(pb);
99 
100  st->duration = av_rescale(1000*(min*60+sec)+msec, st->codecpar->sample_rate, 1000 * st->codecpar->frame_size);
101 
103 
104  avio_seek(pb, 512, SEEK_SET);
105 
106  return 0;
107 }
108 
109 
111  AVPacket *pkt)
112 {
113  ACTContext *ctx = s->priv_data;
114  AVIOContext *pb = s->pb;
115  int ret;
116  int frame_size=s->streams[0]->codecpar->sample_rate==8000?10:22;
117 
118 
119  if(s->streams[0]->codecpar->sample_rate==8000)
120  ret=av_new_packet(pkt, 10);
121  else
122  ret=av_new_packet(pkt, 11);
123 
124  if(ret)
125  return ret;
126 
127  if(s->streams[0]->codecpar->sample_rate==4400 && !ctx->second_packet)
128  {
129  ret = avio_read(pb, ctx->audio_buffer, frame_size);
130 
131  if(ret<0)
132  return ret;
133  if(ret!=frame_size)
134  return AVERROR(EIO);
135 
136  pkt->data[0]=ctx->audio_buffer[11];
137  pkt->data[1]=ctx->audio_buffer[0];
138  pkt->data[2]=ctx->audio_buffer[12];
139  pkt->data[3]=ctx->audio_buffer[1];
140  pkt->data[4]=ctx->audio_buffer[13];
141  pkt->data[5]=ctx->audio_buffer[2];
142  pkt->data[6]=ctx->audio_buffer[14];
143  pkt->data[7]=ctx->audio_buffer[3];
144  pkt->data[8]=ctx->audio_buffer[15];
145  pkt->data[9]=ctx->audio_buffer[4];
146  pkt->data[10]=ctx->audio_buffer[16];
147 
148  ctx->second_packet=1;
149  }
150  else if(s->streams[0]->codecpar->sample_rate==4400 && ctx->second_packet)
151  {
152  pkt->data[0]=ctx->audio_buffer[5];
153  pkt->data[1]=ctx->audio_buffer[17];
154  pkt->data[2]=ctx->audio_buffer[6];
155  pkt->data[3]=ctx->audio_buffer[18];
156  pkt->data[4]=ctx->audio_buffer[7];
157  pkt->data[5]=ctx->audio_buffer[19];
158  pkt->data[6]=ctx->audio_buffer[8];
159  pkt->data[7]=ctx->audio_buffer[20];
160  pkt->data[8]=ctx->audio_buffer[9];
161  pkt->data[9]=ctx->audio_buffer[21];
162  pkt->data[10]=ctx->audio_buffer[10];
163 
164  ctx->second_packet=0;
165  }
166  else // 8000 Hz
167  {
168  ret = avio_read(pb, ctx->audio_buffer, frame_size);
169 
170  if(ret<0)
171  return ret;
172  if(ret!=frame_size)
173  return AVERROR(EIO);
174 
175  pkt->data[0]=ctx->audio_buffer[5];
176  pkt->data[1]=ctx->audio_buffer[0];
177  pkt->data[2]=ctx->audio_buffer[6];
178  pkt->data[3]=ctx->audio_buffer[1];
179  pkt->data[4]=ctx->audio_buffer[7];
180  pkt->data[5]=ctx->audio_buffer[2];
181  pkt->data[6]=ctx->audio_buffer[8];
182  pkt->data[7]=ctx->audio_buffer[3];
183  pkt->data[8]=ctx->audio_buffer[9];
184  pkt->data[9]=ctx->audio_buffer[4];
185  }
186 
188 
189  if(ctx->bytes_left_in_chunk < frame_size)
190  {
191  avio_skip(pb, ctx->bytes_left_in_chunk);
193  }
194 
195  pkt->duration=1;
196 
197  return ret;
198 }
199 
201  .name = "act",
202  .long_name = "ACT Voice file format",
203  .priv_data_size = sizeof(ACTContext),
204  .read_probe = probe,
207 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:155
AVInputFormat ff_act_demuxer
Definition: act.c:200
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: act.c:110
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4601
int bytes_left_in_chunk
Definition: act.c:31
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4066
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:316
static AVPacket pkt
uint8_t audio_buffer[22]
temporary buffer for ACT frame
Definition: act.c:32
int frame_size
Audio only.
Definition: avcodec.h:4187
Format I/O context.
Definition: avformat.h:1349
uint8_t
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1675
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4231
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
uint8_t * data
Definition: avcodec.h:1657
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
static int probe(AVProbeData *p)
Definition: act.c:36
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:616
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#define AVERROR(e)
Definition: error.h:43
#define RIFF_TAG
Definition: act.c:27
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:607
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define WAVE_TAG
Definition: act.c:28
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
AVFormatContext * ctx
Definition: movenc.c:48
Stream structure.
Definition: avformat.h:889
#define CHUNK_SIZE
Definition: act.c:26
int frame_size
Definition: mxfenc.c:1820
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
static int read_header(AVFormatContext *s)
Definition: act.c:63
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4176
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
Definition: act.c:30
void * priv_data
Format private data.
Definition: avformat.h:1377
int channels
Audio only.
Definition: avcodec.h:4172
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
AVCodecParameters * codecpar
Definition: avformat.h:1252
char second_packet
1 - if temporary buffer contains valid (second) G.729 packet
Definition: act.c:33
float min
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1634