gxf.c 17 KB

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