You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

523 lines
17KB

  1. /*
  2. * GXF demuxer.
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "avformat.h"
  23. #include "gxf.h"
  24. struct gxf_stream_info {
  25. int64_t first_field;
  26. int64_t last_field;
  27. AVRational frames_per_second;
  28. int32_t fields_per_frame;
  29. };
  30. /**
  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
  36. */
  37. static int parse_packet_header(ByteIOContext *pb, GXFPktType *type, int *length) {
  38. if (get_be32(pb))
  39. return 0;
  40. if (get_byte(pb) != 1)
  41. return 0;
  42. *type = get_byte(pb);
  43. *length = get_be32(pb);
  44. if ((*length >> 24) || *length < 16)
  45. return 0;
  46. *length -= 16;
  47. if (get_be32(pb))
  48. return 0;
  49. if (get_byte(pb) != 0xe1)
  50. return 0;
  51. if (get_byte(pb) != 0xe2)
  52. return 0;
  53. return 1;
  54. }
  55. /**
  56. * \brief check if file starts with a PKT_MAP header
  57. */
  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};
  61. if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
  62. !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
  63. return AVPROBE_SCORE_MAX;
  64. return 0;
  65. }
  66. /**
  67. * \brief gets the stream index for the track with the specified id, creates new
  68. * stream if not found
  69. * \param stream id of stream to find / add
  70. * \param format stream format identifier
  71. */
  72. static int get_sindex(AVFormatContext *s, int id, int format) {
  73. int i;
  74. AVStream *st = NULL;
  75. for (i = 0; i < s->nb_streams; i++) {
  76. if (s->streams[i]->id == id)
  77. return i;
  78. }
  79. st = av_new_stream(s, id);
  80. if (!st)
  81. return AVERROR(ENOMEM);
  82. switch (format) {
  83. case 3:
  84. case 4:
  85. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  86. st->codec->codec_id = CODEC_ID_MJPEG;
  87. break;
  88. case 13:
  89. case 15:
  90. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  91. st->codec->codec_id = CODEC_ID_DVVIDEO;
  92. break;
  93. case 14:
  94. case 16:
  95. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  96. st->codec->codec_id = CODEC_ID_DVVIDEO;
  97. break;
  98. case 11:
  99. case 12:
  100. case 20:
  101. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  102. st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
  103. st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
  104. break;
  105. case 22:
  106. case 23:
  107. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  108. st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
  109. st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
  110. break;
  111. case 9:
  112. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  113. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  114. st->codec->channels = 1;
  115. st->codec->sample_rate = 48000;
  116. st->codec->bit_rate = 3 * 1 * 48000 * 8;
  117. st->codec->block_align = 3 * 1;
  118. st->codec->bits_per_coded_sample = 24;
  119. break;
  120. case 10:
  121. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  122. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  123. st->codec->channels = 1;
  124. st->codec->sample_rate = 48000;
  125. st->codec->bit_rate = 2 * 1 * 48000 * 8;
  126. st->codec->block_align = 2 * 1;
  127. st->codec->bits_per_coded_sample = 16;
  128. break;
  129. case 17:
  130. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  131. st->codec->codec_id = CODEC_ID_AC3;
  132. st->codec->channels = 2;
  133. st->codec->sample_rate = 48000;
  134. break;
  135. // timecode tracks:
  136. case 7:
  137. case 8:
  138. case 24:
  139. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  140. st->codec->codec_id = CODEC_ID_NONE;
  141. break;
  142. default:
  143. st->codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
  144. st->codec->codec_id = CODEC_ID_NONE;
  145. break;
  146. }
  147. return s->nb_streams - 1;
  148. }
  149. /**
  150. * \brief filters out interesting tags from material information.
  151. * \param len length of tag section, will be adjusted to contain remaining bytes
  152. * \param si struct to store collected information into
  153. */
  154. static void gxf_material_tags(ByteIOContext *pb, int *len, struct gxf_stream_info *si) {
  155. si->first_field = AV_NOPTS_VALUE;
  156. si->last_field = AV_NOPTS_VALUE;
  157. while (*len >= 2) {
  158. GXFMatTag tag = get_byte(pb);
  159. int tlen = get_byte(pb);
  160. *len -= 2;
  161. if (tlen > *len)
  162. return;
  163. *len -= tlen;
  164. if (tlen == 4) {
  165. uint32_t value = get_be32(pb);
  166. if (tag == MAT_FIRST_FIELD)
  167. si->first_field = value;
  168. else if (tag == MAT_LAST_FIELD)
  169. si->last_field = value;
  170. } else
  171. url_fskip(pb, tlen);
  172. }
  173. }
  174. /**
  175. * \brief convert fps tag value to AVRational fps
  176. * \param fps fps value from tag
  177. * \return fps as AVRational, or 0 / 0 if unknown
  178. */
  179. static AVRational fps_tag2avr(int32_t fps) {
  180. extern const AVRational ff_frame_rate_tab[];
  181. if (fps < 1 || fps > 9) fps = 9;
  182. return ff_frame_rate_tab[9 - fps]; // values have opposite order
  183. }
  184. /**
  185. * \brief convert UMF attributes flags to AVRational fps
  186. * \param fps fps value from flags
  187. * \return fps as AVRational, or 0 / 0 if unknown
  188. */
  189. static AVRational fps_umf2avr(uint32_t flags) {
  190. static const AVRational map[] = {{50, 1}, {60000, 1001}, {24, 1},
  191. {25, 1}, {30000, 1001}};
  192. int idx = av_log2((flags & 0x7c0) >> 6);
  193. return map[idx];
  194. }
  195. /**
  196. * \brief filters out interesting tags from track information.
  197. * \param len length of tag section, will be adjusted to contain remaining bytes
  198. * \param si struct to store collected information into
  199. */
  200. static void gxf_track_tags(ByteIOContext *pb, int *len, struct gxf_stream_info *si) {
  201. si->frames_per_second = (AVRational){0, 0};
  202. si->fields_per_frame = 0;
  203. while (*len >= 2) {
  204. GXFTrackTag tag = get_byte(pb);
  205. int tlen = get_byte(pb);
  206. *len -= 2;
  207. if (tlen > *len)
  208. return;
  209. *len -= tlen;
  210. if (tlen == 4) {
  211. uint32_t value = get_be32(pb);
  212. if (tag == TRACK_FPS)
  213. si->frames_per_second = fps_tag2avr(value);
  214. else if (tag == TRACK_FPF && (value == 1 || value == 2))
  215. si->fields_per_frame = value;
  216. } else
  217. url_fskip(pb, tlen);
  218. }
  219. }
  220. /**
  221. * \brief read index from FLT packet into stream 0 av_index
  222. */
  223. static void gxf_read_index(AVFormatContext *s, int pkt_len) {
  224. ByteIOContext *pb = s->pb;
  225. AVStream *st = s->streams[0];
  226. uint32_t fields_per_map = get_le32(pb);
  227. uint32_t map_cnt = get_le32(pb);
  228. int i;
  229. pkt_len -= 8;
  230. if (s->flags & AVFMT_FLAG_IGNIDX) {
  231. url_fskip(pb, pkt_len);
  232. return;
  233. }
  234. if (map_cnt > 1000) {
  235. av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt);
  236. map_cnt = 1000;
  237. }
  238. if (pkt_len < 4 * map_cnt) {
  239. av_log(s, AV_LOG_ERROR, "invalid index length\n");
  240. url_fskip(pb, pkt_len);
  241. return;
  242. }
  243. pkt_len -= 4 * map_cnt;
  244. av_add_index_entry(st, 0, 0, 0, 0, 0);
  245. for (i = 0; i < map_cnt; i++)
  246. av_add_index_entry(st, (uint64_t)get_le32(pb) * 1024,
  247. i * (uint64_t)fields_per_map + 1, 0, 0, 0);
  248. url_fskip(pb, pkt_len);
  249. }
  250. static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
  251. ByteIOContext *pb = s->pb;
  252. GXFPktType pkt_type;
  253. int map_len;
  254. int len;
  255. AVRational main_timebase = {0, 0};
  256. struct gxf_stream_info si;
  257. int i;
  258. if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
  259. av_log(s, AV_LOG_ERROR, "map packet not found\n");
  260. return 0;
  261. }
  262. map_len -= 2;
  263. if (get_byte(pb) != 0x0e0 || get_byte(pb) != 0xff) {
  264. av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n");
  265. return 0;
  266. }
  267. map_len -= 2;
  268. len = get_be16(pb); // length of material data section
  269. if (len > map_len) {
  270. av_log(s, AV_LOG_ERROR, "material data longer than map data\n");
  271. return 0;
  272. }
  273. map_len -= len;
  274. gxf_material_tags(pb, &len, &si);
  275. url_fskip(pb, len);
  276. map_len -= 2;
  277. len = get_be16(pb); // length of track description
  278. if (len > map_len) {
  279. av_log(s, AV_LOG_ERROR, "track description longer than map data\n");
  280. return 0;
  281. }
  282. map_len -= len;
  283. while (len > 0) {
  284. int track_type, track_id, track_len;
  285. AVStream *st;
  286. int idx;
  287. len -= 4;
  288. track_type = get_byte(pb);
  289. track_id = get_byte(pb);
  290. track_len = get_be16(pb);
  291. len -= track_len;
  292. gxf_track_tags(pb, &track_len, &si);
  293. url_fskip(pb, track_len);
  294. if (!(track_type & 0x80)) {
  295. av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type);
  296. continue;
  297. }
  298. track_type &= 0x7f;
  299. if ((track_id & 0xc0) != 0xc0) {
  300. av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id);
  301. continue;
  302. }
  303. track_id &= 0x3f;
  304. idx = get_sindex(s, track_id, track_type);
  305. if (idx < 0) continue;
  306. st = s->streams[idx];
  307. if (!main_timebase.num || !main_timebase.den) {
  308. main_timebase.num = si.frames_per_second.den;
  309. main_timebase.den = si.frames_per_second.num * 2;
  310. }
  311. st->start_time = si.first_field;
  312. if (si.first_field != AV_NOPTS_VALUE && si.last_field != AV_NOPTS_VALUE)
  313. st->duration = si.last_field - si.first_field;
  314. }
  315. if (len < 0)
  316. av_log(s, AV_LOG_ERROR, "invalid track description length specified\n");
  317. if (map_len)
  318. url_fskip(pb, map_len);
  319. if (!parse_packet_header(pb, &pkt_type, &len)) {
  320. av_log(s, AV_LOG_ERROR, "sync lost in header\n");
  321. return -1;
  322. }
  323. if (pkt_type == PKT_FLT) {
  324. gxf_read_index(s, len);
  325. if (!parse_packet_header(pb, &pkt_type, &len)) {
  326. av_log(s, AV_LOG_ERROR, "sync lost in header\n");
  327. return -1;
  328. }
  329. }
  330. if (pkt_type == PKT_UMF) {
  331. if (len >= 0x39) {
  332. AVRational fps;
  333. len -= 0x39;
  334. url_fskip(pb, 5); // preamble
  335. url_fskip(pb, 0x30); // payload description
  336. fps = fps_umf2avr(get_le32(pb));
  337. if (!main_timebase.num || !main_timebase.den) {
  338. // this may not always be correct, but simply the best we can get
  339. main_timebase.num = fps.den;
  340. main_timebase.den = fps.num * 2;
  341. }
  342. } else
  343. av_log(s, AV_LOG_INFO, "UMF packet too short\n");
  344. } else
  345. av_log(s, AV_LOG_INFO, "UMF packet missing\n");
  346. url_fskip(pb, len);
  347. // set a fallback value, 60000/1001 is specified for audio-only files
  348. // so use that regardless of why we do not know the video frame rate.
  349. if (!main_timebase.num || !main_timebase.den)
  350. main_timebase = (AVRational){1001, 60000};
  351. for (i = 0; i < s->nb_streams; i++) {
  352. AVStream *st = s->streams[i];
  353. av_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
  354. }
  355. return 0;
  356. }
  357. #define READ_ONE() \
  358. { \
  359. if (!max_interval-- || url_feof(pb)) \
  360. goto out; \
  361. tmp = tmp << 8 | get_byte(pb); \
  362. }
  363. /**
  364. * \brief resync the stream on the next media packet with specified properties
  365. * \param max_interval how many bytes to search for matching packet at most
  366. * \param track track id the media packet must belong to, -1 for any
  367. * \param timestamp minimum timestamp (== field number) the packet must have, -1 for any
  368. * \return timestamp of packet found
  369. */
  370. static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
  371. uint32_t tmp;
  372. uint64_t last_pos;
  373. uint64_t last_found_pos = 0;
  374. int cur_track;
  375. int64_t cur_timestamp = AV_NOPTS_VALUE;
  376. int len;
  377. ByteIOContext *pb = s->pb;
  378. GXFPktType type;
  379. tmp = get_be32(pb);
  380. start:
  381. while (tmp)
  382. READ_ONE();
  383. READ_ONE();
  384. if (tmp != 1)
  385. goto start;
  386. last_pos = url_ftell(pb);
  387. url_fseek(pb, -5, SEEK_CUR);
  388. if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
  389. url_fseek(pb, last_pos, SEEK_SET);
  390. goto start;
  391. }
  392. get_byte(pb);
  393. cur_track = get_byte(pb);
  394. cur_timestamp = get_be32(pb);
  395. last_found_pos = url_ftell(pb) - 16 - 6;
  396. if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
  397. url_fseek(pb, last_pos, SEEK_SET);
  398. goto start;
  399. }
  400. out:
  401. if (last_found_pos)
  402. url_fseek(pb, last_found_pos, SEEK_SET);
  403. return cur_timestamp;
  404. }
  405. static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
  406. ByteIOContext *pb = s->pb;
  407. GXFPktType pkt_type;
  408. int pkt_len;
  409. while (!url_feof(pb)) {
  410. AVStream *st;
  411. int track_type, track_id, ret;
  412. int field_nr, field_info, skip = 0;
  413. int stream_index;
  414. if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
  415. if (!url_feof(pb))
  416. av_log(s, AV_LOG_ERROR, "sync lost\n");
  417. return -1;
  418. }
  419. if (pkt_type == PKT_FLT) {
  420. gxf_read_index(s, pkt_len);
  421. continue;
  422. }
  423. if (pkt_type != PKT_MEDIA) {
  424. url_fskip(pb, pkt_len);
  425. continue;
  426. }
  427. if (pkt_len < 16) {
  428. av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
  429. continue;
  430. }
  431. pkt_len -= 16;
  432. track_type = get_byte(pb);
  433. track_id = get_byte(pb);
  434. stream_index = get_sindex(s, track_id, track_type);
  435. if (stream_index < 0)
  436. return stream_index;
  437. st = s->streams[stream_index];
  438. field_nr = get_be32(pb);
  439. field_info = get_be32(pb);
  440. get_be32(pb); // "timeline" field number
  441. get_byte(pb); // flags
  442. get_byte(pb); // reserved
  443. if (st->codec->codec_id == CODEC_ID_PCM_S24LE ||
  444. st->codec->codec_id == CODEC_ID_PCM_S16LE) {
  445. int first = field_info >> 16;
  446. int last = field_info & 0xffff; // last is exclusive
  447. int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
  448. if (first <= last && last*bps <= pkt_len) {
  449. url_fskip(pb, first*bps);
  450. skip = pkt_len - last*bps;
  451. pkt_len = (last-first)*bps;
  452. } else
  453. av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
  454. }
  455. ret = av_get_packet(pb, pkt, pkt_len);
  456. if (skip)
  457. url_fskip(pb, skip);
  458. pkt->stream_index = stream_index;
  459. pkt->dts = field_nr;
  460. return ret;
  461. }
  462. return AVERROR(EIO);
  463. }
  464. static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
  465. uint64_t pos;
  466. uint64_t maxlen = 100 * 1024 * 1024;
  467. AVStream *st = s->streams[0];
  468. int64_t start_time = s->streams[stream_index]->start_time;
  469. int64_t found;
  470. int idx;
  471. if (timestamp < start_time) timestamp = start_time;
  472. idx = av_index_search_timestamp(st, timestamp - start_time,
  473. AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  474. if (idx < 0)
  475. return -1;
  476. pos = st->index_entries[idx].pos;
  477. if (idx < st->nb_index_entries - 2)
  478. maxlen = st->index_entries[idx + 2].pos - pos;
  479. maxlen = FFMAX(maxlen, 200 * 1024);
  480. url_fseek(s->pb, pos, SEEK_SET);
  481. found = gxf_resync_media(s, maxlen, -1, timestamp);
  482. if (FFABS(found - timestamp) > 4)
  483. return -1;
  484. return 0;
  485. }
  486. static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
  487. int64_t *pos, int64_t pos_limit) {
  488. ByteIOContext *pb = s->pb;
  489. int64_t res;
  490. url_fseek(pb, *pos, SEEK_SET);
  491. res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
  492. *pos = url_ftell(pb);
  493. return res;
  494. }
  495. AVInputFormat gxf_demuxer = {
  496. "gxf",
  497. NULL_IF_CONFIG_SMALL("GXF format"),
  498. 0,
  499. gxf_probe,
  500. gxf_header,
  501. gxf_packet,
  502. NULL,
  503. gxf_seek,
  504. gxf_read_timestamp,
  505. };