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.

497 lines
16KB

  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 "avformat.h"
  22. #include "common.h"
  23. #include "gxf.h"
  24. typedef struct {
  25. int64_t first_field;
  26. int64_t last_field;
  27. AVRational frames_per_second;
  28. int32_t fields_per_frame;
  29. } st_info_t;
  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, pkt_type_t *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. switch (format) {
  81. case 3:
  82. case 4:
  83. st->codec->codec_type = CODEC_TYPE_VIDEO;
  84. st->codec->codec_id = CODEC_ID_MJPEG;
  85. break;
  86. case 13:
  87. case 15:
  88. st->codec->codec_type = CODEC_TYPE_VIDEO;
  89. st->codec->codec_id = CODEC_ID_DVVIDEO;
  90. break;
  91. case 14:
  92. case 16:
  93. st->codec->codec_type = CODEC_TYPE_VIDEO;
  94. st->codec->codec_id = CODEC_ID_DVVIDEO;
  95. break;
  96. case 11:
  97. case 12:
  98. case 20:
  99. st->codec->codec_type = CODEC_TYPE_VIDEO;
  100. st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
  101. st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
  102. break;
  103. case 22:
  104. case 23:
  105. st->codec->codec_type = CODEC_TYPE_VIDEO;
  106. st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
  107. st->need_parsing = AVSTREAM_PARSE_HEADERS; //get keyframe flag etc.
  108. break;
  109. case 9:
  110. st->codec->codec_type = CODEC_TYPE_AUDIO;
  111. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  112. st->codec->channels = 1;
  113. st->codec->sample_rate = 48000;
  114. st->codec->bit_rate = 3 * 1 * 48000 * 8;
  115. st->codec->block_align = 3 * 1;
  116. st->codec->bits_per_sample = 24;
  117. break;
  118. case 10:
  119. st->codec->codec_type = CODEC_TYPE_AUDIO;
  120. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  121. st->codec->channels = 1;
  122. st->codec->sample_rate = 48000;
  123. st->codec->bit_rate = 2 * 1 * 48000 * 8;
  124. st->codec->block_align = 2 * 1;
  125. st->codec->bits_per_sample = 16;
  126. break;
  127. case 17:
  128. st->codec->codec_type = CODEC_TYPE_AUDIO;
  129. st->codec->codec_id = CODEC_ID_AC3;
  130. st->codec->channels = 2;
  131. st->codec->sample_rate = 48000;
  132. break;
  133. // timecode tracks:
  134. case 7:
  135. case 8:
  136. case 24:
  137. st->codec->codec_type = CODEC_TYPE_DATA;
  138. st->codec->codec_id = CODEC_ID_NONE;
  139. break;
  140. default:
  141. st->codec->codec_type = CODEC_TYPE_UNKNOWN;
  142. st->codec->codec_id = CODEC_ID_NONE;
  143. break;
  144. }
  145. return s->nb_streams - 1;
  146. }
  147. /**
  148. * \brief filters out interesting tags from material information.
  149. * \param len length of tag section, will be adjusted to contain remaining bytes
  150. * \param si struct to store collected information into
  151. */
  152. static void gxf_material_tags(ByteIOContext *pb, int *len, st_info_t *si) {
  153. si->first_field = AV_NOPTS_VALUE;
  154. si->last_field = AV_NOPTS_VALUE;
  155. while (*len >= 2) {
  156. mat_tag_t tag = get_byte(pb);
  157. int tlen = get_byte(pb);
  158. *len -= 2;
  159. if (tlen > *len)
  160. return;
  161. *len -= tlen;
  162. if (tlen == 4) {
  163. uint32_t value = get_be32(pb);
  164. if (tag == MAT_FIRST_FIELD)
  165. si->first_field = value;
  166. else if (tag == MAT_LAST_FIELD)
  167. si->last_field = value;
  168. } else
  169. url_fskip(pb, tlen);
  170. }
  171. }
  172. /**
  173. * \brief convert fps tag value to AVRational fps
  174. * \param fps fps value from tag
  175. * \return fps as AVRational, or 0 / 0 if unknown
  176. */
  177. static AVRational fps_tag2avr(int32_t fps) {
  178. extern const AVRational ff_frame_rate_tab[];
  179. if (fps < 1 || fps > 9) fps = 9;
  180. return ff_frame_rate_tab[9 - fps]; // values have opposite order
  181. }
  182. /**
  183. * \brief convert UMF attributes flags to AVRational fps
  184. * \param fps fps value from flags
  185. * \return fps as AVRational, or 0 / 0 if unknown
  186. */
  187. static AVRational fps_umf2avr(uint32_t flags) {
  188. static const AVRational map[] = {{50, 1}, {60000, 1001}, {24, 1},
  189. {25, 1}, {30000, 1001}};
  190. int idx = av_log2((flags & 0x7c0) >> 6);
  191. return map[idx];
  192. }
  193. /**
  194. * \brief filters out interesting tags from track information.
  195. * \param len length of tag section, will be adjusted to contain remaining bytes
  196. * \param si struct to store collected information into
  197. */
  198. static void gxf_track_tags(ByteIOContext *pb, int *len, st_info_t *si) {
  199. si->frames_per_second = (AVRational){0, 0};
  200. si->fields_per_frame = 0;
  201. while (*len >= 2) {
  202. track_tag_t tag = get_byte(pb);
  203. int tlen = get_byte(pb);
  204. *len -= 2;
  205. if (tlen > *len)
  206. return;
  207. *len -= tlen;
  208. if (tlen == 4) {
  209. uint32_t value = get_be32(pb);
  210. if (tag == TRACK_FPS)
  211. si->frames_per_second = fps_tag2avr(value);
  212. else if (tag == TRACK_FPF && (value == 1 || value == 2))
  213. si->fields_per_frame = value;
  214. } else
  215. url_fskip(pb, tlen);
  216. }
  217. }
  218. /**
  219. * \brief read index from FLT packet into stream 0 av_index
  220. */
  221. static void gxf_read_index(AVFormatContext *s, int pkt_len) {
  222. ByteIOContext *pb = s->pb;
  223. AVStream *st = s->streams[0];
  224. uint32_t fields_per_map = get_le32(pb);
  225. uint32_t map_cnt = get_le32(pb);
  226. int i;
  227. pkt_len -= 8;
  228. if (map_cnt > 1000) {
  229. av_log(s, AV_LOG_ERROR, "GXF: too many index entries %u (%x)\n", map_cnt, map_cnt);
  230. map_cnt = 1000;
  231. }
  232. if (pkt_len < 4 * map_cnt) {
  233. av_log(s, AV_LOG_ERROR, "GXF: invalid index length\n");
  234. url_fskip(pb, pkt_len);
  235. return;
  236. }
  237. pkt_len -= 4 * map_cnt;
  238. av_add_index_entry(st, 0, 0, 0, 0, 0);
  239. for (i = 0; i < map_cnt; i++)
  240. av_add_index_entry(st, (uint64_t)get_le32(pb) * 1024,
  241. i * (uint64_t)fields_per_map + 1, 0, 0, 0);
  242. url_fskip(pb, pkt_len);
  243. }
  244. static int gxf_header(AVFormatContext *s, AVFormatParameters *ap) {
  245. ByteIOContext *pb = s->pb;
  246. pkt_type_t pkt_type;
  247. int map_len;
  248. int len;
  249. AVRational main_timebase = {0, 0};
  250. st_info_t si;
  251. int i;
  252. if (!parse_packet_header(pb, &pkt_type, &map_len) || pkt_type != PKT_MAP) {
  253. av_log(s, AV_LOG_ERROR, "GXF: map packet not found\n");
  254. return 0;
  255. }
  256. map_len -= 2;
  257. if (get_byte(pb) != 0x0e0 || get_byte(pb) != 0xff) {
  258. av_log(s, AV_LOG_ERROR, "GXF: unknown version or invalid map preamble\n");
  259. return 0;
  260. }
  261. map_len -= 2;
  262. len = get_be16(pb); // length of material data section
  263. if (len > map_len) {
  264. av_log(s, AV_LOG_ERROR, "GXF: material data longer than map data\n");
  265. return 0;
  266. }
  267. map_len -= len;
  268. gxf_material_tags(pb, &len, &si);
  269. url_fskip(pb, len);
  270. map_len -= 2;
  271. len = get_be16(pb); // length of track description
  272. if (len > map_len) {
  273. av_log(s, AV_LOG_ERROR, "GXF: track description longer than map data\n");
  274. return 0;
  275. }
  276. map_len -= len;
  277. while (len > 0) {
  278. int track_type, track_id, track_len;
  279. AVStream *st;
  280. int idx;
  281. len -= 4;
  282. track_type = get_byte(pb);
  283. track_id = get_byte(pb);
  284. track_len = get_be16(pb);
  285. len -= track_len;
  286. gxf_track_tags(pb, &track_len, &si);
  287. url_fskip(pb, track_len);
  288. if (!(track_type & 0x80)) {
  289. av_log(s, AV_LOG_ERROR, "GXF: invalid track type %x\n", track_type);
  290. continue;
  291. }
  292. track_type &= 0x7f;
  293. if ((track_id & 0xc0) != 0xc0) {
  294. av_log(s, AV_LOG_ERROR, "GXF: invalid track id %x\n", track_id);
  295. continue;
  296. }
  297. track_id &= 0x3f;
  298. idx = get_sindex(s, track_id, track_type);
  299. if (idx < 0) continue;
  300. st = s->streams[idx];
  301. if (!main_timebase.num || !main_timebase.den) {
  302. main_timebase.num = si.frames_per_second.den;
  303. main_timebase.den = si.frames_per_second.num * si.fields_per_frame;
  304. }
  305. st->start_time = si.first_field;
  306. if (si.first_field != AV_NOPTS_VALUE && si.last_field != AV_NOPTS_VALUE)
  307. st->duration = si.last_field - si.first_field;
  308. }
  309. if (len < 0)
  310. av_log(s, AV_LOG_ERROR, "GXF: invalid track description length specified\n");
  311. if (map_len)
  312. url_fskip(pb, map_len);
  313. if (!parse_packet_header(pb, &pkt_type, &len)) {
  314. av_log(s, AV_LOG_ERROR, "GXF: sync lost in header\n");
  315. return -1;
  316. }
  317. if (pkt_type == PKT_FLT) {
  318. gxf_read_index(s, len);
  319. if (!parse_packet_header(pb, &pkt_type, &len)) {
  320. av_log(s, AV_LOG_ERROR, "GXF: sync lost in header\n");
  321. return -1;
  322. }
  323. }
  324. if (pkt_type == PKT_UMF) {
  325. if (len >= 0x39) {
  326. AVRational fps;
  327. len -= 0x39;
  328. url_fskip(pb, 5); // preamble
  329. url_fskip(pb, 0x30); // payload description
  330. fps = fps_umf2avr(get_le32(pb));
  331. if (!main_timebase.num || !main_timebase.den) {
  332. // this may not always be correct, but simply the best we can get
  333. main_timebase.num = fps.den;
  334. main_timebase.den = fps.num;
  335. }
  336. } else
  337. av_log(s, AV_LOG_INFO, "GXF: UMF packet too short\n");
  338. } else
  339. av_log(s, AV_LOG_INFO, "GXF: UMF packet missing\n");
  340. url_fskip(pb, len);
  341. if (!main_timebase.num || !main_timebase.den)
  342. main_timebase = (AVRational){1, 50}; // set some arbitrary fallback
  343. for (i = 0; i < s->nb_streams; i++) {
  344. AVStream *st = s->streams[i];
  345. av_set_pts_info(st, 32, main_timebase.num, main_timebase.den);
  346. }
  347. return 0;
  348. }
  349. #define READ_ONE() \
  350. { \
  351. if (!max_interval-- || url_feof(pb)) \
  352. goto out; \
  353. tmp = tmp << 8 | get_byte(pb); \
  354. }
  355. /**
  356. * \brief resync the stream on the next media packet with specified properties
  357. * \param max_interval how many bytes to search for matching packet at most
  358. * \param track track id the media packet must belong to, -1 for any
  359. * \param timestamp minimum timestamp (== field number) the packet must have, -1 for any
  360. * \return timestamp of packet found
  361. */
  362. static int64_t gxf_resync_media(AVFormatContext *s, uint64_t max_interval, int track, int timestamp) {
  363. uint32_t tmp;
  364. uint64_t last_pos;
  365. uint64_t last_found_pos = 0;
  366. int cur_track;
  367. int64_t cur_timestamp = AV_NOPTS_VALUE;
  368. int len;
  369. ByteIOContext *pb = s->pb;
  370. pkt_type_t type;
  371. tmp = get_be32(pb);
  372. start:
  373. while (tmp)
  374. READ_ONE();
  375. READ_ONE();
  376. if (tmp != 1)
  377. goto start;
  378. last_pos = url_ftell(pb);
  379. url_fseek(pb, -5, SEEK_CUR);
  380. if (!parse_packet_header(pb, &type, &len) || type != PKT_MEDIA) {
  381. url_fseek(pb, last_pos, SEEK_SET);
  382. goto start;
  383. }
  384. get_byte(pb);
  385. cur_track = get_byte(pb);
  386. cur_timestamp = get_be32(pb);
  387. last_found_pos = url_ftell(pb) - 16 - 6;
  388. if ((track >= 0 && track != cur_track) || (timestamp >= 0 && timestamp > cur_timestamp)) {
  389. url_fseek(pb, last_pos, SEEK_SET);
  390. goto start;
  391. }
  392. out:
  393. if (last_found_pos)
  394. url_fseek(pb, last_found_pos, SEEK_SET);
  395. return cur_timestamp;
  396. }
  397. static int gxf_packet(AVFormatContext *s, AVPacket *pkt) {
  398. ByteIOContext *pb = s->pb;
  399. pkt_type_t pkt_type;
  400. int pkt_len;
  401. while (!url_feof(pb)) {
  402. int track_type, track_id, ret;
  403. int field_nr;
  404. if (!parse_packet_header(pb, &pkt_type, &pkt_len)) {
  405. if (!url_feof(pb))
  406. av_log(s, AV_LOG_ERROR, "GXF: sync lost\n");
  407. return -1;
  408. }
  409. if (pkt_type == PKT_FLT) {
  410. gxf_read_index(s, pkt_len);
  411. continue;
  412. }
  413. if (pkt_type != PKT_MEDIA) {
  414. url_fskip(pb, pkt_len);
  415. continue;
  416. }
  417. if (pkt_len < 16) {
  418. av_log(s, AV_LOG_ERROR, "GXF: invalid media packet length\n");
  419. continue;
  420. }
  421. pkt_len -= 16;
  422. track_type = get_byte(pb);
  423. track_id = get_byte(pb);
  424. field_nr = get_be32(pb);
  425. get_be32(pb); // field information
  426. get_be32(pb); // "timeline" field number
  427. get_byte(pb); // flags
  428. get_byte(pb); // reserved
  429. // NOTE: there is also data length information in the
  430. // field information, it might be better to take this into account
  431. // as well.
  432. ret = av_get_packet(pb, pkt, pkt_len);
  433. pkt->stream_index = get_sindex(s, track_id, track_type);
  434. pkt->dts = field_nr;
  435. return ret;
  436. }
  437. return AVERROR(EIO);
  438. }
  439. static int gxf_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
  440. uint64_t pos;
  441. uint64_t maxlen = 100 * 1024 * 1024;
  442. AVStream *st = s->streams[0];
  443. int64_t start_time = s->streams[stream_index]->start_time;
  444. int64_t found;
  445. int idx;
  446. if (timestamp < start_time) timestamp = start_time;
  447. idx = av_index_search_timestamp(st, timestamp - start_time,
  448. AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
  449. if (idx < 0)
  450. return -1;
  451. pos = st->index_entries[idx].pos;
  452. if (idx < st->nb_index_entries - 2)
  453. maxlen = st->index_entries[idx + 2].pos - pos;
  454. maxlen = FFMAX(maxlen, 200 * 1024);
  455. url_fseek(s->pb, pos, SEEK_SET);
  456. found = gxf_resync_media(s, maxlen, -1, timestamp);
  457. if (FFABS(found - timestamp) > 4)
  458. return -1;
  459. return 0;
  460. }
  461. static int64_t gxf_read_timestamp(AVFormatContext *s, int stream_index,
  462. int64_t *pos, int64_t pos_limit) {
  463. ByteIOContext *pb = s->pb;
  464. int64_t res;
  465. url_fseek(pb, *pos, SEEK_SET);
  466. res = gxf_resync_media(s, pos_limit - *pos, -1, -1);
  467. *pos = url_ftell(pb);
  468. return res;
  469. }
  470. AVInputFormat gxf_demuxer = {
  471. "gxf",
  472. "GXF format",
  473. 0,
  474. gxf_probe,
  475. gxf_header,
  476. gxf_packet,
  477. NULL,
  478. gxf_seek,
  479. gxf_read_timestamp,
  480. };