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.

844 lines
25KB

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