-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
transcoding.c
1992 lines (1604 loc) · 55.9 KB
/
transcoding.c
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
/**
* Transcoding
* Copyright (C) 2013 John Törnblom
*
* 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/>.
*/
#include <unistd.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavresample/avresample.h>
#include <libavutil/opt.h>
#include <libavutil/audio_fifo.h>
#include <libavutil/dict.h>
#if LIBAVUTIL_VERSION_MICRO >= 100 /* FFMPEG */
#define USING_FFMPEG 1
#endif
#include "tvheadend.h"
#include "settings.h"
#include "streaming.h"
#include "service.h"
#include "packet.h"
#include "transcoding.h"
#include "libav.h"
#include "parsers/bitstream.h"
#include "parsers/parser_avc.h"
LIST_HEAD(transcoder_stream_list, transcoder_stream);
struct transcoder;
typedef struct transcoder_stream {
int ts_index;
streaming_component_type_t ts_type;
streaming_target_t *ts_target;
LIST_ENTRY(transcoder_stream) ts_link;
int ts_first;
void (*ts_handle_pkt) (struct transcoder *, struct transcoder_stream *, th_pkt_t *);
void (*ts_destroy) (struct transcoder *, struct transcoder_stream *);
} transcoder_stream_t;
typedef struct audio_stream {
transcoder_stream_t;
AVCodecContext *aud_ictx;
AVCodec *aud_icodec;
AVCodecContext *aud_octx;
AVCodec *aud_ocodec;
uint64_t aud_dec_pts;
uint64_t aud_enc_pts;
int8_t aud_channels;
int32_t aud_bitrate;
AVAudioResampleContext *resample_context;
AVAudioFifo *fifo;
int resample;
int resample_is_open;
enum AVSampleFormat last_sample_fmt;
int last_sample_rate;
uint64_t last_channel_layout;
} audio_stream_t;
typedef struct video_stream {
transcoder_stream_t;
AVCodecContext *vid_ictx;
AVCodec *vid_icodec;
AVCodecContext *vid_octx;
AVCodec *vid_ocodec;
AVFrame *vid_dec_frame;
struct SwsContext *vid_scaler;
AVFrame *vid_enc_frame;
int16_t vid_width;
int16_t vid_height;
int vid_first_sent;
int vid_first_encoded;
th_pkt_t *vid_first_pkt;
} video_stream_t;
typedef struct subtitle_stream {
transcoder_stream_t;
AVCodecContext *sub_ictx;
AVCodec *sub_icodec;
AVCodecContext *sub_octx;
AVCodec *sub_ocodec;
} subtitle_stream_t;
typedef struct transcoder {
streaming_target_t t_input; // must be first
streaming_target_t *t_output;
uint32_t t_id;
transcoder_props_t t_props;
struct transcoder_stream_list t_stream_list;
} transcoder_t;
#define WORKING_ENCODER(x) \
((x) == AV_CODEC_ID_H264 || (x) == AV_CODEC_ID_MPEG2VIDEO || \
(x) == AV_CODEC_ID_VP8 || /* (x) == AV_CODEC_ID_VP9 || */ \
(x) == AV_CODEC_ID_HEVC || (x) == AV_CODEC_ID_AAC || \
(x) == AV_CODEC_ID_MP2 || (x) == AV_CODEC_ID_VORBIS)
/**
*
*/
static inline int
shortid(transcoder_t *t)
{
return t->t_id & 0xffff;
}
static inline void
transcoder_stream_invalidate(transcoder_stream_t *ts)
{
ts->ts_index = 0;
}
static AVCodecContext *
avcodec_alloc_context3_tvh(const AVCodec *codec)
{
AVCodecContext *ctx = avcodec_alloc_context3(codec);
if (ctx) {
ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
}
return ctx;
}
static char *const
get_error_text(const int error)
{
static char error_buffer[255];
av_strerror(error, error_buffer, sizeof(error_buffer));
return error_buffer;
}
static int
transcode_opt_set_int(transcoder_t *t, transcoder_stream_t *ts,
void *ctx, const char *opt,
int64_t val, int abort)
{
int opt_error;
if ((opt_error = av_opt_set_int(ctx, opt, val, 0)) != 0) {
tvherror("transcode", "%04X: Could not set option %s (error '%s')",
shortid(t), opt, get_error_text(opt_error));
if (abort)
transcoder_stream_invalidate(ts);
return -1;
}
return 0;
}
static void
av_dict_set_int__(AVDictionary **opts, const char *key, int64_t val, int flags)
{
char buf[32];
snprintf(buf, sizeof(buf), "%"PRId64, val);
av_dict_set(opts, key, buf, flags);
}
/**
* get best effort sample rate
*/
static int
transcode_get_sample_rate(int rate, AVCodec *codec)
{
/* if codec only supports certain rates, check if rate is available */
if (codec->supported_samplerates) {
/* Find if we have a matching sample_rate */
int acount = 0;
int rate_alt = 0;
while (codec->supported_samplerates[acount] > 0) {
if (codec->supported_samplerates[acount] == rate) {
/* original rate supported by codec */
return rate;
}
/* check for highest available rate smaller that the original rate */
if (codec->supported_samplerates[acount] > rate_alt &&
codec->supported_samplerates[acount] < rate) {
rate_alt = codec->supported_samplerates[acount];
}
acount++;
}
return rate_alt;
}
return rate;
}
/**
* get best effort sample format
*/
static enum AVSampleFormat
transcode_get_sample_fmt(enum AVSampleFormat fmt, AVCodec *codec)
{
/* if codec only supports certain formats, check if selected format is available */
if (codec->sample_fmts) {
/* Find if we have a matching sample_fmt */
int acount = 0;
while (codec->sample_fmts[acount] > AV_SAMPLE_FMT_NONE) {
if (codec->sample_fmts[acount] == fmt) {
/* original format supported by codec */
return fmt;
}
acount++;
}
/* use first supported sample format */
if (acount > 0) {
return codec->sample_fmts[0];
} else {
return AV_SAMPLE_FMT_NONE;
}
}
return fmt;
}
/**
* get best effort channel layout
*/
static uint64_t
transcode_get_channel_layout(int *channels, AVCodec *codec)
{
uint64_t channel_layout = AV_CH_LAYOUT_STEREO;
/* use channel layout based on input field */
switch (*channels) {
case 1: channel_layout = AV_CH_LAYOUT_MONO; break;
case 2: channel_layout = AV_CH_LAYOUT_STEREO; break;
case 3: channel_layout = AV_CH_LAYOUT_SURROUND; break;
case 4: channel_layout = AV_CH_LAYOUT_QUAD; break;
case 5: channel_layout = AV_CH_LAYOUT_5POINT0; break;
case 6: channel_layout = AV_CH_LAYOUT_5POINT1; break;
case 7: channel_layout = AV_CH_LAYOUT_6POINT1; break;
case 8: channel_layout = AV_CH_LAYOUT_7POINT1; break;
}
/* if codec only supports certain layouts, check if selected layout is available */
if (codec->channel_layouts) {
int acount = 0;
uint64_t channel_layout_def = av_get_default_channel_layout(*channels);
uint64_t channel_layout_alt = 0;
while (codec->channel_layouts[acount] > 0) {
if (codec->channel_layouts[acount] == channel_layout) {
/* original layout supported by codec */
return channel_layout;
}
/* check for best matching layout with same or less number of channels */
if (av_get_channel_layout_nb_channels(codec->channel_layouts[acount]) <= *channels) {
if (av_get_channel_layout_nb_channels(codec->channel_layouts[acount]) >
av_get_channel_layout_nb_channels(channel_layout_alt)) {
/* prefer layout with more channels */
channel_layout_alt = codec->channel_layouts[acount];
} else if (av_get_channel_layout_nb_channels(codec->channel_layouts[acount]) ==
av_get_channel_layout_nb_channels(channel_layout_alt) &&
codec->channel_layouts[acount] == channel_layout_def) {
/* prefer default layout for number of channels over alternative layout */
channel_layout_alt = channel_layout_def;
}
}
acount++;
}
if (channel_layout_alt) {
channel_layout = channel_layout_alt;
*channels = av_get_channel_layout_nb_channels(channel_layout_alt);
} else {
channel_layout = 0;
*channels = 0;
}
}
return channel_layout;
}
/**
*
*/
static AVCodec *
transcoder_get_decoder(transcoder_t *t, streaming_component_type_t ty)
{
enum AVCodecID codec_id;
AVCodec *codec;
/* the MP4A and AAC packet format is same, reduce to one type */
if (ty == SCT_MP4A)
ty = SCT_AAC;
codec_id = streaming_component_type2codec_id(ty);
if (codec_id == AV_CODEC_ID_NONE) {
tvherror("transcode", "%04X: Unsupported input codec %s",
shortid(t), streaming_component_type2txt(ty));
return NULL;
}
codec = avcodec_find_decoder(codec_id);
if (!codec) {
tvherror("transcode", "%04X: Unable to find %s decoder",
shortid(t), streaming_component_type2txt(ty));
return NULL;
}
tvhtrace("transcode", "%04X: Using decoder %s", shortid(t), codec->name);
return codec;
}
/**
*
*/
static AVCodec *
transcoder_get_encoder(transcoder_t *t, const char *codec_name)
{
AVCodec *codec;
codec = avcodec_find_encoder_by_name(codec_name);
if (!codec) {
tvherror("transcode", "%04X: Unable to find %s encoder",
shortid(t), codec_name);
return NULL;
}
tvhtrace("transcode", "%04X: Using encoder %s", shortid(t), codec->name);
return codec;
}
/**
*
*/
static void
transcoder_stream_packet(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
{
streaming_message_t *sm;
tvhtrace("transcode", "%04X: deliver copy (pts = %" PRIu64 ")",
shortid(t), pkt->pkt_pts);
sm = streaming_msg_create_pkt(pkt);
streaming_target_deliver2(ts->ts_target, sm);
pkt_ref_dec(pkt);
}
/**
*
*/
static void
transcoder_stream_subtitle(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
{
//streaming_message_t *sm;
AVCodec *icodec;
AVCodecContext *ictx;
AVPacket packet;
AVSubtitle sub;
int length, got_subtitle;
subtitle_stream_t *ss = (subtitle_stream_t*)ts;
ictx = ss->sub_ictx;
//octx = ss->sub_octx;
icodec = ss->sub_icodec;
//ocodec = ss->sub_ocodec;
if (!avcodec_is_open(ictx)) {
if (avcodec_open2(ictx, icodec, NULL) < 0) {
tvherror("transcode", "%04X: Unable to open %s decoder",
shortid(t), icodec->name);
transcoder_stream_invalidate(ts);
goto cleanup;
}
}
av_init_packet(&packet);
packet.data = pktbuf_ptr(pkt->pkt_payload);
packet.size = pktbuf_len(pkt->pkt_payload);
packet.pts = pkt->pkt_pts;
packet.dts = pkt->pkt_dts;
packet.duration = pkt->pkt_duration;
length = avcodec_decode_subtitle2(ictx, &sub, &got_subtitle, &packet);
if (length <= 0) {
if (length == AVERROR_INVALIDDATA) goto cleanup;
tvherror("transcode", "%04X: Unable to decode subtitle (%d, %s)",
shortid(t), length, get_error_text(length));
goto cleanup;
}
if (!got_subtitle)
goto cleanup;
//TODO: encoding
cleanup:
av_free_packet(&packet);
avsubtitle_free(&sub);
}
static void
create_adts_header(pktbuf_t *pb, int sri, int channels)
{
bitstream_t bs;
/* 7 bytes of ADTS header */
init_wbits(&bs, pktbuf_ptr(pb), 56);
put_bits(&bs, 0xfff, 12); // Sync marker
put_bits(&bs, 0, 1); // ID 0 = MPEG 4, 1 = MPEG 2
put_bits(&bs, 0, 2); // Layer
put_bits(&bs, 1, 1); // Protection absent
put_bits(&bs, 1, 2); // AOT, 1 = AAC LC
put_bits(&bs, sri, 4);
put_bits(&bs, 1, 1); // Private bit
put_bits(&bs, channels, 3);
put_bits(&bs, 1, 1); // Original
put_bits(&bs, 1, 1); // Copy
put_bits(&bs, 1, 1); // Copyright identification bit
put_bits(&bs, 1, 1); // Copyright identification start
put_bits(&bs, pktbuf_len(pb), 13);
put_bits(&bs, 0x7ff, 11); // Buffer fullness
put_bits(&bs, 0, 2); // RDB in frame
}
/**
*
*/
static void
transcoder_stream_audio(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt)
{
AVCodec *icodec, *ocodec;
AVCodecContext *ictx, *octx;
AVPacket packet;
int length;
streaming_message_t *sm;
th_pkt_t *n;
audio_stream_t *as = (audio_stream_t*)ts;
int got_frame, got_packet_ptr;
AVFrame *frame = av_frame_alloc();
char layout_buf[100];
ictx = as->aud_ictx;
octx = as->aud_octx;
icodec = as->aud_icodec;
ocodec = as->aud_ocodec;
if (!avcodec_is_open(ictx)) {
if (icodec->id == AV_CODEC_ID_AAC || icodec->id == AV_CODEC_ID_VORBIS) {
if (pkt->pkt_meta) {
ictx->extradata_size = pktbuf_len(pkt->pkt_meta);
ictx->extradata = av_malloc(ictx->extradata_size);
memcpy(ictx->extradata,
pktbuf_ptr(pkt->pkt_meta), pktbuf_len(pkt->pkt_meta));
} else {
/* wait for metadata */
return;
}
}
if (avcodec_open2(ictx, icodec, NULL) < 0) {
tvherror("transcode", "%04X: Unable to open %s decoder",
shortid(t), icodec->name);
transcoder_stream_invalidate(ts);
goto cleanup;
}
as->aud_dec_pts = pkt->pkt_pts;
}
if (pkt->pkt_pts > as->aud_dec_pts) {
tvhwarn("transcode", "%04X: Detected framedrop in audio", shortid(t));
as->aud_enc_pts += (pkt->pkt_pts - as->aud_dec_pts);
as->aud_dec_pts += (pkt->pkt_pts - as->aud_dec_pts);
}
av_init_packet(&packet);
packet.data = pktbuf_ptr(pkt->pkt_payload);
packet.size = pktbuf_len(pkt->pkt_payload);
packet.pts = pkt->pkt_pts;
packet.dts = pkt->pkt_dts;
packet.duration = pkt->pkt_duration;
length = avcodec_decode_audio4(ictx, frame, &got_frame, &packet);
av_free_packet(&packet);
tvhtrace("transcode", "%04X: audio decode: consumed=%d size=%zu, got=%d, pts=%" PRIi64,
shortid(t), length, pktbuf_len(pkt->pkt_payload), got_frame, pkt->pkt_pts);
if (length < 0) {
if (length == AVERROR_INVALIDDATA) goto cleanup;
tvherror("transcode", "%04X: Unable to decode audio (%d, %s)",
shortid(t), length, get_error_text(length));
transcoder_stream_invalidate(ts);
goto cleanup;
}
if (!got_frame) {
tvhtrace("transcode", "%04X: Did not have a full frame in the packet", shortid(t));
goto cleanup;
}
if (length != pktbuf_len(pkt->pkt_payload))
tvhwarn("transcode",
"%04X: undecoded data (in=%zu, consumed=%d)",
shortid(t), pktbuf_len(pkt->pkt_payload), length);
if (!avcodec_is_open(octx)) {
as->aud_enc_pts = pkt->pkt_pts;
octx->sample_rate = transcode_get_sample_rate(ictx->sample_rate, ocodec);
octx->sample_fmt = transcode_get_sample_fmt(ictx->sample_fmt, ocodec);
octx->time_base = ictx->time_base;
octx->channels = as->aud_channels ? as->aud_channels : ictx->channels;
octx->channel_layout = transcode_get_channel_layout(&octx->channels, ocodec);
octx->bit_rate = as->aud_bitrate ? as->aud_bitrate : 0;
octx->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (!octx->sample_rate) {
tvherror("transcode", "%04X: audio encoder has no suitable sample rate!", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
} else {
tvhdebug("transcode", "%04X: using audio sample rate %d",
shortid(t), octx->sample_rate);
}
if (octx->sample_fmt == AV_SAMPLE_FMT_NONE) {
tvherror("transcode", "%04X: audio encoder has no suitable sample format!", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
} else {
tvhdebug("transcode", "%04X: using audio sample format %s",
shortid(t), av_get_sample_fmt_name(octx->sample_fmt));
}
if (!octx->channel_layout) {
tvherror("transcode", "%04X: audio encoder has no suitable channel layout!", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
} else {
av_get_channel_layout_string(layout_buf, sizeof (layout_buf), octx->channels, octx->channel_layout);
tvhdebug("transcode", "%04X: using audio channel layout %s",
shortid(t), layout_buf);
}
// Set flags and quality settings, if no bitrate was specified.
// The MPEG2 encoder only supports encoding with fixed bitrate.
// All AAC encoders should support encoding with fixed bitrate,
// but some don't support encoding with global_quality (vbr).
// All vorbis encoders support encoding with global_quality (vbr),
// but the built in vorbis encoder doesn't support fixed bitrate.
switch (ts->ts_type) {
case SCT_MPEG2AUDIO:
// use 96 kbit per channel as default
if (octx->bit_rate == 0) {
octx->bit_rate = octx->channels * 96000;
}
break;
case SCT_AAC:
octx->flags |= CODEC_FLAG_BITEXACT;
// use 64 kbit per channel as default
if (octx->bit_rate == 0) {
octx->bit_rate = octx->channels * 64000;
}
break;
case SCT_VORBIS:
// use vbr with quality setting as default
// and also use a user specified bitrate < 16 kbit as quality setting
if (octx->bit_rate == 0) {
octx->flags |= CODEC_FLAG_QSCALE;
octx->global_quality = 4 * FF_QP2LAMBDA;
} else if (t->t_props.tp_abitrate < 16) {
octx->flags |= CODEC_FLAG_QSCALE;
octx->global_quality = t->t_props.tp_abitrate * FF_QP2LAMBDA;
octx->bit_rate = 0;
}
break;
default:
break;
}
if (avcodec_open2(octx, ocodec, NULL) < 0) {
tvherror("transcode", "%04X: Unable to open %s encoder",
shortid(t), ocodec->name);
transcoder_stream_invalidate(ts);
goto cleanup;
}
as->fifo = av_audio_fifo_alloc(octx->sample_fmt, octx->channels, 1);
if (!as->fifo) {
tvherror("transcode", "%04X: Could not allocate fifo", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
as->resample_context = NULL;
as->last_sample_rate = ictx->sample_rate;
as->last_sample_fmt = ictx->sample_fmt;
as->last_channel_layout = ictx->channel_layout;
}
/* check for changed input format and close resampler, if changed */
if (as->last_sample_rate != ictx->sample_rate ||
as->last_sample_fmt != ictx->sample_fmt ||
as->last_channel_layout != ictx->channel_layout) {
tvhdebug("transcode", "%04X: audio input format changed", shortid(t));
as->last_sample_rate = ictx->sample_rate;
as->last_sample_fmt = ictx->sample_fmt;
as->last_channel_layout = ictx->channel_layout;
if (as->resample_context) {
tvhdebug("transcode", "%04X: stopping audio resampling", shortid(t));
avresample_free(&as->resample_context);
as->resample_context = NULL;
as->resample_is_open = 0;
}
}
as->resample = (ictx->channel_layout != octx->channel_layout) ||
(ictx->sample_fmt != octx->sample_fmt) ||
(ictx->sample_rate != octx->sample_rate);
if (as->resample) {
if (!as->resample_context) {
if (!(as->resample_context = avresample_alloc_context())) {
tvherror("transcode", "%04X: Could not allocate resample context", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
// resample audio
tvhdebug("transcode", "%04X: starting audio resampling", shortid(t));
av_get_channel_layout_string(layout_buf, sizeof (layout_buf), ictx->channels, ictx->channel_layout);
tvhdebug("transcode", "%04X: IN : channel_layout=%s, rate=%d, fmt=%s, bitrate=%d",
shortid(t), layout_buf, ictx->sample_rate,
av_get_sample_fmt_name(ictx->sample_fmt), ictx->bit_rate);
av_get_channel_layout_string(layout_buf, sizeof (layout_buf), octx->channels, octx->channel_layout);
tvhdebug("transcode", "%04X: OUT: channel_layout=%s, rate=%d, fmt=%s, bitrate=%d",
shortid(t), layout_buf, octx->sample_rate,
av_get_sample_fmt_name(octx->sample_fmt), octx->bit_rate);
if (transcode_opt_set_int(t, ts, as->resample_context,
"in_channel_layout", ictx->channel_layout, 1))
goto cleanup;
if (transcode_opt_set_int(t, ts, as->resample_context,
"out_channel_layout", octx->channel_layout, 1))
goto cleanup;
if (transcode_opt_set_int(t, ts, as->resample_context,
"in_sample_rate", ictx->sample_rate, 1))
goto cleanup;
if (transcode_opt_set_int(t, ts, as->resample_context,
"out_sample_rate", octx->sample_rate, 1))
goto cleanup;
if (transcode_opt_set_int(t, ts, as->resample_context,
"in_sample_fmt", ictx->sample_fmt, 1))
goto cleanup;
if (transcode_opt_set_int(t, ts, as->resample_context,
"out_sample_fmt", octx->sample_fmt, 1))
goto cleanup;
if (avresample_open(as->resample_context) < 0) {
tvherror("transcode", "%04X: Error avresample_open", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
as->resample_is_open = 1;
}
uint8_t **output = alloca(octx->channels * sizeof(uint8_t *));
if (av_samples_alloc(output, NULL, octx->channels, frame->nb_samples, octx->sample_fmt, 1) < 0) {
tvherror("transcode", "%04X: av_resamples_alloc failed", shortid(t));
transcoder_stream_invalidate(ts);
goto scleanup;
}
length = avresample_convert(as->resample_context, NULL, 0, frame->nb_samples,
frame->extended_data, 0, frame->nb_samples);
tvhtrace("transcode", "%04X: avresample_convert: %d", shortid(t), length);
while (avresample_available(as->resample_context) > 0) {
length = avresample_read(as->resample_context, output, frame->nb_samples);
if (length > 0) {
if (av_audio_fifo_realloc(as->fifo, av_audio_fifo_size(as->fifo) + length) < 0) {
tvhlog(LOG_ERR, "transcode", "%04X: Could not reallocate FIFO", shortid(t));
transcoder_stream_invalidate(ts);
goto scleanup;
}
if (av_audio_fifo_write(as->fifo, (void **)output, length) < length) {
tvhlog(LOG_ERR, "transcode", "%04X: Could not write to FIFO", shortid(t));
goto scleanup;
}
}
continue;
scleanup:
transcoder_stream_invalidate(ts);
av_freep(&output[0]);
goto cleanup;
}
av_freep(&output[0]);
/* Need to find out where we are going to do this. Normally at the end.
int delay_samples = avresample_get_delay(as->resample_context);
if (delay_samples) {
tvhlog(LOG_DEBUG, "transcode", "%d samples in resamples delay buffer.", delay_samples);
goto cleanup;
}
*/
} else {
if (av_audio_fifo_realloc(as->fifo, av_audio_fifo_size(as->fifo) + frame->nb_samples) < 0) {
tvherror("transcode", "%04X: Could not reallocate FIFO", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
if (av_audio_fifo_write(as->fifo, (void **)frame->extended_data, frame->nb_samples) < frame->nb_samples) {
tvherror("transcode", "%04X: Could not write to FIFO", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
}
as->aud_dec_pts += pkt->pkt_duration;
while (av_audio_fifo_size(as->fifo) >= octx->frame_size) {
tvhtrace("transcode", "%04X: audio loop: fifo=%d, frame=%d",
shortid(t), av_audio_fifo_size(as->fifo), octx->frame_size);
av_frame_free(&frame);
frame = av_frame_alloc();
frame->nb_samples = octx->frame_size;
frame->format = octx->sample_fmt;
#if USING_FFMPEG
frame->channels = octx->channels;
#endif
frame->channel_layout = octx->channel_layout;
frame->sample_rate = octx->sample_rate;
if (av_frame_get_buffer(frame, 0) < 0) {
tvherror("transcode", "%04X: Could not allocate output frame samples", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
if ((length = av_audio_fifo_read(as->fifo, (void **)frame->data, octx->frame_size)) != octx->frame_size) {
tvherror("transcode", "%04X: Could not read data from FIFO", shortid(t));
transcoder_stream_invalidate(ts);
goto cleanup;
}
tvhtrace("transcode", "%04X: pre-encode: linesize=%d, samples=%d, pts=%" PRIi64,
shortid(t), frame->linesize[0], length, as->aud_enc_pts);
frame->pts = as->aud_enc_pts;
as->aud_enc_pts += (octx->frame_size * 90000) / octx->sample_rate;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
length = avcodec_encode_audio2(octx, &packet, frame, &got_packet_ptr);
tvhtrace("transcode", "%04X: encoded: packet=%d, ret=%d, got=%d, pts=%" PRIi64,
shortid(t), packet.size, length, got_packet_ptr, packet.pts);
if ((length < 0) || (got_packet_ptr < -1)) {
tvherror("transcode", "%04X: Unable to encode audio (%d:%d)",
shortid(t), length, got_packet_ptr);
transcoder_stream_invalidate(ts);
goto cleanup;
} else if (got_packet_ptr && packet.pts >= 0) {
int extra_size = 0;
if (ts->ts_type == SCT_AAC) {
/* only if ADTS header is missing, create it */
if (packet.size < 2 || packet.data[0] != 0xff || (packet.data[1] & 0xf0) != 0xf0)
extra_size = 7;
}
n = pkt_alloc(NULL, packet.size + extra_size, packet.pts, packet.pts);
memcpy(pktbuf_ptr(n->pkt_payload) + extra_size, packet.data, packet.size);
n->pkt_componentindex = ts->ts_index;
n->pkt_channels = octx->channels;
n->pkt_sri = rate_to_sri(octx->sample_rate);
n->pkt_duration = packet.duration;
if (extra_size && ts->ts_type == SCT_AAC)
create_adts_header(n->pkt_payload, n->pkt_sri, octx->channels);
if (octx->extradata_size)
n->pkt_meta = pktbuf_alloc(octx->extradata, octx->extradata_size);
tvhtrace("transcode", "%04X: deliver audio (pts = %" PRIi64 ", delay = %i)",
shortid(t), n->pkt_pts, octx->delay);
sm = streaming_msg_create_pkt(n);
streaming_target_deliver2(ts->ts_target, sm);
pkt_ref_dec(n);
}
av_free_packet(&packet);
}
cleanup:
av_frame_free(&frame);
av_free_packet(&packet);
pkt_ref_dec(pkt);
}
/**
* Parse MPEG2 header, simplifier version (we know what ffmpeg/libav generates
*/
static void
extract_mpeg2_global_data(th_pkt_t *n, uint8_t *data, int len)
{
/*
From: http://en.wikipedia.org/wiki/Elementary_stream
Field Name # of bits Description
start code 32 0x000001B3
Horizontal Size 12
Vertical Size 12
Aspect ratio 4
Frame rate code 4
Bit rate 18 Actual bit rate = bit rate * 400, rounded upwards. Use 0x3FFFF for variable bit rate.
Marker bit 1 Always 1.
VBV buf size 10 Size of video buffer verifier = 16*1024*vbv buf size
constrained parameters flag 1
load intra quantizer matrix 1 If bit set then intra quantizer matrix follows, otherwise use default values.
intra quantizer matrix 0 or 64*8
load non intra quantizer matrix 1 If bit set then non intra quantizer matrix follows.
non intra quantizer matrix 0 or 64*8
Minimal of 12 bytes.
*/
int hs = 12;
if (len >= hs && RB32(data) == 0x000001b3) { // SEQ_START_CODE
// load intra quantizer matrix
if (data[hs-1] & 0x02) {
if (hs + 64 < len) return;
hs += 64;
}
// load non intra quantizer matrix
if (data[hs-1] & 0x01) {
if (hs + 64 < len) return;
hs += 64;
}
// See if we have the first EXT_START_CODE. Normally 10 bytes
// https://git.libav.org/?p=libav.git;a=blob;f=libavcodec/mpeg12enc.c;h=3376f1075f4b7582a8e4556e98deddab3e049dab;hb=HEAD#l272
if (hs + 10 <= len && RB32(data + hs) == 0x000001b5) // EXT_START_CODE
hs += 10;
// See if we have the second EXT_START_CODE. Normally 12 bytes
// https://git.libav.org/?p=libav.git;a=blob;f=libavcodec/mpeg12enc.c;h=3376f1075f4b7582a8e4556e98deddab3e049dab;hb=HEAD#l291
// ffmpeg libs might have this block missing
if (hs + 12 <= len && RB32(data + hs) == 0x000001b5) // EXT_START_CODE
hs += 12;
// See if we have the second GOP_START_CODE. Normally 31 bits == 4 bytes
// https://git.libav.org/?p=libav.git;a=blob;f=libavcodec/mpeg12enc.c;h=3376f1075f4b7582a8e4556e98deddab3e049dab;hb=HEAD#l304
if (hs + 4 <= len && RB32(data + hs) == 0x000001b8) // GOP_START_CODE
hs += 4;
n->pkt_meta = pktbuf_alloc(data, hs);
}
}
/**
*
*/
static void
send_video_packet(transcoder_t *t, transcoder_stream_t *ts, th_pkt_t *pkt,
AVPacket *epkt, AVCodecContext *octx)
{
video_stream_t *vs = (video_stream_t*)ts;
streaming_message_t *sm;
th_pkt_t *n;
if (epkt->size <= 0) {
if (epkt->size) {
tvherror("transcode", "%04X: Unable to encode video (%d)", shortid(t), epkt->size);
transcoder_stream_invalidate(ts);
}
return;
}
if (!octx->coded_frame)
return;
if ((ts->ts_type == SCT_H264 || ts->ts_type == SCT_HEVC) &&
octx->extradata_size &&
(ts->ts_first || octx->coded_frame->pict_type == AV_PICTURE_TYPE_I)) {
n = pkt_alloc(NULL, octx->extradata_size + epkt->size, epkt->pts, epkt->dts);
memcpy(pktbuf_ptr(n->pkt_payload), octx->extradata, octx->extradata_size);
memcpy(pktbuf_ptr(n->pkt_payload) + octx->extradata_size, epkt->data, epkt->size);
ts->ts_first = 0;
} else {
n = pkt_alloc(epkt->data, epkt->size, epkt->pts, epkt->dts);
}
switch (octx->coded_frame->pict_type) {
case AV_PICTURE_TYPE_I:
n->pkt_frametype = PKT_I_FRAME;
break;
case AV_PICTURE_TYPE_P:
n->pkt_frametype = PKT_P_FRAME;
break;
case AV_PICTURE_TYPE_B:
n->pkt_frametype = PKT_B_FRAME;
break;
default:
break;
}
n->pkt_duration = pkt->pkt_duration;
n->pkt_commercial = pkt->pkt_commercial;
n->pkt_componentindex = pkt->pkt_componentindex;
n->pkt_field = pkt->pkt_field;
n->pkt_aspect_num = pkt->pkt_aspect_num;
n->pkt_aspect_den = pkt->pkt_aspect_den;
if(octx->coded_frame && octx->coded_frame->pts != AV_NOPTS_VALUE) {
if(n->pkt_dts != PTS_UNSET)
n->pkt_dts -= n->pkt_pts;
n->pkt_pts = octx->coded_frame->pts;
if(n->pkt_dts != PTS_UNSET)
n->pkt_dts += n->pkt_pts;
}
if (octx->extradata_size) {
n->pkt_meta = pktbuf_alloc(octx->extradata, octx->extradata_size);
} else {
if (octx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
extract_mpeg2_global_data(n, epkt->data, epkt->size);
}