3 * Copyright (c) 2006 Reimar Doeffinger.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 * \brief parses a packet header, extracting type and length
32 * \param pb ByteIOContext to read header from
33 * \param type detected packet type is stored here
34 * \param length detected packet length, excluding header is stored here
35 * \return 0 if header not found or contains invalid data, 1 otherwise
37 static int parse_packet_header(ByteIOContext *pb, pkt_type_t *type, int *length) {
40 if (get_byte(pb) != 1)
43 *length = get_be32(pb);
44 if ((*length >> 24) || *length < 16)
49 if (get_byte(pb) != 0xe1)
51 if (get_byte(pb) != 0xe2)
57 * \brief check if file starts with a PKT_MAP header
59 static int gxf_probe(AVProbeData *p) {
60 static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc}; // start with map packet
61 static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2};
64 if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
65 !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
66 return AVPROBE_SCORE_MAX;
71 * \brief gets the stream index for the track with the specified id, creates new
73 * \param stream id of stream to find / add
74 * \param format stream format identifier
76 static int get_sindex(AVFormatContext *s, int id, int format) {
79 for (i = 0; i < s->nb_streams; i++) {
80 if (s->streams[i]->id == id)
83 st = av_new_stream(s, id);
87 st->codec->codec_type = CODEC_TYPE_VIDEO;
88 st->codec->codec_id = CODEC_ID_MJPEG;
89 st->codec->codec_tag = MKTAG('M', 'J', 'P', 'G');
93 st->codec->codec_type = CODEC_TYPE_VIDEO;
94 st->codec->codec_id = CODEC_ID_DVVIDEO;
95 st->codec->codec_tag = MKTAG('d', 'v', 'c', ' ');
99 st->codec->codec_type = CODEC_TYPE_VIDEO;
100 st->codec->codec_id = CODEC_ID_DVVIDEO;
101 st->codec->codec_tag = MKTAG('d', 'v', 'c', 'p');
106 st->codec->codec_type = CODEC_TYPE_VIDEO;
107 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
108 st->codec->codec_tag = MKTAG('M', 'P', 'G', '2');
112 st->codec->codec_type = CODEC_TYPE_VIDEO;
113 st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
114 st->codec->codec_tag = MKTAG('M', 'P', 'G', '1');
117 st->codec->codec_type = CODEC_TYPE_AUDIO;
118 st->codec->codec_id = CODEC_ID_PCM_S24LE;
119 st->codec->codec_tag = 0x1;
120 st->codec->channels = 1;
121 st->codec->sample_rate = 48000;
122 st->codec->bit_rate = 3 * 1 * 48000 * 8;
123 st->codec->block_align = 3 * 1;
124 st->codec->bits_per_sample = 24;
127 st->codec->codec_type = CODEC_TYPE_AUDIO;
128 st->codec->codec_id = CODEC_ID_PCM_S16LE;
129 st->codec->codec_tag = 0x1;
130 st->codec->channels = 1;
131 st->codec->sample_rate = 48000;
132 st->codec->bit_rate = 2 * 1 * 48000 * 8;
133 st->codec->block_align = 2 * 1;
134 st->codec->bits_per_sample = 16;
137 st->codec->codec_type = CODEC_TYPE_AUDIO;
138 st->codec->codec_id = CODEC_ID_AC3;
139 st->codec->codec_tag = 0x2000;
140 st->codec->channels = 2;
141 st->codec->sample_rate = 48000;
144 st->codec->codec_type = CODEC_TYPE_UNKNOWN;
145 st->codec->codec_id = CODEC_ID_NONE;
148 return s->nb_streams - 1;
151 static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
152 ByteIOContext *pb = &s->pb;
156 if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
157 av_log(s, AV_LOG_ERROR, "GXF: map packet not found\n");
161 if (get_byte(pb) != 0x0e0 || get_byte(pb) != 0xff) {
162 av_log(s, AV_LOG_ERROR, "GXF: unknown version or invalid map preamble\n");
166 len = get_be16(pb); // length of material data section
168 av_log(s, AV_LOG_ERROR, "GXF: material data longer than map data\n");
174 len = get_be16(pb); // length of track description
176 av_log(s, AV_LOG_ERROR, "GXF: track description longer than map data\n");
181 int track_type, track_id, track_len;
183 track_type = get_byte(pb);
184 track_id = get_byte(pb);
185 track_len = get_be16(pb);
187 url_fskip(pb, track_len);
188 if (!(track_type & 0x80)) {
189 av_log(s, AV_LOG_ERROR, "GXF: invalid track type %x\n", track_type);
193 if ((track_id & 0xc0) != 0xc0) {
194 av_log(s, AV_LOG_ERROR, "GXF: invalid track id %x\n", track_id);
198 get_sindex(s, track_id, track_type);
201 av_log(s, AV_LOG_ERROR, "GXF: invalid track description length specified\n");
203 url_fskip(pb, map_len);
207 static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
208 ByteIOContext *pb = &s->pb;
211 while (!url_feof(pb)) {
212 int track_type, track_id, ret;
213 if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
215 av_log(s, AV_LOG_ERROR, "GXF: sync lost\n");
218 if (pkt_type != PKT_MEDIA) {
219 url_fskip(pb, pkt_len);
223 av_log(s, AV_LOG_ERROR, "GXF: invalid media packet length\n");
227 track_type = get_byte(pb);
228 track_id = get_byte(pb);
229 get_be32(pb); // field number
230 get_be32(pb); // field information
231 get_be32(pb); // "timeline" field number
232 get_byte(pb); // flags
233 get_byte(pb); // reserved
234 // NOTE: there is also data length information in the
235 // field information, it might be better to take this int account
237 ret = av_get_packet(pb, pkt, pkt_len);
238 pkt->stream_index = get_sindex(s, track_id, track_type);
244 static AVInputFormat gxf_iformat = {
256 av_register_input_format(&gxf_iformat);