gxf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 "gxf.h"
  24. struct gxf_stream_info {
  25. int64_t first_field;
  26. int64_t last_field;
  27. AVRational frames_per_second;
  28. int32_t fields_per_frame;
  29. };
  30. /**
  31. * \brief parses a packet header, extracting type and length
  32. * \param pb ByteIOContext to read header from
  33. * \param type detected packet type is stored here
  34. * \param length detected packet length, excluding header is stored here
  35. * \return 0 if header not found or contains invalid data, 1 otherwise
  36. */
  37. static int parse_packet_header(ByteIOContext *pb, GXFPktType *type, int *length) {
  38. if (get_be32(pb))
  39. return 0;
  40. if (get_byte(pb) != 1)
  41. return 0;
  42. *type = get_byte(pb);
  43. *length = get_be32(pb);
  44. if ((*length >> 24) || *length < 16)
  45. return 0;
  46. *length -= 16;
  47. if (get_be32(pb))
  48. return 0;
  49. if (get_byte(pb) != 0xe1)
  50. return 0;
  51. if (get_byte(pb) != 0xe2)
  52. return 0;
  53. return 1;
  54. }
  55. /**
  56. * \brief check if file starts with a PKT_MAP header
  57. */
  58. static int gxf_probe(AVProbeData *p) {
  59. static const uint8_t startcode[] = {0, 0, 0, 0, 1, 0xbc}; // start with map packet
  60. static const uint8_t endcode[] = {0, 0, 0, 0, 0xe1, 0xe2};
  61. if (!memcmp(p->buf, startcode, sizeof(startcode)) &&
  62. !memcmp(&p->buf[16 - sizeof(endcode)], endcode, sizeof(endcode)))
  63. return AVPROBE_SCORE_MAX;
  64. return 0;
  65. }
  66. /**
  67. * \brief gets the stream index for the track with the specified id, creates new
  68. * stream if not found
  69. * \param stream id of stream to find / add
  70. * \param format stream format identifier
  71. */
  72. static int get_sindex(AVFormatContext *s, int id, int format) {
  73. int i;
  74. AVStream *st = NULL;
  75. for (i = 0; i < s->nb_streams; i++) {
  76. if (s->streams[i]->id == id)
  77. return i;
  78. }
  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 = CODEC_TYPE_VIDEO;
  86. st->codec->codec_id = CODEC_ID_MJPEG;
  87. break;
  88. case 13:
  89. case 15:
  90. st->codec->codec_type = CODEC_TYPE_VIDEO;
  91. st->codec->codec_id = CODEC_ID_DVVIDEO;
  92. break;
  93. case 14:
  94. case 16:
  95. st->codec->codec_type = CODEC_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 = CODEC_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 = CODEC_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 = CODEC_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 = CODEC_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 = CODEC_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 = CODEC_TYPE_DATA;
  140. st->codec->codec_id = CODEC_ID_NONE;
  141. break;
  142. default:
  143. st->codec->codec_type = CODEC_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(ByteIOContext *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 = get_byte(pb);
  159. int tlen = get_byte(pb);
  160. *len -= 2;
  161. if (tlen > *len)
  162. return;
  163. *len -= tlen;
  164. if (tlen == 4) {
  165. uint32_t value = get_be32(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. url_fskip(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 fps fps value from flags
  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(ByteIOContext *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 = get_byte(pb);
  205. int tlen = get_byte(pb);
  206. *len -= 2;
  207. if (tlen > *len)
  208. return;
  209. *len -= tlen;
  210. if (tlen == 4) {
  211. uint32_t value = get_be32(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. url_fskip(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. ByteIOContext *pb = s->pb;
  225. AVStream *st = s->streams[0];
  226. uint32_t fields_per_map = get_le32(pb);
  227. uint32_t map_cnt = get_le32(pb);
  228. int i;
  229. pkt_len -= 8;
  230. if (map_cnt > 1000) {
  231. av_log(s, AV_LOG_ERROR, "too many index entries %u (%x)\n", map_cnt, map_cnt);
  232. map_cnt = 1000;
  233. }
  234. if (pkt_len < 4 * map_cnt) {
  235. av_log(s, AV_LOG_ERROR, "invalid index length\n");
  236. url_fskip(pb, pkt_len);
  237. return;
  238. }
  239. pkt_len -= 4 * map_cnt;
  240. av_add_index_entry(st, 0, 0, 0, 0, 0);
  241. for (i = 0; i < map_cnt; i++)
  242. av_add_index_entry(st, (uint64_t)get_le32(pb) * 1024,
  243. i * (uint64_t)fields_per_map + 1, 0, 0, 0);
  244. url_fskip(pb, pkt_len);
  245. }
  246. static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
  247. ByteIOContext *pb = s->pb;
  248. GXFPktType pkt_type;
  249. int map_len;
  250. int len;
  251. AVRational main_timebase = {0, 0};
  252. struct gxf_stream_info si;
  253. int i;
  254. if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
  255. av_log(s, AV_LOG_ERROR, "map packet not found\n");
  256. return 0;
  257. }
  258. map_len -= 2;
  259. if (get_byte(pb) != 0x0e0 || get_byte(pb) != 0xff) {
  260. av_log(s, AV_LOG_ERROR, "unknown version or invalid map preamble\n");
  261. return 0;
  262. }
  263. map_len -= 2;
  264. len = get_be16(pb); // length of material data section
  265. if (len > map_len) {
  266. av_log(s, AV_LOG_ERROR, "material data longer than map data\n");
  267. return 0;
  268. }
  269. map_len -= len;
  270. gxf_material_tags(pb, &len, &si);
  271. url_fskip(pb, len);
  272. map_len -= 2;
  273. len = get_be16(pb); // length of track description
  274. if (len > map_len) {
  275. av_log(s, AV_LOG_ERROR, "track description longer than map data\n");
  276. return 0;
  277. }
  278. map_len -= len;
  279. while (len > 0) {
  280. int track_type, track_id, track_len;
  281. AVStream *st;
  282. int idx;
  283. len -= 4;
  284. track_type = get_byte(pb);
  285. track_id = get_byte(pb);
  286. track_len = get_be16(pb);
  287. len -= track_len;
  288. gxf_track_tags(pb, &track_len, &si);
  289. url_fskip(pb, track_len);
  290. if (!(track_type & 0x80)) {
  291. av_log(s, AV_LOG_ERROR, "invalid track type %x\n", track_type);
  292. continue;
  293. }
  294. track_type &= 0x7f;
  295. if ((track_id & 0xc0) != 0xc0) {
  296. av_log(s, AV_LOG_ERROR, "invalid track id %x\n", track_id);
  297. continue;
  298. }
  299. track_id &= 0x3f;
  300. idx = get_sindex(s, track_id, track_type);
  301. if (idx < 0) continue;
  302. st = s->streams[idx];
  303. if (!main_timebase.num || !main_timebase.den) {
  304. main_timebase.num = si.frames_per_second.den;
  305. main_timebase.den = si.frames_per_second.num * si.fields_per_frame;
  306. }
  307. st->start_time = si.first_field;
  308. if (si.first_field != AV_NOPTS_VALUE && si.last_field != AV_NOPTS_VALUE)
  309. st->duration = si.last_field - si.first_field;
  310. }
  311. if (len < 0)
  312. av_log(s, AV_LOG_ERROR, "invalid track description length specified\n");
  313. if (map_len)
  314. url_fskip(pb, map_len);
  315. if (!parse_packet_header(pb, &pkt_type, &len)) {
  316. av_log(s, AV_LOG_ERROR, "sync lost in header\n");
  317. return -1;
  318. }
  319. if (pkt_type == PKT_FLT) {
  320. gxf_read_index(s, len);
  321. if (!parse_packet_header(pb, &pkt_type, &len)) {
  322. av_log(s, AV_LOG_ERROR, "sync lost in header\n");
  323. return -1;
  324. }
  325. }
  326. if (pkt_type == PKT_UMF) {
  327. if (len >= 0x39) {
  328. AVRational fps;
  329. len -= 0x39;
  330. url_fskip(pb, 5); // preamble
  331. url_fskip(pb, 0x30); // payload description
  332. fps = fps_umf2avr(get_le32(pb));
  333. if (!main_timebase.num || !main_timebase.den) {
  334. // this may not always be correct, but simply the best we can get
  335. main_timebase.num = fps.den;
  336. main_timebase.den = fps.num;
  337. }
  338. } else
  339. av_log(s, AV_LOG_INFO, "UMF packet too short\n");
  340. } else
  341. av_log(s, AV_LOG_INFO, "UMF packet missing\n");
  342. url_fskip(pb, len);
  343. if (!main_timebase.num || !main_timebase.den)
  344. main_timebase = (AVRational){1, 50}; // set some arbitrary fallback
  345. for (i = 0; i < s->nb_streams; i++) {
  346. AVStream *st = s->streams[i];
  347. av_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
  348. }
  349. return 0;
  350. }
  351. #define READ_ONE() \
  352. { \
  353. if (!max_interval-- || url_feof(pb)) \
  354. goto out; \
  355. tmp = tmp << 8 | get_byte(pb); \
  356. }
  357. /**
  358. * \brief resync the stream on the next media packet with specified properties
  359. * \param max_interval how many bytes to search for matching packet at most
  360. * \param track track id the media packet must belong to, -1 for any
  361. * \param timestamp minimum timestamp (== field number) the packet must have, -1 for any
  362. * \return timestamp of packet found
  363. */
  364. static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
  365. uint32_t tmp;
  366. uint64_t last_pos;
  367. uint64_t last_found_pos = 0;
  368. int cur_track;
  369. int64_t cur_timestamp = AV_NOPTS_VALUE;
  370. int len;
  371. ByteIOContext *pb = s->pb;
  372. GXFPktType type;
  373. tmp = get_be32(pb);
  374. start:
  375. while (tmp)
  376. READ_ONE();
  377. READ_ONE();
  378. if (tmp != 1)
  379. goto start;
  380. last_pos = url_ftell(pb);
  381. url_fseek(pb, -5, SEEK_CUR);
  382. if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
  383. url_fseek(pb, last_pos, SEEK_SET);
  384. goto start;
  385. }
  386. get_byte(pb);
  387. cur_track = get_byte(pb);
  388. cur_timestamp = get_be32(pb);
  389. last_found_pos = url_ftell(pb) - 16 - 6;
  390. if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
  391. url_fseek(pb, last_pos, SEEK_SET);
  392. goto start;
  393. }
  394. out:
  395. if (last_found_pos)
  396. url_fseek(pb, last_found_pos, SEEK_SET);
  397. return cur_timestamp;
  398. }
  399. static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
  400. ByteIOContext *pb = s->pb;
  401. GXFPktType pkt_type;
  402. int pkt_len;
  403. while (!url_feof(pb)) {
  404. AVStream *st;
  405. int track_type, track_id, ret;
  406. int field_nr, field_info, skip = 0;
  407. int stream_index;
  408. if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
  409. if (!url_feof(pb))
  410. av_log(s, AV_LOG_ERROR, "sync lost\n");
  411. return -1;
  412. }
  413. if (pkt_type == PKT_FLT) {
  414. gxf_read_index(s, pkt_len);
  415. continue;
  416. }
  417. if (pkt_type != PKT_MEDIA) {
  418. url_fskip(pb, pkt_len);
  419. continue;
  420. }
  421. if (pkt_len < 16) {
  422. av_log(s, AV_LOG_ERROR, "invalid media packet length\n");
  423. continue;
  424. }
  425. pkt_len -= 16;
  426. track_type = get_byte(pb);
  427. track_id = get_byte(pb);
  428. stream_index = get_sindex(s, track_id, track_type);
  429. if (stream_index < 0)
  430. return stream_index;
  431. st = s->streams[stream_index];
  432. field_nr = get_be32(pb);
  433. field_info = get_be32(pb);
  434. get_be32(pb); // "timeline" field number
  435. get_byte(pb); // flags
  436. get_byte(pb); // reserved
  437. if (st->codec->codec_id == CODEC_ID_PCM_S24LE ||
  438. st->codec->codec_id == CODEC_ID_PCM_S16LE) {
  439. int first = field_info >> 16;
  440. int last = field_info & 0xffff; // last is exclusive
  441. int bps = av_get_bits_per_sample(st->codec->codec_id)>>3;
  442. if (first <= last && last*bps <= pkt_len) {
  443. url_fskip(pb, first*bps);
  444. skip = pkt_len - last*bps;
  445. pkt_len = (last-first)*bps;
  446. } else
  447. av_log(s, AV_LOG_ERROR, "invalid first and last sample values\n");
  448. }
  449. ret = av_get_packet(pb, pkt, pkt_len);
  450. if (skip)
  451. url_fskip(pb, skip);
  452. pkt->stream_index = stream_index;
  453. pkt->dts = field_nr;
  454. return ret;
  455. }
  456. return AVERROR(EIO);
  457. }
  458. static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
  459. uint64_t pos;
  460. uint64_t maxlen = 100 * 1024 * 1024;
  461. AVStream *st = s->streams[0];
  462. int64_t start_time = s->streams[stream_index]->start_time;
  463. int64_t found;
  464. int idx;
  465. if (timestamp < start_time) timestamp = start_time;
  466. idx = av_index_search_timestamp(st, timestamp - start_time,
  467. AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  468. if (idx < 0)
  469. return -1;
  470. pos = st->index_entries[idx].pos;
  471. if (idx < st->nb_index_entries - 2)
  472. maxlen = st->index_entries[idx + 2].pos - pos;
  473. maxlen = FFMAX(maxlen, 200 * 1024);
  474. url_fseek(s->pb, pos, SEEK_SET);
  475. found = gxf_resync_media(s, maxlen, -1, timestamp);
  476. if (FFABS(found - timestamp) > 4)
  477. return -1;
  478. return 0;
  479. }
  480. static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
  481. int64_t *pos, int64_t pos_limit) {
  482. ByteIOContext *pb = s->pb;
  483. int64_t res;
  484. url_fseek(pb, *pos, SEEK_SET);
  485. res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
  486. *pos = url_ftell(pb);
  487. return res;
  488. }
  489. AVInputFormat gxf_demuxer = {
  490. "gxf",
  491. NULL_IF_CONFIG_SMALL("GXF format"),
  492. 0,
  493. gxf_probe,
  494. gxf_header,
  495. gxf_packet,
  496. NULL,
  497. gxf_seek,
  498. gxf_read_timestamp,
  499. };