gxf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 "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 = 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(AVIOContext *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 = avio_r8(pb);
  159. int tlen = avio_r8(pb);
  160. *len -= 2;
  161. if (tlen > *len)
  162. return;
  163. *len -= tlen;
  164. if (tlen == 4) {
  165. uint32_t value = avio_rb32(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. avio_skip(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 flags UMF flags to convert
  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(AVIOContext *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 = avio_r8(pb);
  205. int tlen = avio_r8(pb);
  206. *len -= 2;
  207. if (tlen > *len)
  208. return;
  209. *len -= tlen;
  210. if (tlen == 4) {
  211. uint32_t value = avio_rb32(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. avio_skip(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. AVIOContext *pb = s->pb;
  225. AVStream *st = s->streams[0];
  226. uint32_t fields_per_map = avio_rl32(pb);
  227. uint32_t map_cnt = avio_rl32(pb);
  228. int i;
  229. pkt_len -= 8;
  230. if (s->flags & AVFMT_FLAG_IGNIDX) {
  231. avio_skip(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. avio_skip(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)avio_rl32(pb) * 1024,
  247. i * (uint64_t)fields_per_map + 1, 0, 0, 0);
  248. avio_skip(pb, pkt_len);
  249. }
  250. static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
  251. AVIOContext *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 = s->priv_data;
  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 (avio_r8(pb) != 0x0e0 || avio_r8(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 = avio_rb16(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. avio_skip(pb, len);
  276. map_len -= 2;
  277. len = avio_rb16(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 = avio_r8(pb);
  289. track_id = avio_r8(pb);
  290. track_len = avio_rb16(pb);
  291. len -= track_len;
  292. gxf_track_tags(pb, &track_len, si);
  293. avio_skip(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. avio_skip(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. avio_skip(pb, 5); // preamble
  335. avio_skip(pb, 0x30); // payload description
  336. fps = fps_umf2avr(avio_rl32(pb));
  337. if (!main_timebase.num || !main_timebase.den) {
  338. av_log(s, AV_LOG_WARNING, "No FPS track tag, using UMF fps tag."
  339. " This might give wrong results.\n");
  340. // this may not always be correct, but simply the best we can get
  341. main_timebase.num = fps.den;
  342. main_timebase.den = fps.num * 2;
  343. }
  344. } else
  345. av_log(s, AV_LOG_INFO, "UMF packet too short\n");
  346. } else
  347. av_log(s, AV_LOG_INFO, "UMF packet missing\n");
  348. avio_skip(pb, len);
  349. // set a fallback value, 60000/1001 is specified for audio-only files
  350. // so use that regardless of why we do not know the video frame rate.
  351. if (!main_timebase.num || !main_timebase.den)
  352. main_timebase = (AVRational){1001, 60000};
  353. for (i = 0; i < s->nb_streams; i++) {
  354. AVStream *st = s->streams[i];
  355. av_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
  356. }
  357. return 0;
  358. }
  359. #define READ_ONE() \
  360. { \
  361. if (!max_interval-- || url_feof(pb)) \
  362. goto out; \
  363. tmp = tmp << 8 | avio_r8(pb); \
  364. }
  365. /**
  366. * @brief resync the stream on the next media packet with specified properties
  367. * @param max_interval how many bytes to search for matching packet at most
  368. * @param track track id the media packet must belong to, -1 for any
  369. * @param timestamp minimum timestamp (== field number) the packet must have, -1 for any
  370. * @return timestamp of packet found
  371. */
  372. static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
  373. uint32_t tmp;
  374. uint64_t last_pos;
  375. uint64_t last_found_pos = 0;
  376. int cur_track;
  377. int64_t cur_timestamp = AV_NOPTS_VALUE;
  378. int len;
  379. AVIOContext *pb = s->pb;
  380. GXFPktType type;
  381. tmp = avio_rb32(pb);
  382. start:
  383. while (tmp)
  384. READ_ONE();
  385. READ_ONE();
  386. if (tmp != 1)
  387. goto start;
  388. last_pos = avio_tell(pb);
  389. if (avio_seek(pb, -5, SEEK_CUR) < 0)
  390. goto out;
  391. if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
  392. if (avio_seek(pb, last_pos, SEEK_SET) < 0)
  393. goto out;
  394. goto start;
  395. }
  396. avio_r8(pb);
  397. cur_track = avio_r8(pb);
  398. cur_timestamp = avio_rb32(pb);
  399. last_found_pos = avio_tell(pb) - 16 - 6;
  400. if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
  401. if (avio_seek(pb, last_pos, SEEK_SET) >= 0)
  402. goto start;
  403. }
  404. out:
  405. if (last_found_pos)
  406. avio_seek(pb, last_found_pos, SEEK_SET);
  407. return cur_timestamp;
  408. }
  409. static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
  410. AVIOContext *pb = s->pb;
  411. GXFPktType pkt_type;
  412. int pkt_len;
  413. struct gxf_stream_info *si = s->priv_data;
  414. while (!url_feof(pb)) {
  415. AVStream *st;
  416. int track_type, track_id, ret;
  417. int field_nr, field_info, skip = 0;
  418. int stream_index;
  419. if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
  420. if (!url_feof(pb))
  421. av_log(s, AV_LOG_ERROR, "sync lost\n");
  422. return -1;
  423. }
  424. if (pkt_type == PKT_FLT) {
  425. gxf_read_index(s, pkt_len);
  426. continue;
  427. }
  428. if (pkt_type != PKT_MEDIA) {
  429. avio_skip(pb, pkt_len);
  430. continue;
  431. }
  432. if (pkt_len < 16) {
  433. av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
  434. continue;
  435. }
  436. pkt_len -= 16;
  437. track_type = avio_r8(pb);
  438. track_id = avio_r8(pb);
  439. stream_index = get_sindex(s, track_id, track_type);
  440. if (stream_index < 0)
  441. return stream_index;
  442. st = s->streams[stream_index];
  443. field_nr = avio_rb32(pb);
  444. field_info = avio_rb32(pb);
  445. avio_rb32(pb); // "timeline" field number
  446. avio_r8(pb); // flags
  447. avio_r8(pb); // reserved
  448. if (st->codec->codec_id == CODEC_ID_PCM_S24LE ||
  449. st->codec->codec_id == CODEC_ID_PCM_S16LE) {
  450. int first = field_info >> 16;
  451. int last = field_info & 0xffff; // last is exclusive
  452. int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
  453. if (first <= last && last*bps <= pkt_len) {
  454. avio_skip(pb, first*bps);
  455. skip = pkt_len - last*bps;
  456. pkt_len = (last-first)*bps;
  457. } else
  458. av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
  459. }
  460. ret = av_get_packet(pb, pkt, pkt_len);
  461. if (skip)
  462. avio_skip(pb, skip);
  463. pkt->stream_index = stream_index;
  464. pkt->dts = field_nr;
  465. //set duration manually for DV or else lavf misdetects the frame rate
  466. if (st->codec->codec_id == CODEC_ID_DVVIDEO)
  467. pkt->duration = si->fields_per_frame;
  468. return ret;
  469. }
  470. return AVERROR(EIO);
  471. }
  472. static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
  473. int res = 0;
  474. uint64_t pos;
  475. uint64_t maxlen = 100 * 1024 * 1024;
  476. AVStream *st = s->streams[0];
  477. int64_t start_time = s->streams[stream_index]->start_time;
  478. int64_t found;
  479. int idx;
  480. if (timestamp < start_time) timestamp = start_time;
  481. idx = av_index_search_timestamp(st, timestamp - start_time,
  482. AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  483. if (idx < 0)
  484. return -1;
  485. pos = st->index_entries[idx].pos;
  486. if (idx < st->nb_index_entries - 2)
  487. maxlen = st->index_entries[idx + 2].pos - pos;
  488. maxlen = FFMAX(maxlen, 200 * 1024);
  489. res = avio_seek(s->pb, pos, SEEK_SET);
  490. if (res < 0)
  491. return res;
  492. found = gxf_resync_media(s, maxlen, -1, timestamp);
  493. if (FFABS(found - timestamp) > 4)
  494. return -1;
  495. return 0;
  496. }
  497. static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
  498. int64_t *pos, int64_t pos_limit) {
  499. AVIOContext *pb = s->pb;
  500. int64_t res;
  501. if (avio_seek(pb, *pos, SEEK_SET) < 0)
  502. return AV_NOPTS_VALUE;
  503. res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
  504. *pos = avio_tell(pb);
  505. return res;
  506. }
  507. AVInputFormat ff_gxf_demuxer = {
  508. .name = "gxf",
  509. .long_name = NULL_IF_CONFIG_SMALL("GXF format"),
  510. .priv_data_size = sizeof(struct gxf_stream_info),
  511. .read_probe = gxf_probe,
  512. .read_header = gxf_header,
  513. .read_packet = gxf_packet,
  514. .read_seek = gxf_seek,
  515. .read_timestamp = gxf_read_timestamp,
  516. };