allow ffmpeg video to resample curr_pos, add bluray format
[goodguy/history.git] / cinelerra-5.0 / quicktime / mts2mp4.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8
9 #include "libavcodec/avcodec.h"
10 #include "libzmpeg3.h"
11 #include "quicktime.h"
12
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.
15
16
17
18 // Testing the ffmpeg decoder:
19 // ffmpeg -i ~mov/saved/test.m2ts -an /tmp/test.mp4
20
21
22 // Override the video format
23 //#define OVERRIDE_FORMAT
24
25 //#define MAX_FRAMES 1000
26
27 // Use NAL_AUD to denote the start of frame
28 #define USE_NAL_AUD
29
30 // Encode the audio.  Better to use ffmpeg for this.
31 //#define ENCODE_AUDIO
32
33
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;
40 int audio_track = 0;
41
42 #define AUDIO_BUFFER_SIZE       0x10000
43 #define BUFFER_SIZE             0x100000
44 #define MAX_FRAME_SIZE          0x400000
45 #define NAL_SLICE               0x01
46 #define NAL_DPA                 0x02
47 #define NAL_DPB                 0x03
48 #define NAL_DPC                 0x04
49 #define NAL_IDR_SLICE           0x05
50 #define NAL_SEI                 0x06
51 #define NAL_SPS                 0x07
52 #define NAL_PPS                 0x08
53 #define NAL_AUD                 0x09
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
59
60 #define NAL_CODE_SIZE            4
61
62
63
64
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;
70 int 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.
80 int is_keyframe = 0;
81 // This tells it when it doesn't need to decode sequence headers anymore.
82 int got_header = 0;
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
87 // be decoded.
88 zmpeg3_t *mpegv_fd = 0;
89 zmpeg3_t *mpega_fd = 0;
90 int video_eof = 0;
91 int audio_eof = 0;
92 char *in_path = 0;
93 char *out_path = 0;
94 int pass = 0;
95 AVCodec *decoder = 0;
96 AVCodecContext *decoder_context = 0;
97
98
99
100
101
102
103
104
105
106 void write_frame()
107 {
108         int result = 0;
109         int i;
110
111 // Shift data into frame buffer
112         frame_buffer_size = frame_end - buffer;
113         memcpy(frame_buffer, buffer, frame_buffer_size);
114
115
116
117
118
119 /*
120  * if(
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 &&
128  * buffer[7] == 0x2b)
129  * printf("write_frame %d\n", __LINE__);
130  */
131
132 // Shift input buffer
133         buffer_size = end - frame_end;
134         memcpy(buffer, frame_end, buffer_size);
135
136 // Seek to end of this NAL
137         ptr -= (frame_end - buffer);
138         end = buffer + buffer_size;
139
140         if(!frame_buffer_size) return;
141
142
143
144
145 /*
146  * static int debug_count = 0;
147  * char string[1024];
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);
152  * fclose(test);
153  */
154
155
156         
157         int got_picture = 0;
158         AVFrame picture;
159
160
161 // Skip frames until first keyframe
162 /*
163  *      if(!bytes_written && !is_keyframe)
164  *      {
165  *      }
166  *      else
167  */
168         if(pass == 0)
169         {
170 // Decode header only
171
172 printf("write_frame %d pass=%d %d\n", __LINE__, pass, frame_buffer_size);
173
174 /*
175 * static FILE *test = 0;
176 * if(!test) test = fopen("test", "w");
177 * fwrite(buffer, output_size, 1, test);
178 */
179
180                 avcodec_get_frame_defaults(&picture);
181                 avcodec_decode_video(decoder_context, 
182                         &picture, 
183                         &got_picture, 
184                         buffer, 
185                         frame_buffer_size);
186
187
188 printf("write_frame %d %d\n", __LINE__, frame_buffer_size);
189
190
191                 if(decoder_context->width > 0)
192                 {
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 / 
197                                 2;
198                         got_header = 1;
199 printf("%s format:\nwidth=%d\nheight=%d\nframerate=%f\naudio_track=%d\nsamplerate=%d\nchannels=%d\n", 
200 in_path,
201 video_width, 
202 video_height,
203 video_frame_rate,
204 audio_track,
205 audio_samplerate,
206 audio_channels);
207                 }
208
209         }
210         else
211         {
212 /*
213 * printf("write_frame: %s at offset %llx\n", 
214 * is_keyframe ? "Keyframe" : "Frame", 
215 * bytes_written);
216 */
217                 if(!quicktime_fd)
218                 {
219                         quicktime_fd = quicktime_open(out_path, 0, 1);
220                         if(!quicktime_fd)
221                         {
222                                 fprintf(stderr, 
223                                         "write_frame: Failed to open %s for writing\n",
224                                         out_path);
225                                 exit(1);
226                         }
227
228 #ifdef ENCODE_AUDIO
229                         quicktime_set_audio(quicktime_fd, 
230                                 audio_channels, 
231                                 audio_samplerate, 
232                                 16, 
233                                 QUICKTIME_MP4A);
234                         quicktime_set_parameter(quicktime_fd, "mp4a_bitrate", &audio_bitrate);
235 #endif
236
237                         quicktime_set_video(quicktime_fd, 
238                                 1, 
239                                 video_width, 
240                                 video_height,
241                                 video_frame_rate,
242                                 QUICKTIME_H264);
243                 }
244
245
246
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;
251                 int nal_size;
252                 int total_nals = 1;
253                 while(ptr2 < end2 + NAL_CODE_SIZE)
254                 {
255 // Start of next NAL code
256                         if(ptr2[0] == 0x00 &&
257                                 ptr2[1] == 0x00 &&
258                                 ptr2[2] == 0x00 &&
259                                 ptr2[3] == 0x01)
260                         {
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);
266                                 last_start = ptr2;
267                                 ptr2 += 4;
268                                 total_nals++;
269                         }
270                         else
271                                 ptr2++;
272                 }
273
274 //printf("write_frame total_nals=%d\n", total_nals);
275 // Last NAL in frame
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);
281
282
283                 if(is_keyframe)
284                         quicktime_insert_keyframe(quicktime_fd, frames_written, 0);
285
286                 result = quicktime_write_frame(quicktime_fd,
287                         frame_buffer,
288                         frame_buffer_size,
289                         0);
290
291 //printf("write_frame %d %d\n", __LINE__, frame_buffer_size);
292
293
294 // Test decode it
295 /*
296  * avcodec_get_frame_defaults(&picture);
297  * avcodec_decode_video(decoder_context, 
298  * &picture, 
299  * &got_picture, 
300  * frame_buffer, 
301  * frame_buffer_size);
302  */
303
304
305
306                 if(result)
307                 {
308                         fprintf(stderr, "Error writing frame\n");
309                         exit(1);
310                 }
311
312                 bytes_written += frame_buffer_size;
313                 frames_written++;
314                 printf("write_frame %d: video_eof=%d frames_written=%d         \n", 
315                         __LINE__, 
316                         video_eof,
317                         frames_written);
318                 fflush(stdout);
319         }
320
321         frame_buffer_size = 0;
322         is_keyframe = 0;
323
324 // Write audio up to current frame time
325 #ifdef ENCODE_AUDIO
326         if((video_eof || frames_written >= next_audio_frame) &&
327                 pass > 0 &&
328                 !audio_eof)
329         {
330                 int64_t next_audio_sample = (int64_t)next_audio_frame * 
331                         audio_samplerate /
332                         video_frame_rate;
333                 while((video_eof || samples_written < next_audio_sample) &&
334                         !audio_eof &&
335                         !mpeg3_end_of_audio(mpega_fd, audio_track))
336                 {
337 printf("write_frame %d: video_eof=%d samples_written=%lld\n", 
338 __LINE__, 
339 video_eof, 
340 samples_written);
341                         for(i = 0; i < audio_channels; i++)
342                         {
343                                 if(i == 0)
344                                         result = mpeg3_read_audio(mpega_fd, 
345                                                 audio_buffer[i], 
346                                                 0, 
347                                                 i, 
348                                                 AUDIO_BUFFER_SIZE, 
349                                                 audio_track);
350                                 else
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 */
356                                                 audio_track);
357                         }
358 //printf("write_frame %d\n", __LINE__);
359
360                         if(result || mpeg3_end_of_audio(mpega_fd, audio_track)) audio_eof = 1;
361
362                         result = quicktime_encode_audio(quicktime_fd, 
363                                 0, 
364                                 audio_buffer, 
365                                 AUDIO_BUFFER_SIZE);
366                         samples_written += AUDIO_BUFFER_SIZE;
367                         if(result)
368                         {
369                                 fprintf(stderr, "Error writing audio\n");
370                                 exit(1);
371                         }
372                 }
373
374                 next_audio_frame = frames_written + (int)video_frame_rate;
375         }
376 #endif // ENCODE_AUDIO
377 }
378
379 // Returns 1 and leaves ptr unchanged if more data needed
380 int decode_header()
381 {
382         if(got_header)
383         {
384                 if(ptr < end - NAL_CODE_SIZE)
385                 {
386                         ptr += NAL_CODE_SIZE;
387                         return 0;
388                 }
389         }
390         else
391         {
392 // Extract encoding information here.
393                 if(ptr < end - NAL_CODE_SIZE)
394                 {
395                         ptr += NAL_CODE_SIZE;
396                         next_audio_frame = (int)video_frame_rate;
397                         return 0;
398                 }
399         }
400
401         return 1;
402 }
403
404 // Returns 1 and leaves ptr unchanged if more data needed
405 int decode_nal()
406 {
407         if(ptr < end - NAL_CODE_SIZE - 2)
408         {
409                 unsigned char nal_type = ptr[NAL_CODE_SIZE] & 0x1f;
410
411
412
413 /*
414  * printf("decode_nal %d type=0x%02x 0x%02x offset=%lld video_eof=%d\n", 
415  * __LINE__, 
416  * nal_type, 
417  * ptr[NAL_CODE_SIZE + 1], 
418  * ptr - buffer,
419  * video_eof);
420  */
421
422
423
424                 switch(nal_type)
425                 {
426 #ifdef USE_NAL_AUD
427                         case NAL_AUD:
428 // End of frame is start of current NAL
429                                 frame_end = ptr;
430                                 write_frame();
431                                 ptr += NAL_CODE_SIZE;
432                                 break;
433
434
435 // sequence header
436                         case NAL_SPS:
437 /*
438  * printf("decode_nal %d type=0x%02x 0x%02x offset=%lld\n", 
439  * __LINE__, 
440  * nal_type, 
441  * ptr[NAL_CODE_SIZE + 1], 
442  * ptr - buffer);
443  */
444 // {
445 // int j;
446 // printf("decode_nal %d\n", __LINE__);
447 // for(j = 0; j < 66; j++)
448 // printf("%02x ", ptr[j]);
449 // printf("\n");
450 // }
451
452                                 is_keyframe = 1;
453                                 ptr += NAL_CODE_SIZE;
454                                 break;
455                 
456
457 // picture header
458                         case NAL_PPS:
459 // printf("decode_nal %d type=0x%02x 0x%02x offset=%lld\n", 
460 // __LINE__, 
461 // nal_type, 
462 // ptr[NAL_CODE_SIZE + 1], 
463 // ptr - buffer);
464                                 ptr += NAL_CODE_SIZE;
465                                 break;
466                 
467
468 #endif // USE_NAL_AUD
469
470
471
472
473
474
475
476
477
478
479 #ifndef USE_NAL_AUD
480 // sequence header
481                         case NAL_SPS:
482                         {
483 // End of frame is start of sequence header
484                                 frame_end = ptr;
485
486 // Decode header NAL
487                                 if(decode_header()) return 1;
488
489 printf("decode_nal %d: Got SPS buffer offset=%lld\n", __LINE__, ptr - buffer - 4);
490
491                                 write_frame();
492
493 // Set flags for next frame
494                                 is_keyframe = 1;
495                                 got_seq_header = 1;
496                                 break;
497                         }
498
499                         case NAL_PPS:
500 printf("decode_nal %d: Got PPS buffer offset=%lld\n", __LINE__, ptr - buffer - 4);
501 // Picture 
502                                 if(got_seq_header)
503                                 {
504 // Skip if preceeded by sequence header
505                                         got_seq_header = 0;
506                                         ptr += NAL_CODE_SIZE;
507                                 }
508                                 else
509                                 {
510 // End of frame is start of NAL
511                                         frame_end = ptr;
512                                         write_frame();
513                                         ptr += NAL_CODE_SIZE;
514                                 }
515                                 break;
516 #endif // !USE_NAL_AUD
517
518
519
520
521
522 // Unrecognized NAL
523                         default:
524 // Skip NAL start code
525                                 ptr += NAL_CODE_SIZE;
526                                 break;
527
528
529
530
531                 }
532         }
533         else
534                 return 1;
535         
536         return 0;
537 }
538
539 int main(int argc, char *argv[])
540 {
541         int error = 0;
542         int i;
543
544         buffer = malloc(BUFFER_SIZE);
545         frame_buffer = malloc(MAX_FRAME_SIZE);
546         ptr = buffer;
547         end = buffer + buffer_size;
548         next_audio_frame = 30;
549
550         if(argc < 3)
551         {
552                 printf("Usage: %s <input file> <output file>\n", argv[0]);
553                 exit(1);
554         }
555
556
557         in_path = argv[1];
558         out_path = argv[2];
559
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);
563         if(!mpegv_fd)
564         {
565                 fprintf(stderr, "Failed to open input file %s\n", in_path);
566                 exit(1);
567         }
568
569         if(!mpeg3_total_vstreams(mpegv_fd))
570         {
571                 fprintf(stderr, "No video streams in %s\n", in_path);
572                 exit(1);
573         }
574
575         if(!mpeg3_total_astreams(mpegv_fd))
576         {
577                 fprintf(stderr, "No audio streams in %s\n", in_path);
578                 exit(1);
579         }
580
581         mpega_fd = mpeg3_open_copy(in_path, mpegv_fd, 0);
582         printf("main %d: Decoding encoding parameters\n", __LINE__);
583
584
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);
592
593
594 // Initialize ffmpeg to decode headers
595         avcodec_init();
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;
602
603
604
605
606 //      printf("main %d: %d\n", __LINE__, decoder->id);
607
608 // Decode header on 1st pass.  Transcode on 2nd pass.
609         decoder_context->width = 0;
610         int64_t total_bytes = 0;
611
612
613
614         printf("main %d: Transcoding\n", __LINE__);
615
616
617 // Audio EOF is handled when audio is written
618         pass = 0;
619
620 #ifdef OVERRIDE_FORMAT
621         pass++;
622         got_header = 1;
623 #endif
624
625         for( ; pass < 2; pass++)
626         {
627 // Rewind streams
628                 mpeg3_seek_byte(mpegv_fd, 0);
629                 while(!video_eof &&
630                         (pass > 0 || !got_header) 
631
632 #ifdef MAX_FRAMES
633                         && frames_written < MAX_FRAMES
634 #endif
635                         )
636                 {
637 // Fill video input buffer
638                         while(!(error = mpeg3_read_video_chunk(mpegv_fd, 
639                                         buffer + buffer_size, 
640                                         &output_size, 
641                                         BUFFER_SIZE - buffer_size,
642                                         0)) &&
643                                         output_size >= 4)
644                         {
645
646 /*
647  * static FILE *test = 0;
648  * if(!test) test = fopen("test", "w");
649  * fwrite(buffer + buffer_size, output_size, 1, test);
650  */
651
652                                 buffer_size += output_size;
653                                 if(BUFFER_SIZE - buffer_size < 4) break;
654                         }
655
656                         if(error)
657                         {
658                                 video_eof = 1;
659                                 buffer_size += output_size;
660                         }
661
662                         end = buffer + buffer_size;
663
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))
668                         {
669 // Got a NAL
670                                 if(ptr[0] == 0x00 &&
671                                         ptr[1] == 0x00 &&
672                                         ptr[2] == 0x00 &&
673                                         ptr[3] == 0x01)
674                                 {
675 // Need more data if it fails
676                                         if(decode_nal()) break;
677                                 }
678                                 else
679                                 {
680                                         ptr++;
681                                 }
682                         }
683 //printf("main %d\n", __LINE__);
684                 }
685
686 // Rewind video stream
687                 buffer_size = 0;
688                 ptr = buffer;
689                 end = buffer;
690         }
691
692 // Flush
693         frame_end = buffer + buffer_size;
694         write_frame();
695
696
697         if(quicktime_fd) quicktime_close(quicktime_fd);
698         printf("main %d: Wrote %d frames\n", __LINE__, frames_written);
699 }
700
701
702
703