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.

515 lines
16KB

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