FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gxfenc.c
Go to the documentation of this file.
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 
22 #include "libavutil/avassert.h"
23 #include "libavutil/intfloat.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/timecode.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "gxf.h"
30 #include "audiointerleave.h"
31 
32 #define GXF_AUDIO_PACKET_SIZE 65536
33 
34 #define GXF_TIMECODE(c, d, h, m, s, f) \
35  ((c) << 30 | (d) << 29 | (h) << 24 | (m) << 16 | (s) << 8 | (f))
36 
37 typedef struct GXFTimecode{
38  int hh;
39  int mm;
40  int ss;
41  int ff;
42  int color;
43  int drop;
44 } GXFTimecode;
45 
46 typedef struct GXFStreamContext {
48  uint32_t track_type;
49  uint32_t sample_size;
50  uint32_t sample_rate;
51  uint16_t media_type;
52  uint16_t media_info;
55  int fields;
56  int iframes;
57  int pframes;
58  int bframes;
59  int p_per_gop;
60  int b_per_i_or_p; ///< number of B frames per I frame or P frame
62  unsigned order; ///< interleaving order
64 
65 typedef struct GXFContext {
67  uint32_t nb_fields;
68  uint16_t audio_tracks;
69  uint16_t mpeg_tracks;
70  int64_t creation_time;
71  uint32_t umf_start_offset;
72  uint32_t umf_track_offset;
73  uint32_t umf_media_offset;
74  uint32_t umf_length;
75  uint16_t umf_track_size;
76  uint16_t umf_media_size;
78  int flags;
80  unsigned *flt_entries; ///< offsets of packets /1024, starts after 2nd video field
81  unsigned flt_entries_nb;
82  uint64_t *map_offsets; ///< offset of map packets
83  unsigned map_offsets_nb;
84  unsigned packet_count;
86 } GXFContext;
87 
88 static const struct {
89  int height, index;
90 } gxf_lines_tab[] = {
91  { 480, 1 }, /* NTSC */
92  { 512, 1 }, /* NTSC + VBI */
93  { 576, 2 }, /* PAL */
94  { 608, 2 }, /* PAL + VBI */
95  { 1080, 4 },
96  { 720, 6 },
97 };
98 
99 static const AVCodecTag gxf_media_types[] = {
100  { AV_CODEC_ID_MJPEG , 3 }, /* NTSC */
101  { AV_CODEC_ID_MJPEG , 4 }, /* PAL */
102  { AV_CODEC_ID_PCM_S24LE , 9 },
103  { AV_CODEC_ID_PCM_S16LE , 10 },
104  { AV_CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */
105  { AV_CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */
106  { AV_CODEC_ID_DVVIDEO , 13 }, /* NTSC */
107  { AV_CODEC_ID_DVVIDEO , 14 }, /* PAL */
108  { AV_CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */
109  { AV_CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */
110  { AV_CODEC_ID_AC3 , 17 },
111  //{ AV_CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */
112  { AV_CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */
113  { AV_CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */
114  { AV_CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */
115  { AV_CODEC_ID_NONE, 0 },
116 };
117 
118 #define SERVER_PATH "EXT:/PDR/default/"
119 #define ES_NAME_PATTERN "EXT:/PDR/default/ES."
120 
122 {
123  GXFStreamContext *sc = st->priv_data;
124  int i;
125 
126  for (i = 0; i < 6; ++i) {
127  if (st->codec->height == gxf_lines_tab[i].height) {
128  sc->lines_index = gxf_lines_tab[i].index;
129  return 0;
130  }
131  }
132  return -1;
133 }
134 
135 static void gxf_write_padding(AVIOContext *pb, int64_t to_pad)
136 {
137  for (; to_pad > 0; to_pad--) {
138  avio_w8(pb, 0);
139  }
140 }
141 
142 static int64_t updatePacketSize(AVIOContext *pb, int64_t pos)
143 {
144  int64_t curpos;
145  int size;
146 
147  size = avio_tell(pb) - pos;
148  if (size % 4) {
149  gxf_write_padding(pb, 4 - size % 4);
150  size = avio_tell(pb) - pos;
151  }
152  curpos = avio_tell(pb);
153  avio_seek(pb, pos + 6, SEEK_SET);
154  avio_wb32(pb, size);
155  avio_seek(pb, curpos, SEEK_SET);
156  return curpos - pos;
157 }
158 
159 static int64_t updateSize(AVIOContext *pb, int64_t pos)
160 {
161  int64_t curpos;
162 
163  curpos = avio_tell(pb);
164  avio_seek(pb, pos, SEEK_SET);
165  avio_wb16(pb, curpos - pos - 2);
166  avio_seek(pb, curpos, SEEK_SET);
167  return curpos - pos;
168 }
169 
171 {
172  avio_wb32(pb, 0); /* packet leader for synchro */
173  avio_w8(pb, 1);
174  avio_w8(pb, type); /* map packet */
175  avio_wb32(pb, 0); /* size */
176  avio_wb32(pb, 0); /* reserved */
177  avio_w8(pb, 0xE1); /* trailer 1 */
178  avio_w8(pb, 0xE2); /* trailer 2 */
179 }
180 
182 {
183  GXFStreamContext *sc = st->priv_data;
184  char buffer[1024];
185  int size, starting_line;
186 
187  if (sc->iframes) {
188  sc->p_per_gop = sc->pframes / sc->iframes;
189  if (sc->pframes % sc->iframes)
190  sc->p_per_gop++;
191  if (sc->pframes) {
192  sc->b_per_i_or_p = sc->bframes / sc->pframes;
193  if (sc->bframes % sc->pframes)
194  sc->b_per_i_or_p++;
195  }
196  if (sc->p_per_gop > 9)
197  sc->p_per_gop = 9; /* ensure value won't take more than one char */
198  if (sc->b_per_i_or_p > 9)
199  sc->b_per_i_or_p = 9; /* ensure value won't take more than one char */
200  }
201  if (st->codec->height == 512 || st->codec->height == 608)
202  starting_line = 7; // VBI
203  else if (st->codec->height == 480)
204  starting_line = 20;
205  else
206  starting_line = 23; // default PAL
207 
208  size = snprintf(buffer, sizeof(buffer), "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n"
209  "Pix 0\nCf %d\nCg %d\nSl %d\nnl16 %d\nVi 1\nf1 1\n",
210  (float)st->codec->bit_rate, sc->p_per_gop, sc->b_per_i_or_p,
211  st->codec->pix_fmt == AV_PIX_FMT_YUV422P ? 2 : 1, sc->first_gop_closed == 1,
212  starting_line, (st->codec->height + 15) / 16);
213  av_assert0(size < sizeof(buffer));
214  avio_w8(pb, TRACK_MPG_AUX);
215  avio_w8(pb, size + 1);
216  avio_write(pb, (uint8_t *)buffer, size + 1);
217  return size + 3;
218 }
219 
221 {
222  uint32_t timecode = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
223  gxf->tc.hh, gxf->tc.mm,
224  gxf->tc.ss, gxf->tc.ff);
225 
226  avio_wl32(pb, timecode);
227  /* reserved */
228  avio_wl32(pb, 0);
229  return 8;
230 }
231 
233 {
234  GXFContext *gxf = s->priv_data;
235  AVIOContext *pb = s->pb;
236  int64_t pos;
237  int mpeg = sc->track_type == 4 || sc->track_type == 9;
238 
239  /* track description section */
240  avio_w8(pb, sc->media_type + 0x80);
241  avio_w8(pb, index + 0xC0);
242 
243  pos = avio_tell(pb);
244  avio_wb16(pb, 0); /* size */
245 
246  /* media file name */
247  avio_w8(pb, TRACK_NAME);
248  avio_w8(pb, strlen(ES_NAME_PATTERN) + 3);
249  avio_write(pb, ES_NAME_PATTERN, sizeof(ES_NAME_PATTERN) - 1);
250  avio_wb16(pb, sc->media_info);
251  avio_w8(pb, 0);
252 
253  if (!mpeg) {
254  /* auxiliary information */
255  avio_w8(pb, TRACK_AUX);
256  avio_w8(pb, 8);
257  if (sc->track_type == 3)
259  else
260  avio_wl64(pb, 0);
261  }
262 
263  /* file system version */
264  avio_w8(pb, TRACK_VER);
265  avio_w8(pb, 4);
266  avio_wb32(pb, 0);
267 
268  if (mpeg)
269  gxf_write_mpeg_auxiliary(pb, s->streams[index]);
270 
271  /* frame rate */
272  avio_w8(pb, TRACK_FPS);
273  avio_w8(pb, 4);
274  avio_wb32(pb, sc->frame_rate_index);
275 
276  /* lines per frame */
277  avio_w8(pb, TRACK_LINES);
278  avio_w8(pb, 4);
279  avio_wb32(pb, sc->lines_index);
280 
281  /* fields per frame */
282  avio_w8(pb, TRACK_FPF);
283  avio_w8(pb, 4);
284  avio_wb32(pb, sc->fields);
285 
286  return updateSize(pb, pos);
287 }
288 
290 {
291  GXFContext *gxf = s->priv_data;
292  AVIOContext *pb = s->pb;
293  int64_t pos;
294  int len;
295  const char *filename = strrchr(s->filename, '/');
296 
297  pos = avio_tell(pb);
298  avio_wb16(pb, 0); /* size */
299 
300  /* name */
301  if (filename)
302  filename++;
303  else
304  filename = s->filename;
305  len = strlen(filename);
306 
307  avio_w8(pb, MAT_NAME);
308  avio_w8(pb, strlen(SERVER_PATH) + len + 1);
309  avio_write(pb, SERVER_PATH, sizeof(SERVER_PATH) - 1);
310  avio_write(pb, filename, len);
311  avio_w8(pb, 0);
312 
313  /* first field */
315  avio_w8(pb, 4);
316  avio_wb32(pb, 0);
317 
318  /* last field */
319  avio_w8(pb, MAT_LAST_FIELD);
320  avio_w8(pb, 4);
321  avio_wb32(pb, gxf->nb_fields);
322 
323  /* reserved */
324  avio_w8(pb, MAT_MARK_IN);
325  avio_w8(pb, 4);
326  avio_wb32(pb, 0);
327 
328  avio_w8(pb, MAT_MARK_OUT);
329  avio_w8(pb, 4);
330  avio_wb32(pb, gxf->nb_fields);
331 
332  /* estimated size */
333  avio_w8(pb, MAT_SIZE);
334  avio_w8(pb, 4);
335  avio_wb32(pb, avio_size(pb) / 1024);
336 
337  return updateSize(pb, pos);
338 }
339 
341 {
342  GXFContext *gxf = s->priv_data;
343  AVIOContext *pb = s->pb;
344  int64_t pos;
345  int i;
346 
347  pos = avio_tell(pb);
348  avio_wb16(pb, 0); /* size */
349  for (i = 0; i < s->nb_streams; ++i)
351 
353 
354  return updateSize(pb, pos);
355 }
356 
357 static int gxf_write_map_packet(AVFormatContext *s, int rewrite)
358 {
359  GXFContext *gxf = s->priv_data;
360  AVIOContext *pb = s->pb;
361  int64_t pos = avio_tell(pb);
362 
363  if (!rewrite) {
364  if (!(gxf->map_offsets_nb % 30)) {
366  sizeof(*gxf->map_offsets),
367  gxf->map_offsets_nb+30);
368  if (!gxf->map_offsets) {
369  av_log(s, AV_LOG_ERROR, "could not realloc map offsets\n");
370  return -1;
371  }
372  }
373  gxf->map_offsets[gxf->map_offsets_nb++] = pos; // do not increment here
374  }
375 
377 
378  /* preamble */
379  avio_w8(pb, 0xE0); /* version */
380  avio_w8(pb, 0xFF); /* reserved */
381 
384 
385  return updatePacketSize(pb, pos);
386 }
387 
389 {
390  GXFContext *gxf = s->priv_data;
391  AVIOContext *pb = s->pb;
392  int64_t pos = avio_tell(pb);
393  int fields_per_flt = (gxf->nb_fields+1) / 1000 + 1;
394  int flt_entries = gxf->nb_fields / fields_per_flt;
395  int i = 0;
396 
398 
399  avio_wl32(pb, fields_per_flt); /* number of fields */
400  avio_wl32(pb, flt_entries); /* number of active flt entries */
401 
402  if (gxf->flt_entries) {
403  for (i = 0; i < flt_entries; i++)
404  avio_wl32(pb, gxf->flt_entries[(i*fields_per_flt)>>1]);
405  }
406 
407  for (; i < 1000; i++)
408  avio_wl32(pb, 0);
409 
410  return updatePacketSize(pb, pos);
411 }
412 
414 {
415  GXFContext *gxf = s->priv_data;
416  AVIOContext *pb = s->pb;
417  int timecode_base = gxf->time_base.den == 60000 ? 60 : 50;
418  int64_t timestamp = 0;
420  uint64_t nb_fields;
421  uint32_t timecode_in; // timecode at mark in
422  uint32_t timecode_out; // timecode at mark out
423 
424  if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
425  timestamp = ff_iso8601_to_unix_time(t->value);
426 
427  timecode_in = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
428  gxf->tc.hh, gxf->tc.mm,
429  gxf->tc.ss, gxf->tc.ff);
430 
431  nb_fields = gxf->nb_fields +
432  gxf->tc.hh * (timecode_base * 3600) +
433  gxf->tc.mm * (timecode_base * 60) +
434  gxf->tc.ss * timecode_base +
435  gxf->tc.ff;
436 
437  timecode_out = GXF_TIMECODE(gxf->tc.color, gxf->tc.drop,
438  nb_fields / (timecode_base * 3600) % 24,
439  nb_fields / (timecode_base * 60) % 60,
440  nb_fields / timecode_base % 60,
441  nb_fields % timecode_base);
442 
443  avio_wl32(pb, gxf->flags);
444  avio_wl32(pb, gxf->nb_fields); /* length of the longest track */
445  avio_wl32(pb, gxf->nb_fields); /* length of the shortest track */
446  avio_wl32(pb, 0); /* mark in */
447  avio_wl32(pb, gxf->nb_fields); /* mark out */
448  avio_wl32(pb, timecode_in); /* timecode mark in */
449  avio_wl32(pb, timecode_out); /* timecode mark out */
450  avio_wl64(pb, timestamp); /* modification time */
451  avio_wl64(pb, timestamp); /* creation time */
452  avio_wl16(pb, 0); /* reserved */
453  avio_wl16(pb, 0); /* reserved */
454  avio_wl16(pb, gxf->audio_tracks);
455  avio_wl16(pb, 1); /* timecode track count */
456  avio_wl16(pb, 0); /* reserved */
457  avio_wl16(pb, gxf->mpeg_tracks);
458  return 48;
459 }
460 
462 {
463  GXFContext *gxf = s->priv_data;
464  AVIOContext *pb = s->pb;
465 
466  avio_wl32(pb, gxf->umf_length); /* total length of the umf data */
467  avio_wl32(pb, 3); /* version */
468  avio_wl32(pb, s->nb_streams+1);
469  avio_wl32(pb, gxf->umf_track_offset); /* umf track section offset */
470  avio_wl32(pb, gxf->umf_track_size);
471  avio_wl32(pb, s->nb_streams+1);
472  avio_wl32(pb, gxf->umf_media_offset);
473  avio_wl32(pb, gxf->umf_media_size);
474  avio_wl32(pb, gxf->umf_length); /* user data offset */
475  avio_wl32(pb, 0); /* user data size */
476  avio_wl32(pb, 0); /* reserved */
477  avio_wl32(pb, 0); /* reserved */
478  return 48;
479 }
480 
482 {
483  AVIOContext *pb = s->pb;
484  GXFContext *gxf = s->priv_data;
485  int64_t pos = avio_tell(pb);
486  int i;
487 
488  gxf->umf_track_offset = pos - gxf->umf_start_offset;
489  for (i = 0; i < s->nb_streams; ++i) {
490  GXFStreamContext *sc = s->streams[i]->priv_data;
491  avio_wl16(pb, sc->media_info);
492  avio_wl16(pb, 1);
493  }
494 
496  avio_wl16(pb, 1);
497 
498  return avio_tell(pb) - pos;
499 }
500 
502 {
503  GXFStreamContext *sc = st->priv_data;
504 
505  if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P)
506  avio_wl32(pb, 2);
507  else
508  avio_wl32(pb, 1); /* default to 420 */
509  avio_wl32(pb, sc->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */
510  avio_wl32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */
511  avio_wl32(pb, 1); /* I picture per GOP */
512  avio_wl32(pb, sc->p_per_gop);
513  avio_wl32(pb, sc->b_per_i_or_p);
515  avio_wl32(pb, 2);
516  else if (st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO)
517  avio_wl32(pb, 1);
518  else
519  avio_wl32(pb, 0);
520  avio_wl32(pb, 0); /* reserved */
521  return 32;
522 }
523 
524 static int gxf_write_umf_media_timecode(AVIOContext *pb, int drop)
525 {
526  avio_wl32(pb, drop); /* drop frame */
527  avio_wl32(pb, 0); /* reserved */
528  avio_wl32(pb, 0); /* reserved */
529  avio_wl32(pb, 0); /* reserved */
530  avio_wl32(pb, 0); /* reserved */
531  avio_wl32(pb, 0); /* reserved */
532  avio_wl32(pb, 0); /* reserved */
533  avio_wl32(pb, 0); /* reserved */
534  return 32;
535 }
536 
538 {
539  int i;
540 
541  for (i = 0; i < 8; i++) {
542  avio_wb32(pb, 0);
543  }
544  return 32;
545 }
546 
548 {
549  avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
550  avio_wl64(pb, av_double2int(1)); /* sound level to begin to */
551  avio_wl32(pb, 0); /* number of fields over which to ramp up sound level */
552  avio_wl32(pb, 0); /* number of fields over which to ramp down sound level */
553  avio_wl32(pb, 0); /* reserved */
554  avio_wl32(pb, 0); /* reserved */
555  return 32;
556 }
557 
559 {
560  GXFContext *gxf = s->priv_data;
561  AVIOContext *pb = s->pb;
562  int64_t pos;
563  int i, j;
564 
565  pos = avio_tell(pb);
566  gxf->umf_media_offset = pos - gxf->umf_start_offset;
567  for (i = 0; i <= s->nb_streams; ++i) {
568  GXFStreamContext *sc;
569  int64_t startpos, curpos;
570 
571  if (i == s->nb_streams)
572  sc = &gxf->timecode_track;
573  else
574  sc = s->streams[i]->priv_data;
575 
576  startpos = avio_tell(pb);
577  avio_wl16(pb, 0); /* length */
578  avio_wl16(pb, sc->media_info);
579  avio_wl16(pb, 0); /* reserved */
580  avio_wl16(pb, 0); /* reserved */
581  avio_wl32(pb, gxf->nb_fields);
582  avio_wl32(pb, 0); /* attributes rw, ro */
583  avio_wl32(pb, 0); /* mark in */
584  avio_wl32(pb, gxf->nb_fields); /* mark out */
586  avio_wb16(pb, sc->media_info);
587  for (j = strlen(ES_NAME_PATTERN)+2; j < 88; j++)
588  avio_w8(pb, 0);
589  avio_wl32(pb, sc->track_type);
590  avio_wl32(pb, sc->sample_rate);
591  avio_wl32(pb, sc->sample_size);
592  avio_wl32(pb, 0); /* reserved */
593 
594  if (sc == &gxf->timecode_track)
596  else {
597  AVStream *st = s->streams[i];
598  switch (st->codec->codec_id) {
601  gxf_write_umf_media_mpeg(pb, st);
602  break;
605  break;
606  case AV_CODEC_ID_DVVIDEO:
607  gxf_write_umf_media_dv(pb, sc);
608  break;
609  }
610  }
611 
612  curpos = avio_tell(pb);
613  avio_seek(pb, startpos, SEEK_SET);
614  avio_wl16(pb, curpos - startpos);
615  avio_seek(pb, curpos, SEEK_SET);
616  }
617  return avio_tell(pb) - pos;
618 }
619 
621 {
622  GXFContext *gxf = s->priv_data;
623  AVIOContext *pb = s->pb;
624  int64_t pos = avio_tell(pb);
625 
627 
628  /* preamble */
629  avio_w8(pb, 3); /* first and last (only) packet */
630  avio_wb32(pb, gxf->umf_length); /* data length */
631 
632  gxf->umf_start_offset = avio_tell(pb);
637  gxf->umf_length = avio_tell(pb) - gxf->umf_start_offset;
638  return updatePacketSize(pb, pos);
639 }
640 
641 static const int GXF_samples_per_frame[] = { 32768, 0 };
642 
644 {
645  if (!vsc)
646  return;
647 
648  sc->media_type = vsc->sample_rate == 60 ? 7 : 8;
649  sc->sample_rate = vsc->sample_rate;
650  sc->media_info = ('T'<<8) | '0';
651  sc->track_type = 3;
653  sc->lines_index = vsc->lines_index;
654  sc->sample_size = 16;
655  sc->fields = vsc->fields;
656 }
657 
658 static int gxf_init_timecode(AVFormatContext *s, GXFTimecode *tc, const char *tcstr, int fields)
659 {
660  char c;
661 
662  if (sscanf(tcstr, "%d:%d:%d%c%d", &tc->hh, &tc->mm, &tc->ss, &c, &tc->ff) != 5) {
663  av_log(s, AV_LOG_ERROR, "unable to parse timecode, "
664  "syntax: hh:mm:ss[:;.]ff\n");
665  return -1;
666  }
667 
668  tc->color = 0;
669  tc->drop = c != ':';
670 
671  if (fields == 2)
672  tc->ff = tc->ff * 2;
673 
674  return 0;
675 }
676 
678 {
679  AVIOContext *pb = s->pb;
680  GXFContext *gxf = s->priv_data;
681  GXFStreamContext *vsc = NULL;
682  uint8_t tracks[255] = {0};
683  int i, media_info = 0;
684  AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
685 
686  if (!pb->seekable) {
687  av_log(s, AV_LOG_ERROR, "gxf muxer does not support streamed output, patch welcome\n");
688  return -1;
689  }
690 
691  gxf->flags |= 0x00080000; /* material is simple clip */
692  for (i = 0; i < s->nb_streams; ++i) {
693  AVStream *st = s->streams[i];
694  GXFStreamContext *sc = av_mallocz(sizeof(*sc));
695  if (!sc)
696  return AVERROR(ENOMEM);
697  st->priv_data = sc;
698 
699  sc->media_type = ff_codec_get_tag(gxf_media_types, st->codec->codec_id);
700  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
701  if (st->codec->codec_id != AV_CODEC_ID_PCM_S16LE) {
702  av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n");
703  return -1;
704  }
705  if (st->codec->sample_rate != 48000) {
706  av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n");
707  return -1;
708  }
709  if (st->codec->channels != 1) {
710  av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n");
711  return -1;
712  }
713  sc->track_type = 2;
714  sc->sample_rate = st->codec->sample_rate;
715  avpriv_set_pts_info(st, 64, 1, sc->sample_rate);
716  sc->sample_size = 16;
717  sc->frame_rate_index = -2;
718  sc->lines_index = -2;
719  sc->fields = -2;
720  gxf->audio_tracks++;
721  gxf->flags |= 0x04000000; /* audio is 16 bit pcm */
722  media_info = 'A';
723  } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
724  if (i != 0) {
725  av_log(s, AV_LOG_ERROR, "video stream must be the first track\n");
726  return -1;
727  }
728  /* FIXME check from time_base ? */
729  if (st->codec->height == 480 || st->codec->height == 512) { /* NTSC or NTSC+VBI */
730  sc->frame_rate_index = 5;
731  sc->sample_rate = 60;
732  gxf->flags |= 0x00000080;
733  gxf->time_base = (AVRational){ 1001, 60000 };
734  } else if (st->codec->height == 576 || st->codec->height == 608) { /* PAL or PAL+VBI */
735  sc->frame_rate_index = 6;
736  sc->media_type++;
737  sc->sample_rate = 50;
738  gxf->flags |= 0x00000040;
739  gxf->time_base = (AVRational){ 1, 50 };
740  } else {
741  av_log(s, AV_LOG_ERROR, "unsupported video resolution, "
742  "gxf muxer only accepts PAL or NTSC resolutions currently\n");
743  return -1;
744  }
745  if (!tcr)
746  tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
747  avpriv_set_pts_info(st, 64, gxf->time_base.num, gxf->time_base.den);
748  if (gxf_find_lines_index(st) < 0)
749  sc->lines_index = -1;
750  sc->sample_size = st->codec->bit_rate;
751  sc->fields = 2; /* interlaced */
752 
753  vsc = sc;
754 
755  switch (st->codec->codec_id) {
756  case AV_CODEC_ID_MJPEG:
757  sc->track_type = 1;
758  gxf->flags |= 0x00004000;
759  media_info = 'J';
760  break;
762  sc->track_type = 9;
763  gxf->mpeg_tracks++;
764  media_info = 'L';
765  break;
767  sc->first_gop_closed = -1;
768  sc->track_type = 4;
769  gxf->mpeg_tracks++;
770  gxf->flags |= 0x00008000;
771  media_info = 'M';
772  break;
773  case AV_CODEC_ID_DVVIDEO:
774  if (st->codec->pix_fmt == AV_PIX_FMT_YUV422P) {
775  sc->media_type += 2;
776  sc->track_type = 6;
777  gxf->flags |= 0x00002000;
778  media_info = 'E';
779  } else {
780  sc->track_type = 5;
781  gxf->flags |= 0x00001000;
782  media_info = 'D';
783  }
784  break;
785  default:
786  av_log(s, AV_LOG_ERROR, "video codec not supported\n");
787  return -1;
788  }
789  }
790  /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
791  sc->media_info = media_info<<8 | ('0'+tracks[media_info]++);
792  sc->order = s->nb_streams - st->index;
793  }
794 
795  if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0)
796  return -1;
797 
798  if (tcr && vsc)
799  gxf_init_timecode(s, &gxf->tc, tcr->value, vsc->fields);
800 
802  gxf->flags |= 0x200000; // time code track is non-drop frame
803 
804  gxf_write_map_packet(s, 0);
807 
808  gxf->packet_count = 3;
809 
810  avio_flush(pb);
811  return 0;
812 }
813 
815 {
816  int64_t pos = avio_tell(pb);
817 
819  return updatePacketSize(pb, pos);
820 }
821 
823 {
824  GXFContext *gxf = s->priv_data;
825  AVIOContext *pb = s->pb;
826  int64_t end;
827  int i;
828 
830 
832  end = avio_tell(pb);
833  avio_seek(pb, 0, SEEK_SET);
834  /* overwrite map, flt and umf packets with new values */
835  gxf_write_map_packet(s, 1);
838  avio_flush(pb);
839  /* update duration in all map packets */
840  for (i = 1; i < gxf->map_offsets_nb; i++) {
841  avio_seek(pb, gxf->map_offsets[i], SEEK_SET);
842  gxf_write_map_packet(s, 1);
843  avio_flush(pb);
844  }
845 
846  avio_seek(pb, end, SEEK_SET);
847 
848  av_freep(&gxf->flt_entries);
849  av_freep(&gxf->map_offsets);
850 
851  return 0;
852 }
853 
854 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size)
855 {
856  uint32_t c=-1;
857  int i;
858  for(i=0; i<size-4 && c!=0x100; i++){
859  c = (c<<8) + buf[i];
860  if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */
861  sc->first_gop_closed= (buf[i+4]>>6)&1;
862  }
863  return (buf[i+1]>>3)&7;
864 }
865 
867 {
868  GXFContext *gxf = s->priv_data;
869  AVIOContext *pb = s->pb;
870  AVStream *st = s->streams[pkt->stream_index];
871  GXFStreamContext *sc = st->priv_data;
872  unsigned field_nb;
873  /* If the video is frame-encoded, the frame numbers shall be represented by
874  * even field numbers.
875  * see SMPTE360M-2004 6.4.2.1.3 Media field number */
876  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
877  field_nb = gxf->nb_fields;
878  } else {
879  field_nb = av_rescale_rnd(pkt->dts, gxf->time_base.den,
880  (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
881  }
882 
883  avio_w8(pb, sc->media_type);
884  avio_w8(pb, st->index);
885  avio_wb32(pb, field_nb);
886  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
887  avio_wb16(pb, 0);
888  avio_wb16(pb, size / 2);
889  } else if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
890  int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size);
891  if (frame_type == AV_PICTURE_TYPE_I) {
892  avio_w8(pb, 0x0d);
893  sc->iframes++;
894  } else if (frame_type == AV_PICTURE_TYPE_B) {
895  avio_w8(pb, 0x0f);
896  sc->bframes++;
897  } else {
898  avio_w8(pb, 0x0e);
899  sc->pframes++;
900  }
901  avio_wb24(pb, size);
902  } else if (st->codec->codec_id == AV_CODEC_ID_DVVIDEO) {
903  avio_w8(pb, size / 4096);
904  avio_wb24(pb, 0);
905  } else
906  avio_wb32(pb, size);
907  avio_wb32(pb, field_nb);
908  avio_w8(pb, 1); /* flags */
909  avio_w8(pb, 0); /* reserved */
910  return 16;
911 }
912 
914 {
915  GXFContext *gxf = s->priv_data;
916  AVIOContext *pb = s->pb;
917  AVStream *st = s->streams[pkt->stream_index];
918  int64_t pos = avio_tell(pb);
919  int padding = 0;
920  int packet_start_offset = avio_tell(pb) / 1024;
921 
923  if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */
924  padding = 4 - pkt->size % 4;
925  else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
926  padding = GXF_AUDIO_PACKET_SIZE - pkt->size;
927  gxf_write_media_preamble(s, pkt, pkt->size + padding);
928  avio_write(pb, pkt->data, pkt->size);
929  gxf_write_padding(pb, padding);
930 
931  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
932  if (!(gxf->flt_entries_nb % 500)) {
934  sizeof(*gxf->flt_entries),
935  gxf->flt_entries_nb+500);
936  if (!gxf->flt_entries) {
937  av_log(s, AV_LOG_ERROR, "could not reallocate flt entries\n");
938  return -1;
939  }
940  }
941  gxf->flt_entries[gxf->flt_entries_nb++] = packet_start_offset;
942  gxf->nb_fields += 2; // count fields
943  }
944 
945  updatePacketSize(pb, pos);
946 
947  gxf->packet_count++;
948  if (gxf->packet_count == 100) {
949  gxf_write_map_packet(s, 0);
950  gxf->packet_count = 0;
951  }
952 
953  avio_flush(pb);
954 
955  return 0;
956 }
957 
959 {
960  GXFContext *gxf = s->priv_data;
961  AVPacket *pkt[2] = { cur, next };
962  int i, field_nb[2];
963  GXFStreamContext *sc[2];
964 
965  for (i = 0; i < 2; i++) {
966  AVStream *st = s->streams[pkt[i]->stream_index];
967  sc[i] = st->priv_data;
968  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
969  field_nb[i] = av_rescale_rnd(pkt[i]->dts, gxf->time_base.den,
970  (int64_t)48000*gxf->time_base.num, AV_ROUND_UP);
971  field_nb[i] &= ~1; // compare against even field number because audio must be before video
972  } else
973  field_nb[i] = pkt[i]->dts; // dts are field based
974  }
975 
976  return field_nb[1] > field_nb[0] ||
977  (field_nb[1] == field_nb[0] && sc[1]->order > sc[0]->order);
978 }
979 
981 {
982  if (pkt && s->streams[pkt->stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
983  pkt->duration = 2; // enforce 2 fields
984  return ff_audio_rechunk_interleave(s, out, pkt, flush,
986 }
987 
989  .name = "gxf",
990  .long_name = NULL_IF_CONFIG_SMALL("GXF (General eXchange Format)"),
991  .extensions = "gxf",
992  .priv_data_size = sizeof(GXFContext),
993  .audio_codec = AV_CODEC_ID_PCM_S16LE,
994  .video_codec = AV_CODEC_ID_MPEG2VIDEO,
999 };