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.

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