-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1334 lines (1078 loc) · 38.6 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2007 by Nikolay Nikolov <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define __STDC_CONSTANT_MACROS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <getopt.h>
#include "ffmpeg_headers.h"
#include "cdgfile.h"
#include "help.h"
#include "utils.h"
enum
{
OPTIONID_FORCE_ENCODE_AUDIO = 500,
OPTIONID_ACODEC,
OPTIONID_VCODEC,
OPTIONID_ASPECT,
OPTIONID_SHOW_FORMATS,
OPTIONID_SHOW_CODECS,
OPTIONID_STDOUT
};
class VideoFrameSurface : public ISurface
{
public:
virtual unsigned long MapRGBColour(int red, int green, int blue)
{
return ((uint8_t)red) << 16 | ((uint8_t)green) << 8 | ((uint8_t)blue);
}
};
typedef struct
{
AVOutputFormat *format;
// picture
int width;
int height;
AVRational aspect_ratio;
AVRational frame_rate;
enum PixelFormat frame_pix_fmt;
// audio
int audio_bit_rate;
int audio_sample_rate;
int audio_channels;
int audio_encode_always;
// video
int video_bit_rate;
int video_max_rate;
int video_min_rate;
int video_buffer_size;
int video_codec_flags;
// misc
int packet_size;
float mux_preload; // demux-decode delay in seconds
int video_stdout; // use "/dev/stdout" as a video file name
}tOptions;
// defualt options
static tOptions Options =
{
NULL,
352, 288, // PAL/SECAM: 352x288 - Video CD resolution
{4, 3}, // display aspect ratio
{25, 1}, // frame rate - PAL/SECAM: 25 frames per second
PIX_FMT_YUV420P,
192000, // audio bit rate
44100, // audio sample rate
2, // audio channels
0, // --force-encode-audio
400000, // video bit rate
0, // video max rate
0, // video min rate
0, // video buffer size
0, // video codec flags
0, // packet_size
0.5, // demux-decode delay in seconds
0 // use "/dev/stdout" as a video file name
};
static CDGFile cdgfile;
static VideoFrameSurface frameSurface;
static AVFrame *picture, *tmp_picture;
static struct SwsContext *img_convert_ctx;
static AVAudioFifo *audio_fifo;
static SwrContext *audio_resample_ctx;
static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
/* rescale output packet timestamp values from codec to stream timebase */
av_packet_rescale_ts(pkt, *time_base, st->time_base);
pkt->stream_index = st->index;
/* Write the compressed frame to the media file. */
return av_interleaved_write_frame(fmt_ctx, pkt);
}
// add audio stream, as a copy of is
static AVStream *add_audio_stream(AVFormatContext *oc, AVStream* is)
{
AVCodecContext *c;
AVStream *st;
if (is == NULL) return NULL;
st = avformat_new_stream(oc, NULL);
if (!st) {
fprintf(stderr, "Could not alloc audio stream\n");
exit(1);
}
st->id = 1;
c = st->codec;
c->codec_id = is->codec->codec_id ;
c->codec_type = is->codec->codec_type;
c->sample_fmt = is->codec->sample_fmt;
c->channel_layout = is->codec->channel_layout;
// put sample parameters
c->bit_rate = is->codec->bit_rate;
c->sample_rate = is->codec->sample_rate;
c->channels = is->codec->channels;
c->frame_size = is->codec->frame_size;
c->block_align= is->codec->block_align;
if (av_q2d(is->codec->time_base) > av_q2d(is->time_base) && av_q2d(is->time_base) < 1.0/1000)
c->time_base = is->codec->time_base;
else
c->time_base = is->time_base;
// some formats want stream headers to be separate
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
// add audio stream by codec_id
static AVStream *add_audio_stream(AVFormatContext *oc, AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
AVCodec *codec;
codec = avcodec_find_encoder(codec_id);
if (!codec)
{
fprintf(stderr, "Codec not found\n");
exit(1);
}
if (codec->type != AVMEDIA_TYPE_AUDIO)
{
fprintf(stderr, "Invalid audio codec\n");
exit(1);
}
st = avformat_new_stream(oc, codec);
if (!st) {
fprintf(stderr, "Could not alloc audio stream\n");
exit(1);
}
st->id = 1;
c = st->codec;
c->codec_id = codec_id;
c->codec_type = AVMEDIA_TYPE_AUDIO;
/* put sample parameters */
c->bit_rate = Options.audio_bit_rate;
c->sample_rate = Options.audio_sample_rate;
if (codec->supported_samplerates) {
c->sample_rate = codec->supported_samplerates[0];
for (int i = 0; codec->supported_samplerates[i]; i++) {
if (codec->supported_samplerates[i] == Options.audio_sample_rate) {
c->sample_rate = codec->supported_samplerates[i];
break;
}
}
}
c->channel_layout = av_get_default_channel_layout(Options.audio_channels);
if (codec->channel_layouts) {
c->channel_layout = codec->channel_layouts[0];
for (int i = 0; codec->channel_layouts[i]; i++) {
if (codec->channel_layouts[i] == (uint64_t)av_get_default_channel_layout(Options.audio_channels)) {
c->channel_layout = codec->channel_layouts[i];
break;
}
}
}
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
c->sample_fmt = AV_SAMPLE_FMT_FLTP;
if (codec->sample_fmts) {
c->sample_fmt = codec->sample_fmts[0];
for (int i = 0; codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
if (codec->sample_fmts[i] == AV_SAMPLE_FMT_S16) {
c->sample_fmt = codec->sample_fmts[i];
break;
}
if (codec->sample_fmts[i] == AV_SAMPLE_FMT_S16P) {
c->sample_fmt = codec->sample_fmts[i];
break;
}
}
}
st->time_base = (AVRational){1, c->sample_rate};
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
// open output audio codec
static void open_audio(AVFormatContext *oc, AVStream *st)
{
AVCodecContext *c;
AVCodec *codec;
c = st->codec;
// find the audio encoder
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
fprintf(stderr, "Output audio codec not found (ID: 0x%08X)\n", c->codec_id);
exit(1);
}
// open it
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open output audio codec (ID: 0x%08X)\n", c->codec_id);
exit(1);
}
audio_fifo = av_audio_fifo_alloc(st->codec->sample_fmt, st->codec->channels, 1);
}
static int init_converted_samples(uint8_t ***converted_input_samples,
AVCodecContext *output_codec_context,
int frame_size)
{
int error;
if (!(*converted_input_samples = (uint8_t **)calloc(output_codec_context->channels,
sizeof(**converted_input_samples)))) {
fprintf(stderr, "Could not allocate converted input sample pointers\n");
return AVERROR(ENOMEM);
}
if ((error = av_samples_alloc(*converted_input_samples, NULL,
output_codec_context->channels,
frame_size,
output_codec_context->sample_fmt, 0)) < 0) {
fprintf(stderr, "Could not allocate converted input samples\n");
av_freep(&(*converted_input_samples)[0]);
free(*converted_input_samples);
*converted_input_samples = NULL;
return error;
}
return 0;
}
static int convert_samples(const uint8_t **input_data,
uint8_t **converted_data, const int frame_size,
SwrContext *resample_context)
{
int error;
if ((error = swr_convert(resample_context,
converted_data, frame_size,
input_data , frame_size)) < 0) {
fprintf(stderr, "Could not convert input samples\n");
return error;
}
return 0;
}
static int add_samples_to_fifo(AVAudioFifo *fifo,
uint8_t **converted_input_samples,
const int frame_size)
{
int error;
if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
fprintf(stderr, "Could not reallocate FIFO\n");
return error;
}
if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
frame_size) < frame_size) {
fprintf(stderr, "Could not write data to FIFO\n");
return AVERROR_EXIT;
}
return 0;
}
static int decode_audio_frame(AVFormatContext *ic, AVStream* is, AVStream* os, int *finished)
{
int error = AVERROR_EXIT;
int data_present = 0;
uint8_t **converted_input_samples = NULL;
AVFrame *input_frame = NULL;
AVPacket input_packet;
av_init_packet(&input_packet);
input_packet.data = NULL;
input_packet.size = 0;
*finished = 0;
if ((error = av_read_frame(ic, &input_packet)) < 0) {
if (error == AVERROR_EOF) {
*finished = 1;
}
else {
fprintf(stderr, "Could not read audio frame \n");
goto cleanup;
}
}
input_frame = av_frame_alloc();
if (input_frame == NULL) {
fprintf(stderr, "Could not allocate input frame\n");
error = AVERROR_EXIT;
goto cleanup;
}
if ((error = avcodec_decode_audio4(is->codec, input_frame, &data_present, &input_packet)) < 0) {
fprintf(stderr, "Could not decode audi oframe\n");
av_free_packet(&input_packet);
goto cleanup;
}
if (*finished && data_present) {
*finished = 0;
}
if (*finished && !data_present) {
error = AVERROR_EOF;
goto cleanup;
}
if (data_present) {
if (init_converted_samples(&converted_input_samples, os->codec, input_frame->nb_samples))
goto cleanup;
if (convert_samples((const uint8_t**)input_frame->extended_data, converted_input_samples,
input_frame->nb_samples, audio_resample_ctx))
goto cleanup;
if (add_samples_to_fifo(audio_fifo, converted_input_samples,
input_frame->nb_samples))
goto cleanup;
}
error = 0;
cleanup:
if (converted_input_samples) {
av_freep(&converted_input_samples[0]);
free(converted_input_samples);
}
av_frame_free(&input_frame);
av_free_packet(&input_packet);
return error;
}
static int encode_audio_frame(AVFrame *frame,
AVFormatContext *oc,
AVStream* os,
int *data_present)
{
int error;
AVPacket output_packet;
av_init_packet(&output_packet);
output_packet.data = NULL;
output_packet.size = 0;
if ((error = avcodec_encode_audio2(os->codec, &output_packet, frame, data_present)) < 0) {
fprintf(stderr, "Could not encode audio frame\n");
av_free_packet(&output_packet);
return error;
}
if (*data_present) {
if ((error = write_frame(oc, &os->codec->time_base, os, &output_packet)) < 0) {
fprintf(stderr, "Could not write audio frame\n");
av_free_packet(&output_packet);
return error;
}
av_free_packet(&output_packet);
}
return 0;
}
static int init_output_frame(AVFrame **frame,
AVCodecContext *output_codec_context,
int frame_size)
{
int error;
/** Create a new frame to store the audio samples. */
if (!(*frame = av_frame_alloc())) {
fprintf(stderr, "Could not allocate output frame\n");
return AVERROR_EXIT;
}
(*frame)->nb_samples = frame_size;
(*frame)->channel_layout = output_codec_context->channel_layout;
(*frame)->format = output_codec_context->sample_fmt;
(*frame)->sample_rate = output_codec_context->sample_rate;
if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
fprintf(stderr, "Could allocate output frame samples\n");
av_frame_free(frame);
return error;
}
return 0;
}
static int encode_audio_from_fifo(AVAudioFifo *fifo,
AVFormatContext *oc,
AVStream *os,
int nb_samples)
{
AVFrame *output_frame;
int data_written;
const int frame_size = FFMIN(av_audio_fifo_size(fifo), nb_samples);
if (init_output_frame(&output_frame, os->codec, frame_size))
return AVERROR_EXIT;
if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
fprintf(stderr, "Could not read data from FIFO\n");
av_frame_free(&output_frame);
return AVERROR_EXIT;
}
if (encode_audio_frame(output_frame, oc, os, &data_written)) {
av_frame_free(&output_frame);
return AVERROR_EXIT;
}
av_frame_free(&output_frame);
return 0;
}
// Read frame from the input stream, decode it, re-encode it and write it in the output stream
// return - 0 if ok and != 0 if eof
static int write_audio_frame(AVFormatContext *ic, AVStream* is, AVFormatContext *oc, AVStream* os)
{
if (!ic || !is || !oc || !os) return 1;
int finished = 0;
int ret = decode_audio_frame(ic, is, os, &finished);
int nb_samples = os->codec->frame_size;
if (os->codec->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
nb_samples = av_audio_fifo_size(audio_fifo);
if (nb_samples <= 0) nb_samples = 1;
}
while (av_audio_fifo_size(audio_fifo) >= nb_samples ||
(finished && av_audio_fifo_size(audio_fifo) > 0))
{
if (encode_audio_from_fifo(audio_fifo, oc, os, nb_samples)) break;
}
if (finished) {
// Flush the encoder as it may have delayed frames.
int data_written = 0;
do {
if (encode_audio_frame(NULL, oc, os, &data_written)) break;
} while (data_written);
}
return ret;
}
// close output audio codec
static void close_audio(AVFormatContext *oc, AVStream *st)
{
avcodec_close(st->codec);
av_audio_fifo_free(audio_fifo);
}
static int copy_audio_frame(AVFormatContext *ic, AVStream* is, AVFormatContext *oc, AVStream* os)
{
int ret;
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
if (!ic || !is || !oc || !os)
return 1;
ret = av_read_frame(ic, &pkt);
if (ret == 0) {
write_frame(oc, &is->time_base, os, &pkt);
av_free_packet(&pkt);
}
return ret;
}
static AVStream* open_input_audio(CdgIoStream* pAudioStream, AVFormatContext **ic)
{
AVStream *st;
int audioStreamIdx;
*ic = avformat_alloc_context();
if (*ic == NULL) return NULL;
(*ic)->pb = pAudioStream->get_avio();
// Open audio file
if (avformat_open_input(ic, NULL, NULL, NULL) != 0)
return NULL; // Couldn't open file
// Retrieve stream information
if (avformat_find_stream_info(*ic, NULL) < 0)
return NULL; // Couldn't find stream information
// Dump information about file onto standard error
av_dump_format(*ic, 0, pAudioStream->getfilename(), false);
// Find the first audio stream
audioStreamIdx = -1;
for (unsigned int i = 0; i < (*ic)->nb_streams; i++) {
if ((*ic)->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStreamIdx = i;
break;
}
}
if (audioStreamIdx == -1)
return NULL; // Didn't find a audio stream
// Get a pointer to the input audio stream
st = (*ic)->streams[audioStreamIdx];
// find the audio encoder
AVCodec *codec = avcodec_find_decoder(st->codec->codec_id);
if (!codec) {
fprintf(stderr, "Input audio codec not found (ID: 0x%08X)\n", st->codec->codec_id);
exit(1);
}
// open it
if (avcodec_open2(st->codec, codec, NULL) < 0) {
fprintf(stderr, "Could not open input audio codec (ID: 0x%08X)\n", st->codec->codec_id);
exit(1);
}
return st;
}
static void close_input_audio(AVFormatContext *ic, AVStream* is)
{
if (is) avcodec_close(is->codec);
if (ic) avformat_close_input(&ic);
}
// Add video output stream
static AVStream *add_video_stream(AVFormatContext *oc, AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
st = avformat_new_stream(oc, NULL);
if (!st) {
fprintf(stderr, "Could not alloc video stream\n");
exit(1);
}
st->id = 0;
c = st->codec;
c->codec_id = codec_id;
c->codec_type = AVMEDIA_TYPE_VIDEO;
c->flags |= Options.video_codec_flags;
c->bit_rate = Options.video_bit_rate;
c->bit_rate_tolerance = c->bit_rate * 20;
c->rc_max_rate = Options.video_max_rate;
c->rc_min_rate = Options.video_min_rate;
c->rc_buffer_size = Options.video_buffer_size;
// resolution must be a multiple of two
c->width = Options.width;
c->height = Options.height;
c->pix_fmt = Options.frame_pix_fmt;
// calculate pixel aspect ratio
c->sample_aspect_ratio = av_d2q(av_q2d(Options.aspect_ratio)*Options.height/Options.width, 255);
st->sample_aspect_ratio = c->sample_aspect_ratio;
// time base: this is the fundamental unit of time (in seconds) in terms
// of which frame timestamps are represented. for fixed-fps content,
// timebase should be 1/framerate and timestamp increments should be
// identically 1.
st->time_base = (AVRational){Options.frame_rate.den, Options.frame_rate.num};
c->time_base = st->time_base;
//c->sample_aspect_ratio = av_d2q(frame_aspect_ratio*c->height/c->width, 255);
c->gop_size = 12; // emit one intra frame every twelve frames at most
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO){
// Needed to avoid using macroblocks in which some coeffs overflow.
// This does not happen with normal video, it just happens here as
// the motion of the chroma plane does not match the luma plane.
c->mb_decision = FF_MB_DECISION_RD; // rate distoration
}
// Fix "rc buffer underflow" warning on the first encoding frame
c->rc_initial_buffer_occupancy = c->rc_buffer_size*3/4;
// some formats want stream headers to be separate
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
static AVFrame *alloc_picture(PixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
picture = av_frame_alloc();
if (!picture)
return NULL;
if (av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 16) < 0) {
av_frame_free(&picture);
return NULL;
}
return picture;
}
// Open output video stram
static void open_video(AVFormatContext *oc, AVStream *st)
{
AVCodec *codec;
AVCodecContext *c;
c = st->codec;
// find the video encoder
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
fprintf(stderr, "Video codec not found (ID: 0x%08X)\n", c->codec_id);
exit(1);
}
// open the codec
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open video codec (ID: 0x%08X)\n", c->codec_id);
exit(1);
}
// allocate the encoded raw picture
picture = alloc_picture(c->pix_fmt, c->width, c->height);
if (!picture) {
fprintf(stderr, "Could not allocate picture\n");
exit(1);
}
// tmp_picture is used for conversion between internal frame format,
// wich is RGB32 with a constant size and the output frame format
tmp_picture = alloc_picture(PIX_FMT_RGB24, CDG_FULL_WIDTH, CDG_FULL_HEIGHT);
if (!tmp_picture) {
fprintf(stderr, "Could not allocate temporary picture\n");
exit(1);
}
// create image convert context used to convert between tmp_picture and picture
img_convert_ctx = sws_getContext(CDG_FULL_WIDTH, CDG_FULL_HEIGHT,
PIX_FMT_RGB24,
c->width, c->height,
c->pix_fmt,
SWS_BICUBIC, NULL, NULL, NULL);
if (img_convert_ctx == NULL) {
fprintf(stderr, "Cannot initialize the conversion context\n");
exit(1);
}
}
static void write_video_frame(AVFormatContext *oc, AVStream *st)
{
AVCodecContext *c;
c = st->codec;
// Copy CD+G frame to a temporary frame object - tmp_picture
for (int height = 0; height < CDG_FULL_HEIGHT; height++) {
for (int width = 0, x=0; width < CDG_FULL_WIDTH; width++, x+=3) {
tmp_picture->data[0][height*tmp_picture->linesize[0] + x] =
(uint8_t)(frameSurface.rgbData[height][width] >> 16);
tmp_picture->data[0][height*tmp_picture->linesize[0] + x + 1] =
(uint8_t)(frameSurface.rgbData[height][width] >> 8);
tmp_picture->data[0][height*tmp_picture->linesize[0] + x + 2] =
(uint8_t)(frameSurface.rgbData[height][width]);
}
}
// As the CDG frame is RGB, convert it to the output color format and scale the image
sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize,
0, CDG_FULL_HEIGHT, picture->data, picture->linesize);
// Encode frame
int got_packet = 0;
AVPacket pkt;
av_init_packet(&pkt);
pkt.data= NULL;
pkt.size= 0;
if (avcodec_encode_video2(c, &pkt, picture, &got_packet) == 0 && got_packet == 1) {
// write the compressed frame in the media file
if (write_frame(oc, &c->time_base, st, &pkt) < 0) {
fprintf(stderr, "Error while writing video frame\n");
exit(1);
}
}
av_free_packet(&pkt);
}
static void close_video(AVFormatContext *oc, AVStream *st)
{
avcodec_close(st->codec);
av_free(picture->data[0]);
av_frame_free(&picture);
av_free(tmp_picture->data[0]);
av_frame_free(&tmp_picture);
sws_freeContext(img_convert_ctx);
}
int cdg2avi(const char* avifile, CdgIoStream* pAudioStream)
{
AVFormatContext *oc = NULL;
AVFormatContext *ic = NULL;
AVStream *video_st = NULL;
AVStream *audio_st = NULL;
AVStream *in_audio_st = NULL;
bool copy_audio = false;
if (pAudioStream) {
in_audio_st = open_input_audio(pAudioStream, &ic);
if (in_audio_st == NULL) {
if (ic == NULL)
fprintf(stderr, "WARNING: Unable to allocate context for audio file: %s\n", pAudioStream->getfilename());
else
fprintf(stderr, "WARNING: Unable to find input audio stream in %s\n", pAudioStream->getfilename());
}
}
// allocate the output media context
oc = avformat_alloc_context();
if (!oc) {
fprintf(stderr, "Memory error\n");
exit(1);
}
oc->oformat = Options.format;
snprintf(oc->filename, sizeof(oc->filename), "%s", avifile);
if (Options.format->video_codec != AV_CODEC_ID_NONE) {
video_st = add_video_stream(oc, Options.format->video_codec);
}
if (in_audio_st && Options.format->audio_codec != AV_CODEC_ID_NONE)
{
if (Options.format->audio_codec == in_audio_st->codec->codec_id) {
copy_audio = true;
}
if (Options.audio_encode_always) {
copy_audio = false;
}
if (copy_audio) {
audio_st = add_audio_stream(oc, in_audio_st);
}
else {
audio_st = add_audio_stream(oc, Options.format->audio_codec);
}
}
av_dump_format(oc, 0, avifile, 1);
if (video_st)
open_video(oc, video_st);
audio_resample_ctx = NULL;
if (copy_audio == false && audio_st) {
open_audio(oc, audio_st);
// Create a resampler context for the conversion
audio_resample_ctx = swr_alloc_set_opts(NULL,
audio_st->codec->channel_layout,
audio_st->codec->sample_fmt,
audio_st->codec->sample_rate,
in_audio_st->codec->channel_layout,
in_audio_st->codec->sample_fmt,
in_audio_st->codec->sample_rate,
0, NULL);
if (!audio_resample_ctx) {
fprintf(stderr, "Can't resample audio. Aborting.\n");
exit(1);
}
// initialize the resampling context
if (swr_init(audio_resample_ctx) < 0) {
fprintf(stderr, "Failed to initialize the resampling context\n");
exit(1);
}
}
// open the output file, if needed
if (!(Options.format->flags & AVFMT_NOFILE)) {
if (avio_open(&oc->pb, avifile, AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "Could not open '%s'\n", avifile);
exit(1);
}
}
// Set context options
oc->packet_size = Options.packet_size;
oc->max_delay = (int)(0.7 * AV_TIME_BASE);
// add meta data to the output file
av_dict_set(&oc->metadata, "encoded_by", PACKAGE " " VERSION, 0);
if (ic) {
AVDictionaryEntry *tag;
tag = av_dict_get(ic->metadata, "title", NULL, 0);
if (tag) av_dict_set(&oc->metadata, tag->key, tag->value, 0);
tag = av_dict_get(ic->metadata, "artist", NULL, 0);
if (tag) av_dict_set(&oc->metadata, tag->key, tag->value, 0);
}
// write the stream header, if any
avformat_write_header(oc, NULL);
// write avi file
int duration = cdgfile.getTotalDuration(); // in miliseconds
int64_t video_pts = 1000 * video_st->pts.val * video_st->time_base.num / video_st->time_base.den;;
while (cdgfile.renderAtPosition(video_pts))
{
write_video_frame(oc, video_st);
video_pts = 1000 * video_st->pts.val * video_st->time_base.num / video_st->time_base.den;;
if (audio_st) {
int audio_ok = 0;
int64_t audio_pts;
do {
if (copy_audio) {
audio_ok = copy_audio_frame(ic, in_audio_st, oc, audio_st);
}
else {
audio_ok = write_audio_frame(ic, in_audio_st, oc, audio_st);
}
audio_pts = 1000 * audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
} while (audio_ok == 0 && audio_pts < video_pts);
}
if (duration)
{
fprintf(stderr, "Progress: %d %%\r", (int)((video_pts * 100) / duration));
}
}
fprintf(stderr, "\n"); // save the status line
// close each codec
if (video_st)
close_video(oc, video_st);
if (copy_audio == false && audio_st)
close_audio(oc, audio_st);
// write the trailer, if any
av_write_trailer(oc);
// free the streams
for(unsigned int i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
if (!(Options.format->flags & AVFMT_NOFILE)) {
// close the output file
avio_close(oc->pb);
}
if (audio_resample_ctx)
swr_free(&audio_resample_ctx);
// free the stream
av_free(oc);
close_input_audio(ic, in_audio_st);
return 0;
}
static void set_audio_codec_defaults()
{
if (!Options.format) return ;
switch(Options.format->audio_codec)
{
case AV_CODEC_ID_AMR_NB:
Options.audio_bit_rate = 12200;
Options.audio_sample_rate = 8000;
Options.audio_channels = 1;
break;
case AV_CODEC_ID_AMR_WB:
Options.audio_bit_rate = 23850;
Options.audio_sample_rate = 16000;
Options.audio_channels = 1;
break;
//case AV_CODEC_ID_AC3:
// Options.audio_bit_rate = 192000;
// Options.audio_sample_rate = 48000;
// Options.audio_channels = 2;