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.

552 lines
18KB

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