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
30 * \brief parses a packet header, extracting type and length
31 * \param pb ByteIOContext to read header from
32 * \param type detected packet type is stored here
33 * \param length detected packet length, excluding header is stored here
34 * \return 0 if header not found or contains invalid data, 1 otherwise
36 static int parse_packet_header(ByteIOContext *pb, pkt_type_t *type, int *length) {
39 if (get_byte(pb) != 1)
42 *length = get_be32(pb);
43 if ((*length >> 24) || *length < 16)
48 if (get_byte(pb) != 0xe1)
50 if (get_byte(pb) != 0xe2)
56 * \brief check if file starts with a PKT_MAP header
58 static int gxf_probe(AVProbeData *p) {
59 static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc}; // start with map packet
60 static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2};
63 if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
64 !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
65 return AVPROBE_SCORE_MAX;
70 * \brief gets the stream index for the track with the specified id, creates new
72 * \param stream id of stream to find / add
73 * \param format stream format identifier
75 static int get_sindex(AVFormatContext *s, int id, int format) {
78 for (i = 0; i < s->nb_streams; i++) {
79 if (s->streams[i]->id == id)
82 st = av_new_stream(s, id);
86 st->codec->codec_type = CODEC_TYPE_VIDEO;
87 st->codec->codec_id = CODEC_ID_MJPEG;
88 st->codec->codec_tag = MKTAG('M', 'J', 'P', 'G');
92 st->codec->codec_type = CODEC_TYPE_VIDEO;
93 st->codec->codec_id = CODEC_ID_DVVIDEO;
94 st->codec->codec_tag = MKTAG('d', 'v', 'c', ' ');
98 st->codec->codec_type = CODEC_TYPE_VIDEO;
99 st->codec->codec_id = CODEC_ID_DVVIDEO;
100 st->codec->codec_tag = MKTAG('d', 'v', 'c', 'p');
105 st->codec->codec_type = CODEC_TYPE_VIDEO;
106 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
107 st->codec->codec_tag = MKTAG('M', 'P', 'G', '2');
111 st->codec->codec_type = CODEC_TYPE_VIDEO;
112 st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
113 st->codec->codec_tag = MKTAG('M', 'P', 'G', '1');
116 st->codec->codec_type = CODEC_TYPE_AUDIO;
117 st->codec->codec_id = CODEC_ID_PCM_S24LE;
118 st->codec->codec_tag = 0x1;
119 st->codec->channels = 1;
120 st->codec->sample_rate = 48000;
121 st->codec->bit_rate = 3 * 1 * 48000 * 8;
122 st->codec->block_align = 3 * 1;
123 st->codec->bits_per_sample = 24;
126 st->codec->codec_type = CODEC_TYPE_AUDIO;
127 st->codec->codec_id = CODEC_ID_PCM_S16LE;
128 st->codec->codec_tag = 0x1;
129 st->codec->channels = 1;
130 st->codec->sample_rate = 48000;
131 st->codec->bit_rate = 2 * 1 * 48000 * 8;
132 st->codec->block_align = 2 * 1;
133 st->codec->bits_per_sample = 16;
136 st->codec->codec_type = CODEC_TYPE_AUDIO;
137 st->codec->codec_id = CODEC_ID_AC3;
138 st->codec->codec_tag = 0x2000;
139 st->codec->channels = 2;
140 st->codec->sample_rate = 48000;
143 st->codec->codec_type = CODEC_TYPE_UNKNOWN;
144 st->codec->codec_id = CODEC_ID_NONE;
147 return s->nb_streams - 1;
150 static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
151 ByteIOContext *pb = &s->pb;
155 if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
156 av_log(s, AV_LOG_ERROR, "GXF: map packet not found\n");
160 if (get_byte(pb) != 0x0e0 || get_byte(pb) != 0xff) {
161 av_log(s, AV_LOG_ERROR, "GXF: unknown version or invalid map preamble\n");
165 len = get_be16(pb); // length of material data section
167 av_log(s, AV_LOG_ERROR, "GXF: material data longer than map data\n");
173 len = get_be16(pb); // length of track description
175 av_log(s, AV_LOG_ERROR, "GXF: track description longer than map data\n");
180 int track_type, track_id, track_len;
182 track_type = get_byte(pb);
183 track_id = get_byte(pb);
184 track_len = get_be16(pb);
186 url_fskip(pb, track_len);
187 if (!(track_type & 0x80)) {
188 av_log(s, AV_LOG_ERROR, "GXF: invalid track type %x\n", track_type);
192 if ((track_id & 0xc0) != 0xc0) {
193 av_log(s, AV_LOG_ERROR, "GXF: invalid track id %x\n", track_id);
197 get_sindex(s, track_id, track_type);
200 av_log(s, AV_LOG_ERROR, "GXF: invalid track description length specified\n");
202 url_fskip(pb, map_len);
206 static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
207 ByteIOContext *pb = &s->pb;
210 while (!url_feof(pb)) {
211 int track_type, track_id, ret;
212 if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
214 av_log(s, AV_LOG_ERROR, "GXF: sync lost\n");
217 if (pkt_type != PKT_MEDIA) {
218 url_fskip(pb, pkt_len);
222 av_log(s, AV_LOG_ERROR, "GXF: invalid media packet length\n");
226 track_type = get_byte(pb);
227 track_id = get_byte(pb);
228 get_be32(pb); // field number
229 get_be32(pb); // field information
230 get_be32(pb); // "timeline" field number
231 get_byte(pb); // flags
232 get_byte(pb); // reserved
233 // NOTE: there is also data length information in the
234 // field information, it might be better to take this into account
236 ret = av_get_packet(pb, pkt, pkt_len);
237 pkt->stream_index = get_sindex(s, track_id, track_type);
243 AVInputFormat gxf_demuxer = {