9 #include "libavcodec/avcodec.h"
10 #include "libzmpeg3.h"
11 #include "quicktime.h"
13 // This converts an AVC-HD transport stream to an mp4 Quicktime movie without
14 // re-encoding the video but does re-encode the audio.
18 // Testing the ffmpeg decoder:
19 // ffmpeg -i ~mov/saved/test.m2ts -an /tmp/test.mp4
22 // Override the video format
23 //#define OVERRIDE_FORMAT
25 //#define MAX_FRAMES 1000
27 // Use NAL_AUD to denote the start of frame
30 // Encode the audio. Better to use ffmpeg for this.
31 //#define ENCODE_AUDIO
34 double video_frame_rate = 24000.0 / 1001;
35 int video_width = 1920;
36 int video_height = 1080;
37 int audio_channels = 6;
38 int audio_samplerate = 48000;
39 int audio_bitrate = 768000;
42 #define AUDIO_BUFFER_SIZE 0x10000
43 #define BUFFER_SIZE 0x100000
44 #define MAX_FRAME_SIZE 0x400000
45 #define NAL_SLICE 0x01
49 #define NAL_IDR_SLICE 0x05
54 #define NAL_END_SEQUENCE 0x0a
55 #define NAL_END_STREAM 0x0b
56 #define NAL_FILLER_DATA 0x0c
57 #define NAL_SPS_EXT 0x0d
58 #define NAL_AUXILIARY_SLICE 0x13
60 #define NAL_CODE_SIZE 4
65 unsigned char *buffer = 0;
66 unsigned char *video_in_buffer = 0;
67 unsigned char *frame_buffer = 0;
68 float **audio_buffer = 0;
69 int frame_buffer_size = 0;
71 int64_t bytes_written = 0;
72 int frames_written = 0;
73 int64_t samples_written = 0;
74 // Last frame when we wrote audio
75 int next_audio_frame = 0;
76 unsigned char *ptr = 0;
77 unsigned char *end = 0;
78 unsigned char *frame_end = 0;
79 // Set to 1 after a sequence header is detected and after the previous frame is written.
81 // This tells it when it doesn't need to decode sequence headers anymore.
83 // This tells it to not flush the frame at the next picture header
84 int got_seq_header = 0;
85 quicktime_t *quicktime_fd = 0;
86 // Use 2 libzmpeg3 instances because while video is direct copied, audio must
88 zmpeg3_t *mpegv_fd = 0;
89 zmpeg3_t *mpega_fd = 0;
96 AVCodecContext *decoder_context = 0;
111 // Shift data into frame buffer
112 frame_buffer_size = frame_end - buffer;
113 memcpy(frame_buffer, buffer, frame_buffer_size);
121 * buffer[0] == 0xc2 &&
122 * buffer[1] == 0x5c &&
123 * buffer[2] == 0x43 &&
124 * buffer[3] == 0x49 &&
125 * buffer[4] == 0x95 &&
126 * buffer[5] == 0xc4 &&
127 * buffer[6] == 0xa6 &&
129 * printf("write_frame %d\n", __LINE__);
132 // Shift input buffer
133 buffer_size = end - frame_end;
134 memcpy(buffer, frame_end, buffer_size);
136 // Seek to end of this NAL
137 ptr -= (frame_end - buffer);
138 end = buffer + buffer_size;
140 if(!frame_buffer_size) return;
146 * static int debug_count = 0;
148 * sprintf(string, "test%06d", debug_count++);
149 * printf("write_frame %d: %s\n", __LINE__, string);
150 * FILE *test = fopen(string, "w");
151 * fwrite(frame_buffer, frame_buffer_size, 1, test);
161 // Skip frames until first keyframe
163 * if(!bytes_written && !is_keyframe)
170 // Decode header only
172 printf("write_frame %d pass=%d %d\n", __LINE__, pass, frame_buffer_size);
175 * static FILE *test = 0;
176 * if(!test) test = fopen("test", "w");
177 * fwrite(buffer, output_size, 1, test);
180 avcodec_get_frame_defaults(&picture);
181 avcodec_decode_video(decoder_context,
188 printf("write_frame %d %d\n", __LINE__, frame_buffer_size);
191 if(decoder_context->width > 0)
193 video_width = decoder_context->width;
194 video_height = decoder_context->height;
195 video_frame_rate = (double)decoder_context->time_base.den /
196 decoder_context->time_base.num /
199 printf("%s format:\nwidth=%d\nheight=%d\nframerate=%f\naudio_track=%d\nsamplerate=%d\nchannels=%d\n",
213 * printf("write_frame: %s at offset %llx\n",
214 * is_keyframe ? "Keyframe" : "Frame",
219 quicktime_fd = quicktime_open(out_path, 0, 1);
223 "write_frame: Failed to open %s for writing\n",
229 quicktime_set_audio(quicktime_fd,
234 quicktime_set_parameter(quicktime_fd, "mp4a_bitrate", &audio_bitrate);
237 quicktime_set_video(quicktime_fd,
247 // Convert NAL codes to AVC format
248 unsigned char *ptr2 = frame_buffer + NAL_CODE_SIZE;
249 unsigned char *end2 = frame_buffer + frame_buffer_size;
250 unsigned char *last_start = frame_buffer;
253 while(ptr2 < end2 + NAL_CODE_SIZE)
255 // Start of next NAL code
256 if(ptr2[0] == 0x00 &&
261 nal_size = ptr2 - last_start - 4;
262 last_start[0] = (nal_size & 0xff000000) >> 24;
263 last_start[1] = (nal_size & 0xff0000) >> 16;
264 last_start[2] = (nal_size & 0xff00) >> 8;
265 last_start[3] = (nal_size & 0xff);
274 //printf("write_frame total_nals=%d\n", total_nals);
276 nal_size = end2 - last_start - 4;
277 last_start[0] = (nal_size & 0xff000000) >> 24;
278 last_start[1] = (nal_size & 0xff0000) >> 16;
279 last_start[2] = (nal_size & 0xff00) >> 8;
280 last_start[3] = (nal_size & 0xff);
284 quicktime_insert_keyframe(quicktime_fd, frames_written, 0);
286 result = quicktime_write_frame(quicktime_fd,
291 //printf("write_frame %d %d\n", __LINE__, frame_buffer_size);
296 * avcodec_get_frame_defaults(&picture);
297 * avcodec_decode_video(decoder_context,
301 * frame_buffer_size);
308 fprintf(stderr, "Error writing frame\n");
312 bytes_written += frame_buffer_size;
314 printf("write_frame %d: video_eof=%d frames_written=%d \n",
321 frame_buffer_size = 0;
324 // Write audio up to current frame time
326 if((video_eof || frames_written >= next_audio_frame) &&
330 int64_t next_audio_sample = (int64_t)next_audio_frame *
333 while((video_eof || samples_written < next_audio_sample) &&
335 !mpeg3_end_of_audio(mpega_fd, audio_track))
337 printf("write_frame %d: video_eof=%d samples_written=%lld\n",
341 for(i = 0; i < audio_channels; i++)
344 result = mpeg3_read_audio(mpega_fd,
351 result = mpeg3_reread_audio(mpega_fd,
352 audio_buffer[i], /* Pointer to pre-allocated buffer of floats */
353 0, /* Pointer to pre-allocated buffer of int16's */
354 i, /* Channel to decode */
355 AUDIO_BUFFER_SIZE, /* Number of samples to decode */
358 //printf("write_frame %d\n", __LINE__);
360 if(result || mpeg3_end_of_audio(mpega_fd, audio_track)) audio_eof = 1;
362 result = quicktime_encode_audio(quicktime_fd,
366 samples_written += AUDIO_BUFFER_SIZE;
369 fprintf(stderr, "Error writing audio\n");
374 next_audio_frame = frames_written + (int)video_frame_rate;
376 #endif // ENCODE_AUDIO
379 // Returns 1 and leaves ptr unchanged if more data needed
384 if(ptr < end - NAL_CODE_SIZE)
386 ptr += NAL_CODE_SIZE;
392 // Extract encoding information here.
393 if(ptr < end - NAL_CODE_SIZE)
395 ptr += NAL_CODE_SIZE;
396 next_audio_frame = (int)video_frame_rate;
404 // Returns 1 and leaves ptr unchanged if more data needed
407 if(ptr < end - NAL_CODE_SIZE - 2)
409 unsigned char nal_type = ptr[NAL_CODE_SIZE] & 0x1f;
414 * printf("decode_nal %d type=0x%02x 0x%02x offset=%lld video_eof=%d\n",
417 * ptr[NAL_CODE_SIZE + 1],
428 // End of frame is start of current NAL
431 ptr += NAL_CODE_SIZE;
438 * printf("decode_nal %d type=0x%02x 0x%02x offset=%lld\n",
441 * ptr[NAL_CODE_SIZE + 1],
446 // printf("decode_nal %d\n", __LINE__);
447 // for(j = 0; j < 66; j++)
448 // printf("%02x ", ptr[j]);
453 ptr += NAL_CODE_SIZE;
459 // printf("decode_nal %d type=0x%02x 0x%02x offset=%lld\n",
462 // ptr[NAL_CODE_SIZE + 1],
464 ptr += NAL_CODE_SIZE;
468 #endif // USE_NAL_AUD
483 // End of frame is start of sequence header
487 if(decode_header()) return 1;
489 printf("decode_nal %d: Got SPS buffer offset=%lld\n", __LINE__, ptr - buffer - 4);
493 // Set flags for next frame
500 printf("decode_nal %d: Got PPS buffer offset=%lld\n", __LINE__, ptr - buffer - 4);
504 // Skip if preceeded by sequence header
506 ptr += NAL_CODE_SIZE;
510 // End of frame is start of NAL
513 ptr += NAL_CODE_SIZE;
516 #endif // !USE_NAL_AUD
524 // Skip NAL start code
525 ptr += NAL_CODE_SIZE;
539 int main(int argc, char *argv[])
544 buffer = malloc(BUFFER_SIZE);
545 frame_buffer = malloc(MAX_FRAME_SIZE);
547 end = buffer + buffer_size;
548 next_audio_frame = 30;
552 printf("Usage: %s <input file> <output file>\n", argv[0]);
560 // Use the libzmpeg3 internals to get at the demuxing support
561 printf("main %d: Opening source %s\n", __LINE__, in_path);
562 mpegv_fd = mpeg3_open(in_path, 0);
565 fprintf(stderr, "Failed to open input file %s\n", in_path);
569 if(!mpeg3_total_vstreams(mpegv_fd))
571 fprintf(stderr, "No video streams in %s\n", in_path);
575 if(!mpeg3_total_astreams(mpegv_fd))
577 fprintf(stderr, "No audio streams in %s\n", in_path);
581 mpega_fd = mpeg3_open_copy(in_path, mpegv_fd, 0);
582 printf("main %d: Decoding encoding parameters\n", __LINE__);
585 // Get encoding parameters
586 audio_channels = mpeg3_audio_channels(mpega_fd, audio_track);
587 audio_samplerate = mpeg3_sample_rate(mpega_fd, audio_track);
588 audio_bitrate = audio_channels * 128000;
589 audio_buffer = calloc(sizeof(float*), audio_channels);
590 for(i = 0; i < audio_channels; i++)
591 audio_buffer[i] = calloc(sizeof(float), AUDIO_BUFFER_SIZE);
594 // Initialize ffmpeg to decode headers
596 avcodec_register_all();
597 decoder = avcodec_find_decoder(CODEC_ID_H264);
598 // decoder = avcodec_find_decoder(CODEC_ID_VC1);
599 decoder_context = avcodec_alloc_context();
600 error = avcodec_open(decoder_context, decoder);
601 long output_size = 0;
606 // printf("main %d: %d\n", __LINE__, decoder->id);
608 // Decode header on 1st pass. Transcode on 2nd pass.
609 decoder_context->width = 0;
610 int64_t total_bytes = 0;
614 printf("main %d: Transcoding\n", __LINE__);
617 // Audio EOF is handled when audio is written
620 #ifdef OVERRIDE_FORMAT
625 for( ; pass < 2; pass++)
628 mpeg3_seek_byte(mpegv_fd, 0);
630 (pass > 0 || !got_header)
633 && frames_written < MAX_FRAMES
637 // Fill video input buffer
638 while(!(error = mpeg3_read_video_chunk(mpegv_fd,
639 buffer + buffer_size,
641 BUFFER_SIZE - buffer_size,
647 * static FILE *test = 0;
648 * if(!test) test = fopen("test", "w");
649 * fwrite(buffer + buffer_size, output_size, 1, test);
652 buffer_size += output_size;
653 if(BUFFER_SIZE - buffer_size < 4) break;
659 buffer_size += output_size;
662 end = buffer + buffer_size;
664 //printf("main %d video_eof=%d size=%p\n", __LINE__, video_eof, end - NAL_CODE_SIZE - 2 - ptr);
665 // Search for start code in buffer
666 while(ptr < end - NAL_CODE_SIZE - 2 &&
667 (pass > 0 || !got_header))
675 // Need more data if it fails
676 if(decode_nal()) break;
683 //printf("main %d\n", __LINE__);
686 // Rewind video stream
693 frame_end = buffer + buffer_size;
697 if(quicktime_fd) quicktime_close(quicktime_fd);
698 printf("main %d: Wrote %d frames\n", __LINE__, frames_written);