Libav
voc_packet.c
Go to the documentation of this file.
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)
41  return AVERROR(EIO);
42  voc->remaining_size = avio_size(pb) - avio_tell(pb);
43  }
44  max_size -= 4;
45 
46  switch (type) {
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;
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 
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 
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 }
Bytestream IO Context.
Definition: avio.h:104
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
int size
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1983
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
Format I/O context.
Definition: avformat.h:940
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2348
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
VocType
Definition: voc.h:32
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
#define FFMIN(a, b)
Definition: common.h:66
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1100
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
Stream structure.
Definition: avformat.h:705
int ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition: voc_packet.c:24
const AVCodecTag ff_voc_codec_tags[]
Definition: voc.c:27
AVIOContext * pb
I/O context.
Definition: avformat.h:982
int sample_rate
Audio only.
Definition: avcodec.h:3564
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:649
Main libavformat public API header.
void * priv_data
Format private data.
Definition: avformat.h:968
int64_t remaining_size
Definition: voc.h:29
int bits_per_coded_sample
Definition: avcodec.h:3514
int channels
Audio only.
Definition: avcodec.h:3560
AVCodecParameters * codecpar
Definition: avformat.h:831
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:657
This structure stores compressed data.
Definition: avcodec.h:1323