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