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.

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