annotate mtv.c @ 1615:8e55b4de921c libavformat

VP6 and flashsv stream copy and muxing support.
author banan
date Mon, 01 Jan 2007 22:52:22 +0000
parents 64a44155a525
children cf1622476eb7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1380
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
1 /*
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
2 * mtv demuxer
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
3 * Copyright (c) 2006 Reynaldo H. Verdejo Pinochet
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
4 *
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
5 * This file is part of FFmpeg.
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
6 *
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
11 *
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
15 * Lesser General Public License for more details.
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
16 *
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
20 */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
21
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
22 /**
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
23 * @file mtv.c
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
24 * MTV demuxer.
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
25 */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
26
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
27 #include "avformat.h"
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
28 #include "bswap.h"
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
29
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
30 #define MTV_ASUBCHUNK_DATA_SIZE 500
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
31 #define MTV_HEADER_SIZE 512
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
32 #define MTV_AUDIO_PADDING_SIZE 12
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
33 #define AUDIO_SAMPLING_RATE 44100
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
34 #define VIDEO_SID 0
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
35 #define AUDIO_SID 1
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
36
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
37 typedef struct MTVDemuxContext {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
38
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
39 unsigned int file_size; ///< filesize, not always right
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
40 unsigned int segments; ///< number of 512 byte segments
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
41 unsigned int audio_identifier; ///< 'MP3' on all files I have seen
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
42 unsigned int audio_br; ///< bitrate of audio chanel (mp3)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
43 unsigned int img_colorfmt; ///< frame colorfmt rgb 565/555
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
44 unsigned int img_bpp; ///< frame bits per pixel
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
45 unsigned int img_width; //
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
46 unsigned int img_height; //
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
47 unsigned int img_segment_size; ///< size of image segment
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
48 unsigned int video_fps; //
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
49 unsigned int audio_subsegments; ///< audio subsegments on one segment
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
50
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
51 uint8_t audio_packet_count;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
52
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
53 } MTVDemuxContext;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
54
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
55 static int mtv_probe(AVProbeData *p)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
56 {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
57 if(p->buf_size < 3)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
58 return 0;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
59
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
60 /* Magic is 'AMV' */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
61
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
62 if(*(p->buf) != 'A' || *(p->buf+1) != 'M' || *(p->buf+2) != 'V')
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
63 return 0;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
64
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
65 return AVPROBE_SCORE_MAX;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
66 }
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
67
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
68 static int mtv_read_header(AVFormatContext *s, AVFormatParameters *ap)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
69 {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
70 MTVDemuxContext *mtv = s->priv_data;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
71 ByteIOContext *pb = &s->pb;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
72 AVStream *st;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
73
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
74
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
75 url_fskip(pb, 3);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
76 mtv->file_size = get_le32(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
77 mtv->segments = get_le32(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
78 url_fskip(pb, 32);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
79 mtv->audio_identifier = get_le24(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
80 mtv->audio_br = get_le16(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
81 mtv->img_colorfmt = get_le24(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
82 mtv->img_bpp = get_byte(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
83 mtv->img_width = get_le16(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
84 mtv->img_height = get_le16(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
85 mtv->img_segment_size = get_le16(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
86 url_fskip(pb, 4);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
87 mtv->audio_subsegments = get_le16(pb);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
88 mtv->video_fps = (mtv->audio_br / 4) / mtv->audio_subsegments;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
89
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
90 /* FIXME Add sanity check here */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
91
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
92 /* first packet is allways audio*/
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
93
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
94 mtv->audio_packet_count = 1;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
95
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
96 /* all systems go! init decoders */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
97
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
98 /* video - raw rgb565 */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
99
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
100 st = av_new_stream(s, VIDEO_SID);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
101 if(!st)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
102 return AVERROR_NOMEM;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
103
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
104 av_set_pts_info(st, 64, 1, mtv->video_fps);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
105 st->codec->codec_type = CODEC_TYPE_VIDEO;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
106 st->codec->codec_id = CODEC_ID_RAWVIDEO;
1449
64a44155a525 now we set codec_tag, still have to figure out how to handle flipping
reynaldo
parents: 1380
diff changeset
107 st->codec->codec_tag = MKTAG('R', 'G', 'B', mtv->img_bpp);
1380
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
108 st->codec->width = mtv->img_width;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
109 st->codec->height = mtv->img_height;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
110 st->codec->bits_per_sample = mtv->img_bpp;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
111 st->codec->sample_rate = mtv->video_fps;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
112
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
113 /* audio - mp3 */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
114
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
115 st = av_new_stream(s, AUDIO_SID);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
116 if(!st)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
117 return AVERROR_NOMEM;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
118
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
119 av_set_pts_info(st, 64, 1, AUDIO_SAMPLING_RATE);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
120 st->codec->codec_type = CODEC_TYPE_AUDIO;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
121 st->codec->codec_id = CODEC_ID_MP3;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
122 st->codec->bit_rate = mtv->audio_br;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
123 st->need_parsing=1;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
124
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
125 /* Jump over header */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
126
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
127 if(url_fseek(pb, MTV_HEADER_SIZE, SEEK_SET) != MTV_HEADER_SIZE)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
128 return AVERROR_IO;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
129
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
130 return(0);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
131
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
132 }
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
133
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
134 static int mtv_read_packet(AVFormatContext *s, AVPacket *pkt)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
135 {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
136 MTVDemuxContext *mtv = s->priv_data;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
137 ByteIOContext *pb = &s->pb;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
138 int ret;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
139 #ifndef WORDS_BIGENDIAN
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
140 int i;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
141 #endif
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
142
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
143 ret = 0;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
144
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
145 if(mtv->audio_subsegments >= mtv->audio_packet_count)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
146 {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
147 url_fskip(pb, MTV_AUDIO_PADDING_SIZE);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
148
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
149 ret = av_get_packet(pb, pkt, MTV_ASUBCHUNK_DATA_SIZE);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
150 if(ret != MTV_ASUBCHUNK_DATA_SIZE)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
151 return AVERROR_IO;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
152
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
153 mtv->audio_packet_count++;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
154 pkt->stream_index = AUDIO_SID;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
155
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
156 }else
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
157 {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
158 ret = av_get_packet(pb, pkt, mtv->img_segment_size);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
159 if(ret != mtv->img_segment_size)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
160 return AVERROR_IO;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
161
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
162 #ifndef WORDS_BIGENDIAN
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
163
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
164 /* pkt->data is GGGRRRR BBBBBGGG
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
165 * and we need RRRRRGGG GGGBBBBB
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
166 * for PIX_FMT_RGB565 so here we
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
167 * just swap bytes as they come
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
168 */
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
169
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
170 for(i=0;i<mtv->img_segment_size/2;i++)
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
171 *((uint16_t *)pkt->data+i) = bswap_16(*((uint16_t *)pkt->data+i));
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
172 #endif
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
173 mtv->audio_packet_count = 1;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
174 pkt->stream_index = VIDEO_SID;
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
175 }
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
176
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
177 return(ret);
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
178 }
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
179
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
180 AVInputFormat mtv_demuxer = {
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
181 "MTV",
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
182 "MTV format",
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
183 sizeof(MTVDemuxContext),
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
184 mtv_probe,
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
185 mtv_read_header,
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
186 mtv_read_packet,
bfcb9fa1f431 new mtv demuxer
reynaldo
parents:
diff changeset
187 };