FFmpeg
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 "avio_internal.h"
23 #include "riff.h"
24 #include "internal.h"
25 #include "libavcodec/get_bits.h"
26 
27 #define CHUNK_SIZE 512
28 #define RIFF_TAG MKTAG('R','I','F','F')
29 #define WAVE_TAG MKTAG('W','A','V','E')
30 
31 typedef struct{
33  uint8_t audio_buffer[22];///< temporary buffer for ACT frame
34  char second_packet; ///< 1 - if temporary buffer contains valid (second) G.729 packet
35 } ACTContext;
36 
37 static int probe(const AVProbeData *p)
38 {
39  int i;
40 
41  if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
42  (AV_RL32(&p->buf[8]) != WAVE_TAG) ||
43  (AV_RL32(&p->buf[16]) != 16))
44  return 0;
45 
46  //We can't be sure that this is ACT and not regular WAV
47  if (p->buf_size<512)
48  return 0;
49 
50  for(i=44; i<256; i++)
51  if(p->buf[i])
52  return 0;
53 
54  if(p->buf[256]!=0x84)
55  return 0;
56 
57  for(i=264; i<512; i++)
58  if(p->buf[i])
59  return 0;
60 
61  return AVPROBE_SCORE_MAX;
62 }
63 
65 {
66  ACTContext* ctx = s->priv_data;
67  AVIOContext *pb = s->pb;
68  int size;
69  AVStream* st;
70 
71  int min,sec,msec;
72 
73  st = avformat_new_stream(s, NULL);
74  if (!st)
75  return AVERROR(ENOMEM);
76 
77  avio_skip(pb, 16);
78  size=avio_rl32(pb);
79  ff_get_wav_header(s, pb, st->codecpar, size, 0);
80 
81  /*
82  8000Hz (Fine-rec) file format has 10 bytes long
83  packets with 10ms of sound data in them
84  */
85  if (st->codecpar->sample_rate != 8000) {
86  av_log(s, AV_LOG_ERROR, "Sample rate %d is not supported.\n", st->codecpar->sample_rate);
87  return AVERROR_INVALIDDATA;
88  }
89 
90  st->codecpar->frame_size=80;
91  st->codecpar->channels=1;
92  avpriv_set_pts_info(st, 64, 1, 100);
93 
95 
96  avio_seek(pb, 257, SEEK_SET);
97  msec=avio_rl16(pb);
98  sec=avio_r8(pb);
99  min=avio_rl32(pb);
100 
101  st->duration = av_rescale(1000*(min*60+sec)+msec, st->codecpar->sample_rate, 1000 * st->codecpar->frame_size);
102 
103  ctx->bytes_left_in_chunk=CHUNK_SIZE;
104 
105  avio_seek(pb, 512, SEEK_SET);
106 
107  return 0;
108 }
109 
110 
112  AVPacket *pkt)
113 {
114  ACTContext *ctx = s->priv_data;
115  AVIOContext *pb = s->pb;
116  int ret;
117  int frame_size=s->streams[0]->codecpar->sample_rate==8000?10:22;
118 
119 
120  if(s->streams[0]->codecpar->sample_rate==8000)
121  ret=av_new_packet(pkt, 10);
122  else
123  ret=av_new_packet(pkt, 11);
124 
125  if(ret)
126  return ret;
127 
128  if(s->streams[0]->codecpar->sample_rate==4400 && !ctx->second_packet)
129  {
130  ret = ffio_read_size(pb, ctx->audio_buffer, frame_size);
131 
132  if(ret<0)
133  return ret;
134 
135  pkt->data[0]=ctx->audio_buffer[11];
136  pkt->data[1]=ctx->audio_buffer[0];
137  pkt->data[2]=ctx->audio_buffer[12];
138  pkt->data[3]=ctx->audio_buffer[1];
139  pkt->data[4]=ctx->audio_buffer[13];
140  pkt->data[5]=ctx->audio_buffer[2];
141  pkt->data[6]=ctx->audio_buffer[14];
142  pkt->data[7]=ctx->audio_buffer[3];
143  pkt->data[8]=ctx->audio_buffer[15];
144  pkt->data[9]=ctx->audio_buffer[4];
145  pkt->data[10]=ctx->audio_buffer[16];
146 
147  ctx->second_packet=1;
148  }
149  else if(s->streams[0]->codecpar->sample_rate==4400 && ctx->second_packet)
150  {
151  pkt->data[0]=ctx->audio_buffer[5];
152  pkt->data[1]=ctx->audio_buffer[17];
153  pkt->data[2]=ctx->audio_buffer[6];
154  pkt->data[3]=ctx->audio_buffer[18];
155  pkt->data[4]=ctx->audio_buffer[7];
156  pkt->data[5]=ctx->audio_buffer[19];
157  pkt->data[6]=ctx->audio_buffer[8];
158  pkt->data[7]=ctx->audio_buffer[20];
159  pkt->data[8]=ctx->audio_buffer[9];
160  pkt->data[9]=ctx->audio_buffer[21];
161  pkt->data[10]=ctx->audio_buffer[10];
162 
163  ctx->second_packet=0;
164  }
165  else // 8000 Hz
166  {
167  ret = ffio_read_size(pb, ctx->audio_buffer, frame_size);
168 
169  if(ret<0)
170  return ret;
171 
172  pkt->data[0]=ctx->audio_buffer[5];
173  pkt->data[1]=ctx->audio_buffer[0];
174  pkt->data[2]=ctx->audio_buffer[6];
175  pkt->data[3]=ctx->audio_buffer[1];
176  pkt->data[4]=ctx->audio_buffer[7];
177  pkt->data[5]=ctx->audio_buffer[2];
178  pkt->data[6]=ctx->audio_buffer[8];
179  pkt->data[7]=ctx->audio_buffer[3];
180  pkt->data[8]=ctx->audio_buffer[9];
181  pkt->data[9]=ctx->audio_buffer[4];
182  }
183 
184  ctx->bytes_left_in_chunk -= frame_size;
185 
186  if(ctx->bytes_left_in_chunk < frame_size)
187  {
188  avio_skip(pb, ctx->bytes_left_in_chunk);
189  ctx->bytes_left_in_chunk=CHUNK_SIZE;
190  }
191 
192  pkt->duration=1;
193 
194  return ret;
195 }
196 
198  .name = "act",
199  .long_name = "ACT Voice file format",
200  .priv_data_size = sizeof(ACTContext),
201  .read_probe = probe,
204 };
read_header
static int read_header(AVFormatContext *s)
Definition: act.c:64
RIFF_TAG
#define RIFF_TAG
Definition: act.c:28
ACTContext::second_packet
char second_packet
1 - if temporary buffer contains valid (second) G.729 packet
Definition: act.c:34
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
WAVE_TAG
#define WAVE_TAG
Definition: act.c:29
ACTContext
Definition: act.c:31
CHUNK_SIZE
#define CHUNK_SIZE
Definition: act.c:27
ACTContext::bytes_left_in_chunk
int bytes_left_in_chunk
Definition: act.c:32
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:450
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:985
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:181
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
frame_size
int frame_size
Definition: mxfenc.c:2199
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: act.c:111
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:447
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
size
int size
Definition: twinvq_data.h:10344
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
avio_internal.h
ff_act_demuxer
const AVInputFormat ff_act_demuxer
Definition: act.c:197
ff_get_wav_header
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:90
av_rescale
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:128
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
probe
static int probe(const AVProbeData *p)
Definition: act.c:37
AV_CODEC_ID_G729
@ AV_CODEC_ID_G729
Definition: codec_id.h:476
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, 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:1196
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
riff.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:691
min
float min
Definition: vorbis_enc_data.h:429