Mercurial > libavformat.hg
annotate gxfenc.c @ 1348:c23cce23abb2 libavformat
fix audio for mog_op1a_prompeg_mpeg.mxf
author | bcoudurier |
---|---|
date | Mon, 02 Oct 2006 14:12:50 +0000 |
parents | 95f56c7b24eb |
children | 0899bfe4105c |
rev | line source |
---|---|
1183 | 1 /* |
2 * GXF muxer. | |
3 * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>. | |
4 * | |
5 * This library is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 */ | |
19 | |
20 #include "avformat.h" | |
21 #include "gxf.h" | |
22 #include "riff.h" | |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
23 #include "fifo.h" |
1183 | 24 |
25 #define GXF_AUDIO_PACKET_SIZE 65536 | |
26 | |
27 typedef struct GXFStreamContext { | |
28 AVCodecContext *codec; | |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
29 AVFifoBuffer audio_buffer; |
1183 | 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_gop; | |
1261 | 44 int first_gop_closed; |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
45 int64_t current_dts; |
1183 | 46 } GXFStreamContext; |
47 | |
48 typedef struct GXFContext { | |
49 uint32_t nb_frames; | |
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 GXFStreamContext streams[48]; | |
67 } GXFContext; | |
68 | |
69 typedef struct GXF_Lines { | |
70 int height; | |
71 int index; | |
72 } GXF_Lines; | |
73 | |
74 | |
75 /* FIXME check if it is relevant */ | |
76 static const GXF_Lines gxf_lines_tab[] = { | |
77 { 480, 1 }, /* NTSC */ | |
78 { 512, 1 }, /* NTSC + VBI */ | |
79 { 576, 2 }, /* PAL */ | |
80 { 608, 2 }, /* PAL + VBI */ | |
81 { 1080, 4 }, | |
82 { 720, 6 }, | |
83 }; | |
84 | |
85 static const CodecTag gxf_media_types[] = { | |
86 { CODEC_ID_MJPEG , 3 }, /* NTSC */ | |
87 { CODEC_ID_MJPEG , 4 }, /* PAL */ | |
88 { CODEC_ID_PCM_S24LE , 9 }, | |
89 { CODEC_ID_PCM_S16LE , 10 }, | |
90 { CODEC_ID_MPEG2VIDEO, 11 }, /* NTSC */ | |
91 { CODEC_ID_MPEG2VIDEO, 12 }, /* PAL */ | |
92 { CODEC_ID_DVVIDEO , 13 }, /* NTSC */ | |
93 { CODEC_ID_DVVIDEO , 14 }, /* PAL */ | |
94 { CODEC_ID_DVVIDEO , 15 }, /* 50M NTSC */ | |
95 { CODEC_ID_DVVIDEO , 16 }, /* 50M PAL */ | |
96 { CODEC_ID_AC3 , 17 }, | |
97 //{ CODEC_ID_NONE, , 18 }, /* Non compressed 24 bit audio */ | |
98 { CODEC_ID_MPEG2VIDEO, 20 }, /* MPEG HD */ | |
99 { CODEC_ID_MPEG1VIDEO, 22 }, /* NTSC */ | |
100 { CODEC_ID_MPEG1VIDEO, 23 }, /* PAL */ | |
101 { 0, 0 }, | |
102 }; | |
103 | |
104 #define SERVER_PATH "/space/" | |
105 #define ES_NAME_PATTERN "ES." | |
106 | |
107 static int gxf_find_lines_index(GXFStreamContext *ctx) | |
108 { | |
109 int i; | |
110 | |
111 for (i = 0; i < 6; ++i) { | |
112 if (ctx->codec->height == gxf_lines_tab[i].height) { | |
113 ctx->lines_index = gxf_lines_tab[i].index; | |
114 return 0; | |
115 } | |
116 } | |
117 return -1; | |
118 } | |
119 | |
120 static void gxf_write_padding(ByteIOContext *pb, offset_t to_pad) | |
121 { | |
1213 | 122 for (; to_pad > 0; to_pad--) { |
1183 | 123 put_byte(pb, 0); |
124 } | |
125 } | |
126 | |
127 static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos) | |
128 { | |
129 offset_t curpos; | |
130 int size; | |
131 | |
132 size = url_ftell(pb) - pos; | |
133 if (size % 4) { | |
134 gxf_write_padding(pb, 4 - size % 4); | |
135 size = url_ftell(pb) - pos; | |
136 } | |
137 curpos = url_ftell(pb); | |
138 url_fseek(pb, pos + 6, SEEK_SET); | |
139 put_be32(pb, size); | |
140 url_fseek(pb, curpos, SEEK_SET); | |
141 return curpos - pos; | |
142 } | |
143 | |
144 static offset_t updateSize(ByteIOContext *pb, offset_t pos) | |
145 { | |
146 offset_t curpos; | |
147 | |
148 curpos = url_ftell(pb); | |
149 url_fseek(pb, pos, SEEK_SET); | |
150 put_be16(pb, curpos - pos - 2); | |
151 url_fseek(pb, curpos, SEEK_SET); | |
152 return curpos - pos; | |
153 } | |
154 | |
155 static void gxf_write_packet_header(ByteIOContext *pb, pkt_type_t type) | |
156 { | |
157 put_be32(pb, 0); /* packet leader for synchro */ | |
158 put_byte(pb, 1); | |
159 put_byte(pb, type); /* map packet */ | |
160 put_be32(pb, 0); /* size */ | |
161 put_be32(pb, 0); /* reserved */ | |
162 put_byte(pb, 0xE1); /* trailer 1 */ | |
163 put_byte(pb, 0xE2); /* trailer 2 */ | |
164 } | |
165 | |
166 static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx) | |
167 { | |
168 char buffer[1024]; | |
169 int size; | |
170 | |
171 if (ctx->iframes) { | |
172 ctx->p_per_gop = ctx->pframes / ctx->iframes; | |
173 if (ctx->pframes % ctx->iframes) | |
174 ctx->p_per_gop++; | |
175 if (ctx->pframes) | |
176 ctx->b_per_gop = ctx->bframes / ctx->pframes; | |
177 if (ctx->p_per_gop > 9) | |
178 ctx->p_per_gop = 9; /* ensure value won't take more than one char */ | |
179 if (ctx->b_per_gop > 9) | |
180 ctx->b_per_gop = 9; /* ensure value won't take more than one char */ | |
181 } | |
182 size = snprintf(buffer, 1024, "Ver 1\nBr %.6f\nIpg 1\nPpi %d\nBpiop %d\n" | |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
183 "Pix 0\nCf %d\nCg %d\nSl 7\nnl16 %d\nVi 1\nf1 1\n", |
1183 | 184 (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop, |
1262 | 185 ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, ctx->first_gop_closed == 1, |
1183 | 186 ctx->codec->height / 16); |
187 put_byte(pb, 0x4F); | |
188 put_byte(pb, size + 1); | |
189 put_buffer(pb, (uint8_t *)buffer, size + 1); | |
190 return size + 3; | |
191 } | |
192 | |
193 static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx) | |
194 { | |
195 /* FIXME implement that */ | |
196 put_byte(pb, 0); /* fields */ | |
197 put_byte(pb, 0); /* seconds */ | |
198 put_byte(pb, 0); /* minutes */ | |
199 put_byte(pb, 0); /* flags + hours */ | |
200 /* reserved */ | |
201 put_be32(pb, 0); | |
202 return 8; | |
203 } | |
204 | |
205 static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream) | |
206 { | |
207 offset_t pos; | |
208 | |
209 /* track description section */ | |
210 put_byte(pb, stream->media_type + 0x80); | |
211 put_byte(pb, stream->index + 0xC0); | |
212 | |
213 pos = url_ftell(pb); | |
214 put_be16(pb, 0); /* size */ | |
215 | |
216 /* media file name */ | |
217 put_byte(pb, 0x4C); | |
218 put_byte(pb, strlen(ES_NAME_PATTERN) + 3); | |
219 put_tag(pb, ES_NAME_PATTERN); | |
220 put_be16(pb, stream->media_info); | |
221 put_byte(pb, 0); | |
222 | |
223 if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) { | |
224 /* auxiliary information */ | |
225 put_byte(pb, 0x4D); | |
226 put_byte(pb, 8); | |
227 if (stream->codec->codec_id == CODEC_ID_NONE) | |
228 gxf_write_timecode_auxiliary(pb, stream); | |
229 else | |
230 put_le64(pb, 0); | |
231 } | |
232 | |
233 /* file system version */ | |
234 put_byte(pb, 0x4E); | |
235 put_byte(pb, 4); | |
236 put_be32(pb, 0); | |
237 | |
238 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO) | |
239 gxf_write_mpeg_auxiliary(pb, stream); | |
240 | |
241 /* frame rate */ | |
242 put_byte(pb, 0x50); | |
243 put_byte(pb, 4); | |
244 put_be32(pb, stream->frame_rate_index); | |
245 | |
246 /* lines per frame */ | |
247 put_byte(pb, 0x51); | |
248 put_byte(pb, 4); | |
249 put_be32(pb, stream->lines_index); | |
250 | |
251 /* fields per frame */ | |
252 put_byte(pb, 0x52); | |
253 put_byte(pb, 4); | |
254 put_be32(pb, stream->fields); | |
255 | |
256 return updateSize(pb, pos); | |
257 } | |
258 | |
259 static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx) | |
260 { | |
261 offset_t pos; | |
262 const char *filename = strrchr(ctx->fc->filename, '/'); | |
263 | |
264 pos = url_ftell(pb); | |
265 put_be16(pb, 0); /* size */ | |
266 | |
267 /* name */ | |
268 if (filename) | |
269 filename++; | |
270 else | |
271 filename = ctx->fc->filename; | |
272 put_byte(pb, 0x40); | |
273 put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1); | |
274 put_tag(pb, SERVER_PATH); | |
275 put_tag(pb, filename); | |
276 put_byte(pb, 0); | |
277 | |
278 /* first field */ | |
279 put_byte(pb, 0x41); | |
280 put_byte(pb, 4); | |
281 put_be32(pb, 0); | |
282 | |
283 /* last field */ | |
284 put_byte(pb, 0x42); | |
285 put_byte(pb, 4); | |
286 put_be32(pb, ctx->nb_frames); | |
287 | |
288 /* reserved */ | |
289 put_byte(pb, 0x43); | |
290 put_byte(pb, 4); | |
291 put_be32(pb, 0); | |
292 | |
293 put_byte(pb, 0x44); | |
294 put_byte(pb, 4); | |
295 put_be32(pb, ctx->nb_frames); | |
296 | |
297 /* estimated size */ | |
298 put_byte(pb, 0x45); | |
299 put_byte(pb, 4); | |
300 put_be32(pb, url_fsize(pb) / 1024); | |
301 | |
302 return updateSize(pb, pos); | |
303 } | |
304 | |
305 static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx) | |
306 { | |
307 offset_t pos; | |
308 int i; | |
309 | |
310 pos = url_ftell(pb); | |
311 put_be16(pb, 0); /* size */ | |
312 for (i = 0; i < ctx->fc->nb_streams; ++i) | |
313 gxf_write_track_description(pb, &ctx->streams[i]); | |
314 return updateSize(pb, pos); | |
315 } | |
316 | |
317 static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx) | |
318 { | |
319 offset_t pos = url_ftell(pb); | |
320 | |
321 gxf_write_packet_header(pb, PKT_MAP); | |
322 | |
323 /* preamble */ | |
324 put_byte(pb, 0xE0); /* version */ | |
325 put_byte(pb, 0xFF); /* reserved */ | |
326 | |
327 gxf_write_material_data_section(pb, ctx); | |
328 gxf_write_track_description_section(pb, ctx); | |
329 | |
330 return updatePacketSize(pb, pos); | |
331 } | |
332 | |
333 #if 0 | |
334 static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx) | |
335 { | |
336 offset_t pos = url_ftell(pb); | |
337 int i; | |
338 | |
339 gxf_write_packet_header(pb, PKT_FLT); | |
340 | |
341 put_le32(pb, 1000); /* number of fields */ | |
342 put_le32(pb, 0); /* number of active flt entries */ | |
343 | |
344 for (i = 0; i < 1000; ++i) { | |
345 put_le32(pb, 0); | |
346 } | |
347 return updatePacketSize(pb, pos); | |
348 } | |
349 #endif | |
350 | |
351 static int gxf_write_umf_material_description(ByteIOContext *pb, GXFContext *ctx) | |
352 { | |
353 put_le32(pb, ctx->flags); | |
354 put_le32(pb, ctx->nb_frames); /* length of the longest track */ | |
355 put_le32(pb, ctx->nb_frames); /* length of the shortest track */ | |
356 put_le32(pb, 0); /* mark in */ | |
357 put_le32(pb, ctx->nb_frames); /* mark out */ | |
358 put_le32(pb, 0); /* timecode mark in */ | |
359 put_le32(pb, ctx->nb_frames); /* timecode mark out */ | |
360 put_le64(pb, ctx->fc->timestamp); /* modification time */ | |
361 put_le64(pb, ctx->fc->timestamp); /* creation time */ | |
362 put_le16(pb, 0); /* reserved */ | |
363 put_le16(pb, 0); /* reserved */ | |
364 put_le16(pb, ctx->audio_tracks); | |
365 put_le16(pb, 0); /* timecode track count */ | |
366 put_le16(pb, 0); /* reserved */ | |
367 put_le16(pb, ctx->mpeg_tracks); | |
368 return 48; | |
369 } | |
370 | |
371 static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx) | |
372 { | |
373 put_le32(pb, ctx->umf_length); /* total length of the umf data */ | |
374 put_le32(pb, 3); /* version */ | |
375 put_le32(pb, ctx->fc->nb_streams); | |
376 put_le32(pb, ctx->umf_track_offset); /* umf track section offset */ | |
377 put_le32(pb, ctx->umf_track_size); | |
378 put_le32(pb, ctx->fc->nb_streams); | |
379 put_le32(pb, ctx->umf_media_offset); | |
380 put_le32(pb, ctx->umf_media_size); | |
381 put_le32(pb, ctx->umf_user_data_offset); /* user data offset */ | |
382 put_le32(pb, ctx->umf_user_data_size); /* user data size */ | |
383 put_le32(pb, 0); /* reserved */ | |
384 put_le32(pb, 0); /* reserved */ | |
385 return 48; | |
386 } | |
387 | |
388 static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx) | |
389 { | |
390 offset_t pos = url_ftell(pb); | |
391 int tracks[255]={0}; | |
392 int i; | |
393 | |
394 ctx->umf_track_offset = pos - ctx->umf_start_offset; | |
395 for (i = 0; i < ctx->fc->nb_streams; ++i) { | |
396 AVStream *st = ctx->fc->streams[i]; | |
397 GXFStreamContext *sc = &ctx->streams[i]; | |
398 int id = 0; | |
399 | |
400 switch (st->codec->codec_id) { | |
401 case CODEC_ID_MPEG1VIDEO: id= 'L'; break; | |
402 case CODEC_ID_MPEG2VIDEO: id= 'M'; break; | |
403 case CODEC_ID_PCM_S16LE: id= 'A'; break; | |
404 case CODEC_ID_DVVIDEO: id= sc->track_type == 6 ? 'E' : 'D'; break; | |
405 case CODEC_ID_MJPEG: id= 'V'; break; | |
406 } | |
407 sc->media_info= id << 8; | |
408 /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */ | |
409 sc->media_info |= '0' + (tracks[id]++); | |
410 put_le16(pb, sc->media_info); | |
411 put_le16(pb, 1); | |
412 } | |
413 return url_ftell(pb) - pos; | |
414 } | |
415 | |
416 static int gxf_write_umf_media_mpeg(ByteIOContext *pb, GXFStreamContext *stream) | |
417 { | |
418 if (stream->codec->pix_fmt == PIX_FMT_YUV422P) | |
419 put_le32(pb, 2); | |
420 else | |
421 put_le32(pb, 1); /* default to 420 */ | |
1262 | 422 put_le32(pb, stream->first_gop_closed == 1); /* closed = 1, open = 0, unknown = 255 */ |
1183 | 423 put_le32(pb, 3); /* top = 1, bottom = 2, frame = 3, unknown = 0 */ |
424 put_le32(pb, 1); /* I picture per GOP */ | |
425 put_le32(pb, stream->p_per_gop); | |
426 put_le32(pb, stream->b_per_gop); | |
427 if (stream->codec->codec_id == CODEC_ID_MPEG2VIDEO) | |
428 put_le32(pb, 2); | |
429 else if (stream->codec->codec_id == CODEC_ID_MPEG1VIDEO) | |
430 put_le32(pb, 1); | |
431 else | |
432 put_le32(pb, 0); | |
433 put_le32(pb, 0); /* reserved */ | |
434 return 32; | |
435 } | |
436 | |
437 static int gxf_write_umf_media_timecode(ByteIOContext *pb, GXFStreamContext *track) | |
438 { | |
439 /* FIXME implement */ | |
440 put_be32(pb, 0); /* drop frame flag */ | |
441 put_be32(pb, 0); /* reserved */ | |
442 put_be32(pb, 0); /* reserved */ | |
443 put_be32(pb, 0); /* reserved */ | |
444 put_be32(pb, 0); /* reserved */ | |
445 put_be32(pb, 0); /* reserved */ | |
446 put_be32(pb, 0); /* reserved */ | |
447 put_be32(pb, 0); /* reserved */ | |
448 return 32; | |
449 } | |
450 | |
451 static int gxf_write_umf_media_dv(ByteIOContext *pb, GXFStreamContext *track) | |
452 { | |
453 int i; | |
454 | |
455 for (i = 0; i < 8; i++) { | |
456 put_be32(pb, 0); | |
457 } | |
458 return 32; | |
459 } | |
460 | |
461 static int gxf_write_umf_media_audio(ByteIOContext *pb, GXFStreamContext *track) | |
462 { | |
463 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */ | |
464 put_le64(pb, av_dbl2int(1)); /* sound level to begin to */ | |
465 put_le32(pb, 0); /* number of fields over which to ramp up sound level */ | |
466 put_le32(pb, 0); /* number of fields over which to ramp down sound level */ | |
467 put_le32(pb, 0); /* reserved */ | |
468 put_le32(pb, 0); /* reserved */ | |
469 return 32; | |
470 } | |
471 | |
472 #if 0 | |
473 static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track) | |
474 { | |
475 put_be64(pb, 0); /* FIXME FLOAT max chroma quant level */ | |
476 put_be64(pb, 0); /* FIXME FLOAT max luma quant level */ | |
477 put_be64(pb, 0); /* FIXME FLOAT min chroma quant level */ | |
478 put_be64(pb, 0); /* FIXME FLOAT min luma quant level */ | |
479 return 32; | |
480 } | |
481 #endif | |
482 | |
483 static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx) | |
484 { | |
485 offset_t pos; | |
486 int i; | |
487 | |
488 pos = url_ftell(pb); | |
489 ctx->umf_media_offset = pos - ctx->umf_start_offset; | |
490 for (i = 0; i < ctx->fc->nb_streams; ++i) { | |
491 GXFStreamContext *sc = &ctx->streams[i]; | |
492 char buffer[88]; | |
493 offset_t startpos, curpos; | |
494 int path_size = strlen(ES_NAME_PATTERN); | |
495 | |
1260 | 496 memset(buffer, 0, 88); |
1183 | 497 startpos = url_ftell(pb); |
498 put_le16(pb, 0); /* length */ | |
499 put_le16(pb, sc->media_info); | |
500 put_le16(pb, 0); /* reserved */ | |
501 put_le16(pb, 0); /* reserved */ | |
502 put_le32(pb, ctx->nb_frames); | |
503 put_le32(pb, 0); /* attributes rw, ro */ | |
504 put_le32(pb, 0); /* mark in */ | |
505 put_le32(pb, ctx->nb_frames); /* mark out */ | |
506 strncpy(buffer, ES_NAME_PATTERN, path_size); | |
507 put_buffer(pb, (uint8_t *)buffer, path_size); | |
508 put_be16(pb, sc->media_info); | |
509 put_buffer(pb, (uint8_t *)buffer + path_size + 2, 88 - path_size - 2); | |
510 put_le32(pb, sc->track_type); | |
511 put_le32(pb, sc->sample_rate); | |
512 put_le32(pb, sc->sample_size); | |
513 put_le32(pb, 0); /* reserved */ | |
514 switch (sc->codec->codec_id) { | |
515 case CODEC_ID_MPEG2VIDEO: | |
516 gxf_write_umf_media_mpeg(pb, sc); | |
517 break; | |
518 case CODEC_ID_PCM_S16LE: | |
519 gxf_write_umf_media_audio(pb, sc); | |
520 break; | |
521 case CODEC_ID_DVVIDEO: | |
522 gxf_write_umf_media_dv(pb, sc); | |
523 break; | |
524 default: | |
525 gxf_write_umf_media_timecode(pb, sc); /* 8 0bytes */ | |
526 } | |
527 curpos = url_ftell(pb); | |
528 url_fseek(pb, startpos, SEEK_SET); | |
529 put_le16(pb, curpos - startpos); | |
530 url_fseek(pb, curpos, SEEK_SET); | |
531 } | |
532 return url_ftell(pb) - pos; | |
533 } | |
534 | |
535 static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx) | |
536 { | |
537 offset_t pos = url_ftell(pb); | |
538 ctx->umf_user_data_offset = pos - ctx->umf_start_offset; | |
539 put_le32(pb, 20); | |
540 put_le32(pb, 0); | |
541 put_le16(pb, 0); | |
542 put_le16(pb, 0); | |
543 put_le32(pb, 0); | |
544 put_byte(pb, 0); | |
545 put_byte(pb, 0); | |
546 put_byte(pb, 0); | |
547 put_byte(pb, 0); | |
548 return 20; | |
549 } | |
550 | |
551 static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx) | |
552 { | |
553 offset_t pos = url_ftell(pb); | |
554 | |
555 gxf_write_packet_header(pb, PKT_UMF); | |
556 | |
557 /* preamble */ | |
558 put_byte(pb, 3); /* first and last (only) packet */ | |
559 put_be32(pb, ctx->umf_length); /* data length */ | |
560 | |
561 ctx->umf_start_offset = url_ftell(pb); | |
562 gxf_write_umf_payload(pb, ctx); | |
563 gxf_write_umf_material_description(pb, ctx); | |
564 ctx->umf_track_size = gxf_write_umf_track_description(pb, ctx); | |
565 ctx->umf_media_size = gxf_write_umf_media_description(pb, ctx); | |
566 ctx->umf_user_data_size = gxf_write_umf_user_data(pb, ctx); | |
567 ctx->umf_length = url_ftell(pb) - ctx->umf_start_offset; | |
568 return updatePacketSize(pb, pos); | |
569 } | |
570 | |
571 static int gxf_write_header(AVFormatContext *s) | |
572 { | |
573 ByteIOContext *pb = &s->pb; | |
574 GXFContext *gxf = s->priv_data; | |
575 int i; | |
576 | |
577 gxf->fc = s; | |
578 gxf->flags |= 0x00080000; /* material is simple clip */ | |
579 for (i = 0; i < s->nb_streams; ++i) { | |
580 AVStream *st = s->streams[i]; | |
581 GXFStreamContext *sc = &gxf->streams[i]; | |
582 | |
583 sc->codec = st->codec; | |
584 sc->index = i; | |
585 sc->media_type = codec_get_tag(gxf_media_types, sc->codec->codec_id); | |
586 if (st->codec->codec_type == CODEC_TYPE_AUDIO) { | |
587 if (st->codec->codec_id != CODEC_ID_PCM_S16LE) { | |
588 av_log(s, AV_LOG_ERROR, "only 16 BIT PCM LE allowed for now\n"); | |
589 return -1; | |
590 } | |
591 if (st->codec->sample_rate != 48000) { | |
592 av_log(s, AV_LOG_ERROR, "only 48000hz sampling rate is allowed\n"); | |
593 return -1; | |
594 } | |
595 if (st->codec->channels != 1) { | |
596 av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n"); | |
597 return -1; | |
598 } | |
599 sc->track_type = 2; | |
600 sc->sample_rate = st->codec->sample_rate; | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
601 av_set_pts_info(st, 64, 1, sc->sample_rate); |
1183 | 602 sc->sample_size = 16; |
603 sc->frame_rate_index = -2; | |
604 sc->lines_index = -2; | |
605 sc->fields = -2; | |
606 gxf->audio_tracks++; | |
607 gxf->flags |= 0x04000000; /* audio is 16 bit pcm */ | |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
608 av_fifo_init(&sc->audio_buffer, 3*GXF_AUDIO_PACKET_SIZE); |
1183 | 609 } else if (sc->codec->codec_type == CODEC_TYPE_VIDEO) { |
610 /* FIXME check from time_base ? */ | |
611 if (sc->codec->height == 480 || sc->codec->height == 512) { /* NTSC or NTSC+VBI */ | |
612 sc->frame_rate_index = 5; | |
613 sc->sample_rate = 60; | |
614 gxf->flags |= 0x00000080; | |
615 } else { /* assume PAL */ | |
616 sc->frame_rate_index = 6; | |
617 sc->media_type++; | |
618 sc->sample_rate = 50; | |
619 gxf->flags |= 0x00000040; | |
620 } | |
621 gxf->sample_rate = sc->sample_rate; | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
622 av_set_pts_info(st, 64, 1, sc->sample_rate); |
1183 | 623 if (gxf_find_lines_index(sc) < 0) |
624 sc->lines_index = -1; | |
625 sc->sample_size = st->codec->bit_rate; | |
626 sc->fields = 2; /* interlaced */ | |
627 switch (sc->codec->codec_id) { | |
628 case CODEC_ID_MPEG2VIDEO: | |
1262 | 629 sc->first_gop_closed = -1; |
1183 | 630 sc->track_type = 4; |
631 gxf->mpeg_tracks++; | |
632 gxf->flags |= 0x00008000; | |
633 break; | |
634 case CODEC_ID_DVVIDEO: | |
635 if (sc->codec->pix_fmt == PIX_FMT_YUV422P) { | |
636 sc->media_type += 2; | |
637 sc->track_type = 6; | |
638 gxf->flags |= 0x00002000; | |
639 } else { | |
640 sc->track_type = 5; | |
641 gxf->flags |= 0x00001000; | |
642 } | |
643 break; | |
644 default: | |
645 av_log(NULL, AV_LOG_ERROR, "video codec not supported\n"); | |
646 return -1; | |
647 } | |
648 } | |
649 } | |
650 gxf_write_map_packet(pb, gxf); | |
651 //gxf_write_flt_packet(pb, gxf); | |
652 gxf_write_umf_packet(pb, gxf); | |
653 put_flush_packet(pb); | |
654 return 0; | |
655 } | |
656 | |
657 static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx) | |
658 { | |
659 offset_t pos = url_ftell(pb); | |
660 | |
661 gxf_write_packet_header(pb, PKT_EOS); | |
662 return updatePacketSize(pb, pos); | |
663 } | |
664 | |
665 static int gxf_write_trailer(AVFormatContext *s) | |
666 { | |
667 ByteIOContext *pb = &s->pb; | |
668 GXFContext *gxf = s->priv_data; | |
669 offset_t end; | |
670 int i; | |
671 | |
672 for (i = 0; i < s->nb_streams; ++i) { | |
673 if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) { | |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
674 av_fifo_free(&gxf->streams[i].audio_buffer); |
1183 | 675 } |
676 if (s->streams[i]->codec->frame_number > gxf->nb_frames) | |
677 gxf->nb_frames = 2 * s->streams[i]->codec->frame_number; | |
678 } | |
679 | |
680 gxf_write_eos_packet(pb, gxf); | |
681 end = url_ftell(pb); | |
682 url_fseek(pb, 0, SEEK_SET); | |
683 /* overwrite map and umf packets with new values */ | |
684 gxf_write_map_packet(pb, gxf); | |
685 //gxf_write_flt_packet(pb, gxf); | |
686 gxf_write_umf_packet(pb, gxf); | |
687 url_fseek(pb, end, SEEK_SET); | |
688 return 0; | |
689 } | |
690 | |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
691 static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int size) |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
692 { |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
693 uint32_t c=-1; |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
694 int i; |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
695 for(i=0; i<size-4 && c!=0x100; i++){ |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
696 c = (c<<8) + buf[i]; |
1262 | 697 if(c == 0x1B8 && sc->first_gop_closed == -1) /* GOP start code */ |
1261 | 698 sc->first_gop_closed= (buf[i+4]>>6)&1; |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
699 } |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
700 return (buf[i+1]>>3)&7; |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
701 } |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
702 |
1183 | 703 static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size) |
704 { | |
705 GXFStreamContext *sc = &ctx->streams[pkt->stream_index]; | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
706 int64_t dts = av_rescale(pkt->dts, ctx->sample_rate, sc->sample_rate); |
1183 | 707 |
708 put_byte(pb, sc->media_type); | |
709 put_byte(pb, sc->index); | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
710 put_be32(pb, dts); |
1183 | 711 if (sc->codec->codec_type == CODEC_TYPE_AUDIO) { |
712 put_be16(pb, 0); | |
713 put_be16(pb, size / 2); | |
714 } else if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO) { | |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
715 int frame_type = gxf_parse_mpeg_frame(sc, pkt->data, pkt->size); |
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
716 if (frame_type == FF_I_TYPE) { |
1183 | 717 put_byte(pb, 0x0d); |
718 sc->iframes++; | |
1247
2f118b3e65c6
parse mpeg frame to get pict type and closed gop flag
bcoudurier
parents:
1213
diff
changeset
|
719 } else if (frame_type == FF_B_TYPE) { |
1183 | 720 put_byte(pb, 0x0f); |
721 sc->bframes++; | |
722 } else { | |
723 put_byte(pb, 0x0e); | |
724 sc->pframes++; | |
725 } | |
726 put_be24(pb, size); | |
727 } else if (sc->codec->codec_id == CODEC_ID_DVVIDEO) { | |
728 put_byte(pb, size / 4096); | |
729 put_be24(pb, 0); | |
730 } else | |
731 put_be32(pb, size); | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
732 put_be32(pb, dts); |
1183 | 733 put_byte(pb, 1); /* flags */ |
734 put_byte(pb, 0); /* reserved */ | |
735 return 16; | |
736 } | |
737 | |
738 static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt) | |
739 { | |
740 GXFStreamContext *sc = &ctx->streams[pkt->stream_index]; | |
741 offset_t pos = url_ftell(pb); | |
742 int padding = 0; | |
743 | |
744 gxf_write_packet_header(pb, PKT_MEDIA); | |
745 if (sc->codec->codec_id == CODEC_ID_MPEG2VIDEO && pkt->size % 4) /* MPEG-2 frames must be padded */ | |
746 padding = 4 - pkt->size % 4; | |
1212 | 747 else if (sc->codec->codec_type == CODEC_TYPE_AUDIO) |
748 padding = GXF_AUDIO_PACKET_SIZE - pkt->size; | |
1183 | 749 gxf_write_media_preamble(pb, ctx, pkt, pkt->size + padding); |
1212 | 750 put_buffer(pb, pkt->data, pkt->size); |
751 gxf_write_padding(pb, padding); | |
1183 | 752 return updatePacketSize(pb, pos); |
753 } | |
754 | |
755 static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt) | |
756 { | |
757 GXFContext *gxf = s->priv_data; | |
758 | |
759 gxf_write_media_packet(&s->pb, gxf, pkt); | |
760 put_flush_packet(&s->pb); | |
761 return 0; | |
762 } | |
763 | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
764 static int gxf_new_audio_packet(GXFContext *gxf, GXFStreamContext *sc, AVPacket *pkt, int flush) |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
765 { |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
766 int size = flush ? av_fifo_size(&sc->audio_buffer) : GXF_AUDIO_PACKET_SIZE; |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
767 |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
768 if (!size) |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
769 return 0; |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
770 av_new_packet(pkt, size); |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
771 av_fifo_read(&sc->audio_buffer, pkt->data, size); |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
772 pkt->stream_index = sc->index; |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
773 pkt->dts = sc->current_dts; |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
774 sc->current_dts += size / 2; /* we only support 16 bit pcm mono for now */ |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
775 return size; |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
776 } |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
777 |
1183 | 778 static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) |
779 { | |
780 GXFContext *gxf = s->priv_data; | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
781 AVPacket new_pkt; |
1183 | 782 int i; |
783 | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
784 for (i = 0; i < s->nb_streams; i++) { |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
785 if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) { |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
786 GXFStreamContext *sc = &gxf->streams[i]; |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
787 if (pkt && pkt->stream_index == i) { |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
788 av_fifo_write(&sc->audio_buffer, pkt->data, pkt->size); |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
789 pkt = NULL; |
1183 | 790 } |
1322
95f56c7b24eb
* Moving FifoBuffer out of libavformat/avformat.h and
romansh
parents:
1262
diff
changeset
|
791 if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) { |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
792 if (gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) { |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
793 pkt = &new_pkt; |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
794 break; /* add pkt right now into list */ |
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
795 } |
1183 | 796 } |
797 } | |
798 } | |
1253
f3d5e1c49875
use packet dts as correct media field number and use av_interleave_pkt_per_dts
bcoudurier
parents:
1247
diff
changeset
|
799 return av_interleave_packet_per_dts(s, out, pkt, flush); |
1183 | 800 } |
801 | |
802 AVOutputFormat gxf_muxer = { | |
803 "gxf", | |
804 "GXF format", | |
805 NULL, | |
806 "gxf", | |
807 sizeof(GXFContext), | |
808 CODEC_ID_PCM_S16LE, | |
809 CODEC_ID_MPEG2VIDEO, | |
810 gxf_write_header, | |
811 gxf_write_packet, | |
812 gxf_write_trailer, | |
813 0, | |
814 NULL, | |
815 gxf_interleave_packet, | |
816 }; |