Libav
mtv.c
Go to the documentation of this file.
1 /*
2  * mtv demuxer
3  * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "libavutil/bswap.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 #define MTV_ASUBCHUNK_DATA_SIZE 500
33 #define MTV_HEADER_SIZE 512
34 #define MTV_AUDIO_PADDING_SIZE 12
35 #define AUDIO_SAMPLING_RATE 44100
36 
37 typedef struct MTVDemuxContext {
38 
39  unsigned int file_size;
40  unsigned int segments;
41  unsigned int audio_identifier;
42  unsigned int audio_br;
43  unsigned int img_colorfmt;
44  unsigned int img_bpp;
45  unsigned int img_width; //
46  unsigned int img_height; //
47  unsigned int img_segment_size;
48  unsigned int video_fps; //
49  unsigned int full_segment_size;
50 
52 
53 static int mtv_probe(AVProbeData *p)
54 {
55  /* Magic is 'AMV' */
56  if (*p->buf != 'A' || *(p->buf + 1) != 'M' || *(p->buf + 2) != 'V')
57  return 0;
58 
59  /* Check for nonzero in bpp and (width|height) header fields */
60  if(!(p->buf[51] && AV_RL16(&p->buf[52]) | AV_RL16(&p->buf[54])))
61  return 0;
62 
63  /* If width or height are 0 then imagesize header field should not */
64  if(!AV_RL16(&p->buf[52]) || !AV_RL16(&p->buf[54]))
65  {
66  if(!!AV_RL16(&p->buf[56]))
68  else
69  return 0;
70  }
71 
72  if(p->buf[51] != 16)
73  return AVPROBE_SCORE_EXTENSION / 2; // But we are going to assume 16bpp anyway ..
74 
75  return AVPROBE_SCORE_MAX;
76 }
77 
79 {
80  MTVDemuxContext *mtv = s->priv_data;
81  AVIOContext *pb = s->pb;
82  AVStream *st;
83  unsigned int audio_subsegments;
84 
85  avio_skip(pb, 3);
86  mtv->file_size = avio_rl32(pb);
87  mtv->segments = avio_rl32(pb);
88  avio_skip(pb, 32);
89  mtv->audio_identifier = avio_rl24(pb);
90  mtv->audio_br = avio_rl16(pb);
91  mtv->img_colorfmt = avio_rl24(pb);
92  mtv->img_bpp = avio_r8(pb);
93  mtv->img_width = avio_rl16(pb);
94  mtv->img_height = avio_rl16(pb);
95  mtv->img_segment_size = avio_rl16(pb);
96 
97  /* Calculate width and height if missing from header */
98 
99  if(!mtv->img_width)
100  mtv->img_width=mtv->img_segment_size / (mtv->img_bpp>>3)
101  / mtv->img_height;
102 
103  if(!mtv->img_height)
104  mtv->img_height=mtv->img_segment_size / (mtv->img_bpp>>3)
105  / mtv->img_width;
106 
107  avio_skip(pb, 4);
108  audio_subsegments = avio_rl16(pb);
109 
110  if (audio_subsegments == 0) {
111  avpriv_request_sample(s, "MTV files without audio");
112  return AVERROR_PATCHWELCOME;
113  }
114 
115  mtv->full_segment_size =
116  audio_subsegments * (MTV_AUDIO_PADDING_SIZE + MTV_ASUBCHUNK_DATA_SIZE) +
117  mtv->img_segment_size;
118  mtv->video_fps = (mtv->audio_br / 4) / audio_subsegments;
119 
120  // FIXME Add sanity check here
121 
122  // all systems go! init decoders
123 
124  // video - raw rgb565
125 
126  st = avformat_new_stream(s, NULL);
127  if(!st)
128  return AVERROR(ENOMEM);
129 
130  avpriv_set_pts_info(st, 64, 1, mtv->video_fps);
134  st->codec->width = mtv->img_width;
135  st->codec->height = mtv->img_height;
136  st->codec->extradata = av_strdup("BottomUp");
137  st->codec->extradata_size = 9;
138 
139  // audio - mp3
140 
141  st = avformat_new_stream(s, NULL);
142  if(!st)
143  return AVERROR(ENOMEM);
144 
148  st->codec->bit_rate = mtv->audio_br;
150 
151  // Jump over header
152 
153  if(avio_seek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
154  return AVERROR(EIO);
155 
156  return 0;
157 
158 }
159 
161 {
162  MTVDemuxContext *mtv = s->priv_data;
163  AVIOContext *pb = s->pb;
164  int ret;
165 
166  if((avio_tell(pb) - s->data_offset + mtv->img_segment_size) % mtv->full_segment_size)
167  {
169 
170  ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
171  if(ret < 0)
172  return ret;
173 
174  pkt->pos -= MTV_AUDIO_PADDING_SIZE;
175  pkt->stream_index = 1;
176 
177  }else
178  {
179  ret = av_get_packet(pb, pkt, mtv->img_segment_size);
180  if(ret < 0)
181  return ret;
182 
183  pkt->stream_index = 0;
184  }
185 
186  return ret;
187 }
188 
190  .name = "mtv",
191  .long_name = NULL_IF_CONFIG_SMALL("MTV"),
192  .priv_data_size = sizeof(MTVDemuxContext),
196 };
#define MTV_ASUBCHUNK_DATA_SIZE
Definition: mtv.c:32
Bytestream IO Context.
Definition: avio.h:68
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:992
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:2800
unsigned int img_bpp
frame bits per pixel
Definition: mtv.c:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
unsigned int img_width
Definition: mtv.c:45
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1248
#define AV_RL16
Definition: intreadwrite.h:42
int64_t data_offset
offset of the first packet
Definition: avformat.h:1220
unsigned int segments
number of 512 byte segments
Definition: mtv.c:40
unsigned int video_fps
Definition: mtv.c:48
#define MTV_HEADER_SIZE
Definition: mtv.c:33
Format I/O context.
Definition: avformat.h:922
unsigned int img_colorfmt
frame colorfmt rgb 565/555
Definition: mtv.c:43
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AVInputFormat ff_mtv_demuxer
Definition: mtv.c:189
enum AVStreamParseType need_parsing
Definition: avformat.h:867
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1158
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2497
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:115
static int mtv_read_header(AVFormatContext *s)
Definition: mtv.c:78
unsigned int audio_identifier
'MP3' on all files I have seen
Definition: mtv.c:41
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:219
unsigned int file_size
filesize, not always right
Definition: mtv.c:39
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
unsigned int full_segment_size
Definition: mtv.c:49
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:379
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
int bit_rate
the average bitrate
Definition: avcodec.h:1108
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1218
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define MTV_AUDIO_PADDING_SIZE
Definition: mtv.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1052
enum AVCodecID codec_id
Definition: avcodec.h:1061
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
AVIOContext * pb
I/O context.
Definition: avformat.h:964
unsigned int img_segment_size
size of image segment
Definition: mtv.c:47
int extradata_size
Definition: avcodec.h:1159
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
byte swapping routines
unsigned int img_height
Definition: mtv.c:46
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static int mtv_probe(AVProbeData *p)
Definition: mtv.c:53
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
full parsing and repack
Definition: avformat.h:653
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:548
Main libavformat public API header.
unsigned int audio_br
bitrate of audio channel (mp3)
Definition: mtv.c:42
#define AUDIO_SAMPLING_RATE
Definition: mtv.c:35
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:556
static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mtv.c:160
int stream_index
Definition: avcodec.h:969
This structure stores compressed data.
Definition: avcodec.h:944