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.

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