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.

943 lines
29KB

  1. /*
  2. * GXF muxer.
  3. * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
  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 "gxf.h"
  23. #include "riff.h"
  24. #include "audiointerleave.h"
  25. #define GXF_AUDIO_PACKET_SIZE 65536
  26. typedef struct GXFStreamContext {
  27. AudioInterleaveContext aic;
  28. uint32_t track_type;
  29. uint32_t sample_size;
  30. uint32_t sample_rate;
  31. uint16_t media_type;
  32. uint16_t media_info;
  33. int frame_rate_index;
  34. int lines_index;
  35. int fields;
  36. int iframes;
  37. int pframes;
  38. int bframes;
  39. int p_per_gop;
  40. int b_per_i_or_p; ///< number of B frames per I frame or P frame
  41. int first_gop_closed;
  42. unsigned order; ///< interleaving order
  43. } GXFStreamContext;
  44. typedef struct GXFContext {
  45. uint32_t nb_fields;
  46. uint16_t audio_tracks;
  47. uint16_t mpeg_tracks;
  48. int64_t creation_time;
  49. uint32_t umf_start_offset;
  50. uint32_t umf_track_offset;
  51. uint32_t umf_media_offset;
  52. uint32_t umf_length;
  53. uint16_t umf_track_size;
  54. uint16_t umf_media_size;
  55. AVRational time_base;
  56. int flags;
  57. GXFStreamContext timecode_track;
  58. unsigned *flt_entries; ///< offsets of packets /1024, starts after 2nd video field
  59. unsigned flt_entries_nb;
  60. uint64_t *map_offsets; ///< offset of map packets
  61. unsigned map_offsets_nb;
  62. unsigned packet_count;
  63. } GXFContext;
  64. static const struct {
  65. int height, index;
  66. } gxf_lines_tab[] = {
  67. { 480, 1 }, /* NTSC */
  68. { 512, 1 }, /* NTSC + VBI */
  69. { 576, 2 }, /* PAL */
  70. { 608, 2 }, /* PAL + VBI */
  71. { 1080, 4 },
  72. { 720, 6 },
  73. };
  74. static const AVCodecTag gxf_media_types[] = {
  75. { CODEC_ID_MJPEG , 3 }, /* NTSC */
  76. { CODEC_ID_MJPEG , 4 }, /* PAL */
  77. { CODEC_ID_PCM_S24LE , 9 },
  78. { CODEC_ID_PCM_S16LE , 10 },
  79. { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
  80. { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
  81. { CODEC_ID_DVVIDEO , 13 }, /* NTSC */
  82. { CODEC_ID_DVVIDEO , 14 }, /* PAL */
  83. { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
  84. { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
  85. { CODEC_ID_AC3 , 17 },
  86. //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
  87. { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
  88. { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
  89. { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
  90. { CODEC_ID_NONE, 0 },
  91. };
  92. #define SERVER_PATH "EXT:/PDR/default/"
  93. #define ES_NAME_PATTERN "EXT:/PDR/default/ES."
  94. static int gxf_find_lines_index(AVStream *st)
  95. {
  96. GXFStreamContext *sc = st->priv_data;
  97. int i;
  98. for (i = 0; i < 6; ++i) {
  99. if (st->codec->height == gxf_lines_tab[i].height) {
  100. sc->lines_index = gxf_lines_tab[i].index;
  101. return 0;
  102. }
  103. }
  104. return -1;
  105. }
  106. static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad)
  107. {
  108. for (; to_pad > 0; to_pad--) {
  109. put_byte(pb, 0);
  110. }
  111. }
  112. static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
  113. {
  114. int64_t curpos;
  115. int size;
  116. size = url_ftell(pb) - pos;
  117. if (size % 4) {
  118. gxf_write_padding(pb, 4 - size % 4);
  119. size = url_ftell(pb) - pos;
  120. }
  121. curpos = url_ftell(pb);
  122. url_fseek(pb, pos + 6, SEEK_SET);
  123. put_be32(pb, size);
  124. url_fseek(pb, curpos, SEEK_SET);
  125. return curpos - pos;
  126. }
  127. static int64_t updateSize(ByteIOContext *pb, int64_t pos)
  128. {
  129. int64_t curpos;
  130. curpos = url_ftell(pb);
  131. url_fseek(pb, pos, SEEK_SET);
  132. put_be16(pb, curpos - pos - 2);
  133. url_fseek(pb, curpos, SEEK_SET);
  134. return curpos - pos;
  135. }
  136. static void gxf_write_packet_header(ByteIOContext *pb, GXFPktType type)
  137. {
  138. put_be32(pb, 0); /* packet leader for synchro */
  139. put_byte(pb, 1);
  140. put_byte(pb, type); /* map packet */
  141. put_be32(pb, 0); /* size */
  142. put_be32(pb, 0); /* reserved */
  143. put_byte(pb, 0xE1); /* trailer 1 */
  144. put_byte(pb, 0xE2); /* trailer 2 */
  145. }
  146. static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, AVStream *st)
  147. {
  148. GXFStreamContext *sc = st->priv_data;
  149. char buffer[1024];
  150. int size, starting_line;
  151. if (sc->iframes) {
  152. sc->p_per_gop = sc->pframes / sc->iframes;
  153. if (sc->pframes % sc->iframes)
  154. sc->p_per_gop++;
  155. if (sc->pframes) {
  156. sc->b_per_i_or_p = sc->bframes / sc->pframes;
  157. if (sc->bframes % sc->pframes)
  158. sc->b_per_i_or_p++;
  159. }
  160. if (sc->p_per_gop > 9)
  161. sc->p_per_gop = 9; /* ensure value won't take more than one char */
  162. if (sc->b_per_i_or_p > 9)
  163. sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
  164. }
  165. if (st->codec->height == 512 || st->codec->height == 608)
  166. starting_line = 7; // VBI
  167. else if (st->codec->height == 480)
  168. starting_line = 20;
  169. else
  170. starting_line = 23; // default PAL
  171. size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
  172. "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
  173. (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
  174. st->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
  175. starting_line, st->codec->height / 16);
  176. put_byte(pb, TRACK_MPG_AUX);
  177. put_byte(pb, size + 1);
  178. put_buffer(pb, (uint8_t *)buffer, size + 1);
  179. return size + 3;
  180. }
  181. static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *sc)
  182. {
  183. put_byte(pb, 0); /* fields */
  184. put_byte(pb, 0); /* seconds */
  185. put_byte(pb, 0); /* minutes */
  186. put_byte(pb, 0); /* flags + hours */
  187. /* reserved */
  188. put_be32(pb, 0);
  189. return 8;
  190. }
  191. static int gxf_write_track_description(AVFormatContext *s, GXFStreamContext *sc, int index)
  192. {
  193. ByteIOContext *pb = s->pb;
  194. int64_t pos;
  195. int mpeg = sc->track_type == 4 || sc->track_type == 9;
  196. /* track description section */
  197. put_byte(pb, sc->media_type + 0x80);
  198. put_byte(pb, index + 0xC0);
  199. pos = url_ftell(pb);
  200. put_be16(pb, 0); /* size */
  201. /* media file name */
  202. put_byte(pb, TRACK_NAME);
  203. put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
  204. put_tag(pb, ES_NAME_PATTERN);
  205. put_be16(pb, sc->media_info);
  206. put_byte(pb, 0);
  207. if (!mpeg) {
  208. /* auxiliary information */
  209. put_byte(pb, TRACK_AUX);
  210. put_byte(pb, 8);
  211. if (sc->track_type == 3)
  212. gxf_write_timecode_auxiliary(pb, sc);
  213. else
  214. put_le64(pb, 0);
  215. }
  216. /* file system version */
  217. put_byte(pb, TRACK_VER);
  218. put_byte(pb, 4);
  219. put_be32(pb, 0);
  220. if (mpeg)
  221. gxf_write_mpeg_auxiliary(pb, s->streams[index]);
  222. /* frame rate */
  223. put_byte(pb, TRACK_FPS);
  224. put_byte(pb, 4);
  225. put_be32(pb, sc->frame_rate_index);
  226. /* lines per frame */
  227. put_byte(pb, TRACK_LINES);
  228. put_byte(pb, 4);
  229. put_be32(pb, sc->lines_index);
  230. /* fields per frame */
  231. put_byte(pb, TRACK_FPF);
  232. put_byte(pb, 4);
  233. put_be32(pb, sc->fields);
  234. return updateSize(pb, pos);
  235. }
  236. static int gxf_write_material_data_section(AVFormatContext *s)
  237. {
  238. GXFContext *gxf = s->priv_data;
  239. ByteIOContext *pb = s->pb;
  240. int64_t pos;
  241. const char *filename = strrchr(s->filename, '/');
  242. pos = url_ftell(pb);
  243. put_be16(pb, 0); /* size */
  244. /* name */
  245. if (filename)
  246. filename++;
  247. else
  248. filename = s->filename;
  249. put_byte(pb, MAT_NAME);
  250. put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
  251. put_tag(pb, SERVER_PATH);
  252. put_tag(pb, filename);
  253. put_byte(pb, 0);
  254. /* first field */
  255. put_byte(pb, MAT_FIRST_FIELD);
  256. put_byte(pb, 4);
  257. put_be32(pb, 0);
  258. /* last field */
  259. put_byte(pb, MAT_LAST_FIELD);
  260. put_byte(pb, 4);
  261. put_be32(pb, gxf->nb_fields);
  262. /* reserved */
  263. put_byte(pb, MAT_MARK_IN);
  264. put_byte(pb, 4);
  265. put_be32(pb, 0);
  266. put_byte(pb, MAT_MARK_OUT);
  267. put_byte(pb, 4);
  268. put_be32(pb, gxf->nb_fields);
  269. /* estimated size */
  270. put_byte(pb, MAT_SIZE);
  271. put_byte(pb, 4);
  272. put_be32(pb, url_fsize(pb) / 1024);
  273. return updateSize(pb, pos);
  274. }
  275. static int gxf_write_track_description_section(AVFormatContext *s)
  276. {
  277. GXFContext *gxf = s->priv_data;
  278. ByteIOContext *pb = s->pb;
  279. int64_t pos;
  280. int i;
  281. pos = url_ftell(pb);
  282. put_be16(pb, 0); /* size */
  283. for (i = 0; i < s->nb_streams; ++i)
  284. gxf_write_track_description(s, s->streams[i]->priv_data, i);
  285. gxf_write_track_description(s, &gxf->timecode_track, s->nb_streams);
  286. return updateSize(pb, pos);
  287. }
  288. static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
  289. {
  290. GXFContext *gxf = s->priv_data;
  291. ByteIOContext *pb = s->pb;
  292. int64_t pos = url_ftell(pb);
  293. if (!rewrite) {
  294. if (!(gxf->map_offsets_nb % 30)) {
  295. gxf->map_offsets = av_realloc(gxf->map_offsets,
  296. (gxf->map_offsets_nb+30)*sizeof(*gxf->map_offsets));
  297. if (!gxf->map_offsets) {
  298. av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
  299. return -1;
  300. }
  301. }
  302. gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here
  303. }
  304. gxf_write_packet_header(pb, PKT_MAP);
  305. /* preamble */
  306. put_byte(pb, 0xE0); /* version */
  307. put_byte(pb, 0xFF); /* reserved */
  308. gxf_write_material_data_section(s);
  309. gxf_write_track_description_section(s);
  310. return updatePacketSize(pb, pos);
  311. }
  312. static int gxf_write_flt_packet(AVFormatContext *s)
  313. {
  314. GXFContext *gxf = s->priv_data;
  315. ByteIOContext *pb = s->pb;
  316. int64_t pos = url_ftell(pb);
  317. int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
  318. int flt_entries = gxf->nb_fields / fields_per_flt - 1;
  319. int i = 0;
  320. gxf_write_packet_header(pb, PKT_FLT);
  321. put_le32(pb, fields_per_flt); /* number of fields */
  322. put_le32(pb, flt_entries); /* number of active flt entries */
  323. if (gxf->flt_entries) {
  324. for (i = 0; i < flt_entries; i++)
  325. put_le32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
  326. }
  327. for (; i < 1000; i++)
  328. put_le32(pb, 0);
  329. return updatePacketSize(pb, pos);
  330. }
  331. static int gxf_write_umf_material_description(AVFormatContext *s)
  332. {
  333. GXFContext *gxf = s->priv_data;
  334. ByteIOContext *pb = s->pb;
  335. int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
  336. // XXX drop frame
  337. uint32_t timecode =
  338. gxf->nb_fields / (timecode_base * 3600) % 24 << 24 | // hours
  339. gxf->nb_fields / (timecode_base * 60) % 60 << 16 | // minutes
  340. gxf->nb_fields / timecode_base % 60 << 8 | // seconds
  341. gxf->nb_fields % timecode_base; // fields
  342. put_le32(pb, gxf->flags);
  343. put_le32(pb, gxf->nb_fields); /* length of the longest track */
  344. put_le32(pb, gxf->nb_fields); /* length of the shortest track */
  345. put_le32(pb, 0); /* mark in */
  346. put_le32(pb, gxf->nb_fields); /* mark out */
  347. put_le32(pb, 0); /* timecode mark in */
  348. put_le32(pb, timecode); /* timecode mark out */
  349. put_le64(pb, s->timestamp); /* modification time */
  350. put_le64(pb, s->timestamp); /* creation time */
  351. put_le16(pb, 0); /* reserved */
  352. put_le16(pb, 0); /* reserved */
  353. put_le16(pb, gxf->audio_tracks);
  354. put_le16(pb, 1); /* timecode track count */
  355. put_le16(pb, 0); /* reserved */
  356. put_le16(pb, gxf->mpeg_tracks);
  357. return 48;
  358. }
  359. static int gxf_write_umf_payload(AVFormatContext *s)
  360. {
  361. GXFContext *gxf = s->priv_data;
  362. ByteIOContext *pb = s->pb;
  363. put_le32(pb, gxf->umf_length); /* total length of the umf data */
  364. put_le32(pb, 3); /* version */
  365. put_le32(pb, s->nb_streams+1);
  366. put_le32(pb, gxf->umf_track_offset); /* umf track section offset */
  367. put_le32(pb, gxf->umf_track_size);
  368. put_le32(pb, s->nb_streams+1);
  369. put_le32(pb, gxf->umf_media_offset);
  370. put_le32(pb, gxf->umf_media_size);
  371. put_le32(pb, gxf->umf_length); /* user data offset */
  372. put_le32(pb, 0); /* user data size */
  373. put_le32(pb, 0); /* reserved */
  374. put_le32(pb, 0); /* reserved */
  375. return 48;
  376. }
  377. static int gxf_write_umf_track_description(AVFormatContext *s)
  378. {
  379. ByteIOContext *pb = s->pb;
  380. GXFContext *gxf = s->priv_data;
  381. int64_t pos = url_ftell(pb);
  382. int i;
  383. gxf->umf_track_offset = pos - gxf->umf_start_offset;
  384. for (i = 0; i < s->nb_streams; ++i) {
  385. GXFStreamContext *sc = s->streams[i]->priv_data;
  386. put_le16(pb, sc->media_info);
  387. put_le16(pb, 1);
  388. }
  389. put_le16(pb, gxf->timecode_track.media_info);
  390. put_le16(pb, 1);
  391. return url_ftell(pb) - pos;
  392. }
  393. static int gxf_write_umf_media_mpeg(ByteIOContext *pb, AVStream *st)
  394. {
  395. GXFStreamContext *sc = st->priv_data;
  396. if (st->codec->pix_fmt == PIX_FMT_YUV422P)
  397. put_le32(pb, 2);
  398. else
  399. put_le32(pb, 1); /* default to 420 */
  400. put_le32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
  401. put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
  402. put_le32(pb, 1); /* I picture per GOP */
  403. put_le32(pb, sc->p_per_gop);
  404. put_le32(pb, sc->b_per_i_or_p);
  405. if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
  406. put_le32(pb, 2);
  407. else if (st->codec->codec_id == CODEC_ID_MPEG1VIDEO)
  408. put_le32(pb, 1);
  409. else
  410. put_le32(pb, 0);
  411. put_le32(pb, 0); /* reserved */
  412. return 32;
  413. }
  414. static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *sc)
  415. {
  416. put_le32(pb, 1); /* non drop frame */
  417. put_le32(pb, 0); /* reserved */
  418. put_le32(pb, 0); /* reserved */
  419. put_le32(pb, 0); /* reserved */
  420. put_le32(pb, 0); /* reserved */
  421. put_le32(pb, 0); /* reserved */
  422. put_le32(pb, 0); /* reserved */
  423. put_le32(pb, 0); /* reserved */
  424. return 32;
  425. }
  426. static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *sc)
  427. {
  428. int i;
  429. for (i = 0; i < 8; i++) {
  430. put_be32(pb, 0);
  431. }
  432. return 32;
  433. }
  434. static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *sc)
  435. {
  436. put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
  437. put_le64(pb, av_dbl2int(1)); /* sound level to begin to */
  438. put_le32(pb, 0); /* number of fields over which to ramp up sound level */
  439. put_le32(pb, 0); /* number of fields over which to ramp down sound level */
  440. put_le32(pb, 0); /* reserved */
  441. put_le32(pb, 0); /* reserved */
  442. return 32;
  443. }
  444. #if 0
  445. static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *sc)
  446. {
  447. put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */
  448. put_be64(pb, 0); /* FIXME FLOAT max luma quant level */
  449. put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */
  450. put_be64(pb, 0); /* FIXME FLOAT min luma quant level */
  451. return 32;
  452. }
  453. #endif
  454. static int gxf_write_umf_media_description(AVFormatContext *s)
  455. {
  456. GXFContext *gxf = s->priv_data;
  457. ByteIOContext *pb = s->pb;
  458. int64_t pos;
  459. int i, j;
  460. pos = url_ftell(pb);
  461. gxf->umf_media_offset = pos - gxf->umf_start_offset;
  462. for (i = 0; i <= s->nb_streams; ++i) {
  463. GXFStreamContext *sc;
  464. int64_t startpos, curpos;
  465. if (i == s->nb_streams)
  466. sc = &gxf->timecode_track;
  467. else
  468. sc = s->streams[i]->priv_data;
  469. startpos = url_ftell(pb);
  470. put_le16(pb, 0); /* length */
  471. put_le16(pb, sc->media_info);
  472. put_le16(pb, 0); /* reserved */
  473. put_le16(pb, 0); /* reserved */
  474. put_le32(pb, gxf->nb_fields);
  475. put_le32(pb, 0); /* attributes rw, ro */
  476. put_le32(pb, 0); /* mark in */
  477. put_le32(pb, gxf->nb_fields); /* mark out */
  478. put_buffer(pb, ES_NAME_PATTERN, strlen(ES_NAME_PATTERN));
  479. put_be16(pb, sc->media_info);
  480. for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
  481. put_byte(pb, 0);
  482. put_le32(pb, sc->track_type);
  483. put_le32(pb, sc->sample_rate);
  484. put_le32(pb, sc->sample_size);
  485. put_le32(pb, 0); /* reserved */
  486. if (sc == &gxf->timecode_track)
  487. gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */
  488. else {
  489. AVStream *st = s->streams[i];
  490. switch (st->codec->codec_id) {
  491. case CODEC_ID_MPEG2VIDEO:
  492. gxf_write_umf_media_mpeg(pb, st);
  493. break;
  494. case CODEC_ID_PCM_S16LE:
  495. gxf_write_umf_media_audio(pb, sc);
  496. break;
  497. case CODEC_ID_DVVIDEO:
  498. gxf_write_umf_media_dv(pb, sc);
  499. break;
  500. }
  501. }
  502. curpos = url_ftell(pb);
  503. url_fseek(pb, startpos, SEEK_SET);
  504. put_le16(pb, curpos - startpos);
  505. url_fseek(pb, curpos, SEEK_SET);
  506. }
  507. return url_ftell(pb) - pos;
  508. }
  509. static int gxf_write_umf_packet(AVFormatContext *s)
  510. {
  511. GXFContext *gxf = s->priv_data;
  512. ByteIOContext *pb = s->pb;
  513. int64_t pos = url_ftell(pb);
  514. gxf_write_packet_header(pb, PKT_UMF);
  515. /* preamble */
  516. put_byte(pb, 3); /* first and last (only) packet */
  517. put_be32(pb, gxf->umf_length); /* data length */
  518. gxf->umf_start_offset = url_ftell(pb);
  519. gxf_write_umf_payload(s);
  520. gxf_write_umf_material_description(s);
  521. gxf->umf_track_size = gxf_write_umf_track_description(s);
  522. gxf->umf_media_size = gxf_write_umf_media_description(s);
  523. gxf->umf_length = url_ftell(pb) - gxf->umf_start_offset;
  524. return updatePacketSize(pb, pos);
  525. }
  526. static const int GXF_samples_per_frame[] = { 32768, 0 };
  527. static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc)
  528. {
  529. if (!vsc)
  530. return;
  531. sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
  532. sc->sample_rate = vsc->sample_rate;
  533. sc->media_info = ('T'<<8) | '0';
  534. sc->track_type = 3;
  535. sc->frame_rate_index = vsc->frame_rate_index;
  536. sc->lines_index = vsc->lines_index;
  537. sc->sample_size = 16;
  538. sc->fields = vsc->fields;
  539. }
  540. static int gxf_write_header(AVFormatContext *s)
  541. {
  542. ByteIOContext *pb = s->pb;
  543. GXFContext *gxf = s->priv_data;
  544. GXFStreamContext *vsc = NULL;
  545. uint8_t tracks[255] = {0};
  546. int i, media_info = 0;
  547. if (url_is_streamed(pb)) {
  548. av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome");
  549. return -1;
  550. }
  551. gxf->flags |= 0x00080000; /* material is simple clip */
  552. for (i = 0; i < s->nb_streams; ++i) {
  553. AVStream *st = s->streams[i];
  554. GXFStreamContext *sc = av_mallocz(sizeof(*sc));
  555. if (!sc)
  556. return AVERROR(ENOMEM);
  557. st->priv_data = sc;
  558. sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
  559. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  560. if (st->codec->codec_id != CODEC_ID_PCM_S16LE) {
  561. av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
  562. return -1;
  563. }
  564. if (st->codec->sample_rate != 48000) {
  565. av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
  566. return -1;
  567. }
  568. if (st->codec->channels != 1) {
  569. av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
  570. return -1;
  571. }
  572. sc->track_type = 2;
  573. sc->sample_rate = st->codec->sample_rate;
  574. av_set_pts_info(st, 64, 1, sc->sample_rate);
  575. sc->sample_size = 16;
  576. sc->frame_rate_index = -2;
  577. sc->lines_index = -2;
  578. sc->fields = -2;
  579. gxf->audio_tracks++;
  580. gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
  581. media_info = 'A';
  582. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  583. if (i != 0) {
  584. av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
  585. return -1;
  586. }
  587. /* FIXME check from time_base ? */
  588. if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
  589. sc->frame_rate_index = 5;
  590. sc->sample_rate = 60;
  591. gxf->flags |= 0x00000080;
  592. gxf->time_base = (AVRational){ 1001, 60000 };
  593. } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
  594. sc->frame_rate_index = 6;
  595. sc->media_type++;
  596. sc->sample_rate = 50;
  597. gxf->flags |= 0x00000040;
  598. gxf->time_base = (AVRational){ 1, 50 };
  599. } else {
  600. av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
  601. "gxf muxer only accepts PAL or NTSC resolutions currently\n");
  602. return -1;
  603. }
  604. av_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
  605. if (gxf_find_lines_index(st) < 0)
  606. sc->lines_index = -1;
  607. sc->sample_size = st->codec->bit_rate;
  608. sc->fields = 2; /* interlaced */
  609. vsc = sc;
  610. switch (st->codec->codec_id) {
  611. case CODEC_ID_MJPEG:
  612. sc->track_type = 1;
  613. gxf->flags |= 0x00004000;
  614. media_info = 'J';
  615. break;
  616. case CODEC_ID_MPEG1VIDEO:
  617. sc->track_type = 9;
  618. gxf->mpeg_tracks++;
  619. media_info = 'L';
  620. break;
  621. case CODEC_ID_MPEG2VIDEO:
  622. sc->first_gop_closed = -1;
  623. sc->track_type = 4;
  624. gxf->mpeg_tracks++;
  625. gxf->flags |= 0x00008000;
  626. media_info = 'M';
  627. break;
  628. case CODEC_ID_DVVIDEO:
  629. if (st->codec->pix_fmt == PIX_FMT_YUV422P) {
  630. sc->media_type += 2;
  631. sc->track_type = 6;
  632. gxf->flags |= 0x00002000;
  633. media_info = 'E';
  634. } else {
  635. sc->track_type = 5;
  636. gxf->flags |= 0x00001000;
  637. media_info = 'D';
  638. }
  639. break;
  640. default:
  641. av_log(s, AV_LOG_ERROR, "video codec not supported\n");
  642. return -1;
  643. }
  644. }
  645. /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
  646. sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
  647. sc->order = s->nb_streams - st->index;
  648. }
  649. if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
  650. return -1;
  651. gxf_init_timecode_track(&gxf->timecode_track, vsc);
  652. gxf->flags |= 0x200000; // time code track is non-drop frame
  653. gxf_write_map_packet(s, 0);
  654. gxf_write_flt_packet(s);
  655. gxf_write_umf_packet(s);
  656. gxf->packet_count = 3;
  657. put_flush_packet(pb);
  658. return 0;
  659. }
  660. static int gxf_write_eos_packet(ByteIOContext *pb)
  661. {
  662. int64_t pos = url_ftell(pb);
  663. gxf_write_packet_header(pb, PKT_EOS);
  664. return updatePacketSize(pb, pos);
  665. }
  666. static int gxf_write_trailer(AVFormatContext *s)
  667. {
  668. GXFContext *gxf = s->priv_data;
  669. ByteIOContext *pb = s->pb;
  670. int64_t end;
  671. int i;
  672. ff_audio_interleave_close(s);
  673. gxf_write_eos_packet(pb);
  674. end = url_ftell(pb);
  675. url_fseek(pb, 0, SEEK_SET);
  676. /* overwrite map, flt and umf packets with new values */
  677. gxf_write_map_packet(s, 1);
  678. gxf_write_flt_packet(s);
  679. gxf_write_umf_packet(s);
  680. put_flush_packet(pb);
  681. /* update duration in all map packets */
  682. for (i = 1; i < gxf->map_offsets_nb; i++) {
  683. url_fseek(pb, gxf->map_offsets[i], SEEK_SET);
  684. gxf_write_map_packet(s, 1);
  685. put_flush_packet(pb);
  686. }
  687. url_fseek(pb, end, SEEK_SET);
  688. av_freep(&gxf->flt_entries);
  689. av_freep(&gxf->map_offsets);
  690. return 0;
  691. }
  692. static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
  693. {
  694. uint32_t c=-1;
  695. int i;
  696. for(i=0; i<size-4 && c!=0x100; i++){
  697. c = (c<<8) + buf[i];
  698. if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
  699. sc->first_gop_closed= (buf[i+4]>>6)&1;
  700. }
  701. return (buf[i+1]>>3)&7;
  702. }
  703. static int gxf_write_media_preamble(AVFormatContext *s, AVPacket *pkt, int size)
  704. {
  705. GXFContext *gxf = s->priv_data;
  706. ByteIOContext *pb = s->pb;
  707. AVStream *st = s->streams[pkt->stream_index];
  708. GXFStreamContext *sc = st->priv_data;
  709. unsigned field_nb;
  710. /* If the video is frame-encoded, the frame numbers shall be represented by
  711. * even field numbers.
  712. * see SMPTE360M-2004 6.4.2.1.3 Media field number */
  713. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  714. field_nb = gxf->nb_fields;
  715. } else {
  716. field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
  717. (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
  718. }
  719. put_byte(pb, sc->media_type);
  720. put_byte(pb, st->index);
  721. put_be32(pb, field_nb);
  722. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  723. put_be16(pb, 0);
  724. put_be16(pb, size / 2);
  725. } else if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
  726. int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
  727. if (frame_type == FF_I_TYPE) {
  728. put_byte(pb, 0x0d);
  729. sc->iframes++;
  730. } else if (frame_type == FF_B_TYPE) {
  731. put_byte(pb, 0x0f);
  732. sc->bframes++;
  733. } else {
  734. put_byte(pb, 0x0e);
  735. sc->pframes++;
  736. }
  737. put_be24(pb, size);
  738. } else if (st->codec->codec_id == CODEC_ID_DVVIDEO) {
  739. put_byte(pb, size / 4096);
  740. put_be24(pb, 0);
  741. } else
  742. put_be32(pb, size);
  743. put_be32(pb, field_nb);
  744. put_byte(pb, 1); /* flags */
  745. put_byte(pb, 0); /* reserved */
  746. return 16;
  747. }
  748. static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
  749. {
  750. GXFContext *gxf = s->priv_data;
  751. ByteIOContext *pb = s->pb;
  752. AVStream *st = s->streams[pkt->stream_index];
  753. int64_t pos = url_ftell(pb);
  754. int padding = 0;
  755. gxf_write_packet_header(pb, PKT_MEDIA);
  756. if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
  757. padding = 4 - pkt->size % 4;
  758. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  759. padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
  760. gxf_write_media_preamble(s, pkt, pkt->size + padding);
  761. put_buffer(pb, pkt->data, pkt->size);
  762. gxf_write_padding(pb, padding);
  763. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  764. if (!(gxf->flt_entries_nb % 500)) {
  765. gxf->flt_entries = av_realloc(gxf->flt_entries,
  766. (gxf->flt_entries_nb+500)*sizeof(*gxf->flt_entries));
  767. if (!gxf->flt_entries) {
  768. av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
  769. return -1;
  770. }
  771. }
  772. gxf->flt_entries[gxf->flt_entries_nb++] = url_ftell(pb) / 1024;
  773. gxf->nb_fields += 2; // count fields
  774. }
  775. updatePacketSize(pb, pos);
  776. gxf->packet_count++;
  777. if (gxf->packet_count == 100) {
  778. gxf_write_map_packet(s, 0);
  779. gxf->packet_count = 0;
  780. }
  781. put_flush_packet(pb);
  782. return 0;
  783. }
  784. static int gxf_compare_field_nb(AVFormatContext *s, AVPacket *next, AVPacket *cur)
  785. {
  786. GXFContext *gxf = s->priv_data;
  787. AVPacket *pkt[2] = { cur, next };
  788. int i, field_nb[2];
  789. GXFStreamContext *sc[2];
  790. for (i = 0; i < 2; i++) {
  791. AVStream *st = s->streams[pkt[i]->stream_index];
  792. sc[i] = st->priv_data;
  793. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  794. field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
  795. (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
  796. field_nb[i] &= ~1; // compare against even field number because audio must be before video
  797. } else
  798. field_nb[i] = pkt[i]->dts; // dts are field based
  799. }
  800. return field_nb[1] > field_nb[0] ||
  801. (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
  802. }
  803. static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  804. {
  805. if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  806. pkt->duration = 2; // enforce 2 fields
  807. return ff_audio_rechunk_interleave(s, out, pkt, flush,
  808. av_interleave_packet_per_dts, gxf_compare_field_nb);
  809. }
  810. AVOutputFormat gxf_muxer = {
  811. "gxf",
  812. NULL_IF_CONFIG_SMALL("GXF format"),
  813. NULL,
  814. "gxf",
  815. sizeof(GXFContext),
  816. CODEC_ID_PCM_S16LE,
  817. CODEC_ID_MPEG2VIDEO,
  818. gxf_write_header,
  819. gxf_write_packet,
  820. gxf_write_trailer,
  821. 0,
  822. NULL,
  823. gxf_interleave_packet,
  824. };