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.

536 lines
17KB

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