gxfenc.c 25 KB

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