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.

597 lines
20KB

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