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.

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