/
ff_mx.c
1590 lines (1406 loc) · 49.1 KB
/
ff_mx.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom Paris 2019-2023
* All rights reserved
*
* This file is part of GPAC / ffmpeg muxer filter
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <gpac/setup.h>
#ifdef GPAC_HAS_FFMPEG
#include "ff_common.h"
#include <gpac/network.h>
#include <gpac/bitstream.h>
#if (LIBAVFORMAT_VERSION_MAJOR <= 56)
#undef GPAC_HAS_FFMPEG
#endif
#endif
#ifdef GPAC_HAS_FFMPEG
#if (LIBAVCODEC_VERSION_MAJOR>58)
#include <libavutil/mastering_display_metadata.h>
#endif
typedef struct
{
AVStream *stream;
Bool ts_rescale;
AVRational in_scale;
u32 in_timescale;
Bool in_seg_flush;
u32 cts_shift;
s64 ts_shift;
Bool ready;
Bool suspended;
Bool reconfig_stream;
Bool webvtt;
} GF_FFMuxStream;
enum{
FFMX_STATE_ALLOC=0,
FFMX_STATE_AVIO_OPEN,
FFMX_STATE_HDR_DONE,
FFMX_STATE_TRAILER_DONE,
FFMX_STATE_EOS,
FFMX_STATE_ERROR
};
enum
{
FFMX_INJECT_DSI = 1,
FFMX_INJECT_VID_INFO = 1<<1,
FFMX_INJECT_AUD_INFO = 1<<2
};
typedef struct
{
//options
char *dst, *mime, *ffmt, *ext;
Double start, speed;
u32 block_size;
Bool nodisc, ffiles, noinit, keepts;
GF_Fraction ileave;
AVFormatContext *muxer;
//decode options
AVDictionary *options;
GF_List *streams;
//0 not created, 1 created, header written, 2 finalized, trailer written, 3: end of stream
u32 status;
Bool dash_mode, init_done;
u32 nb_pck_in_seg;
u32 dash_seg_num;
u64 offset_at_seg_start;
u8 *avio_ctx_buffer;
AVIOContext *avio_ctx;
FILE *gfio;
u32 cur_file_idx_plus_one;
u32 probe_init;
u64 ts_regulate;
#if (LIBAVCODEC_VERSION_MAJOR < 59)
AVPacket pkt;
#else
AVPacket *pkt;
#endif
} GF_FFMuxCtx;
static GF_Err ffmx_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_remove);
static GF_Err ffmx_init_mux(GF_Filter *filter, GF_FFMuxCtx *ctx)
{
u32 i, nb_pids;
u64 min_ts=0;
u64 min_delay_skip=0;
u32 min_delay_ts=0;
u32 min_timescale=1000;
int res;
gf_assert(ctx->status==FFMX_STATE_AVIO_OPEN);
ctx->status = FFMX_STATE_HDR_DONE;
ctx->probe_init = 0;
AVDictionary *options = NULL;
av_dict_copy(&options, ctx->options, 0);
res = avformat_init_output(ctx->muxer, &options);
if (res<0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to open %s - error %s\n", ctx->dst, av_err2str(res) ));
ctx->status = FFMX_STATE_ERROR;
if (options) av_dict_free(&options);
gf_filter_abort(filter);
return GF_NOT_SUPPORTED;
}
nb_pids = gf_filter_get_ipid_count(filter);
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
GF_FFMuxStream *st = gf_filter_pid_get_udta(ipid);
if (!st) continue;
if ((st->in_scale.den == st->stream->time_base.den) && (st->in_scale.num == st->stream->time_base.num)) {
st->ts_rescale = GF_FALSE;
} else {
st->ts_rescale = GF_TRUE;
}
if (!ctx->keepts) {
GF_FilterPacket *pck = gf_filter_pid_get_packet(ipid);
u64 ts = gf_filter_pck_get_dts(pck);
if (ts == GF_FILTER_NO_TS) ts = gf_filter_pck_get_cts(pck);
if (ts == GF_FILTER_NO_TS) continue;
if (!min_ts || (min_ts * st->in_scale.den > ts * min_timescale)) {
min_ts = ts;
min_timescale = st->in_scale.den;
}
}
const GF_PropertyValue *p = gf_filter_pid_get_property(ipid, GF_PROP_PID_DELAY);
if (p) {
if (p->value.longsint < 0) {
if (!min_delay_skip || (gf_timestamp_greater(-p->value.longsint, st->in_scale.den, min_delay_skip, min_delay_ts))) {
min_delay_skip = -p->value.longsint;
min_delay_ts = st->in_scale.den;
}
}
}
}
const GF_PropertyValue *chap_t=NULL;
const GF_PropertyValue *chap_n=NULL;
u64 max_dur=0;
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
GF_FFMuxStream *st = gf_filter_pid_get_udta(ipid);
//get pid delay
const GF_PropertyValue *p = gf_filter_pid_get_property(ipid, GF_PROP_PID_DELAY);
s64 delay = p ? p->value.longsint : 0;
//add min skip, delay is now >=0
delay += gf_timestamp_rescale(min_delay_skip, min_delay_ts, st->in_scale.den);
//may happen due to rounding
if (delay<0) delay=0;
st->ts_shift = delay;
if (!ctx->keepts) {
st->ts_shift = st->ts_shift - gf_timestamp_rescale(min_ts, min_timescale, st->in_scale.den);
}
chap_t = gf_filter_pid_get_property(ipid, GF_PROP_PID_CHAP_TIMES);
chap_n = gf_filter_pid_get_property(ipid, GF_PROP_PID_CHAP_NAMES);
if (chap_t && chap_n && (chap_t->value.uint_list.nb_items == chap_n->value.string_list.nb_items)) {
} else {
chap_t = chap_n = NULL;
}
p = gf_filter_pid_get_property(ipid, GF_PROP_PID_DURATION);
if (p) {
u64 d = gf_timestamp_rescale(p->value.lfrac.num, p->value.lfrac.den, 1000);
if (max_dur<d) max_dur = d;
}
}
if (chap_t) {
ctx->muxer->nb_chapters = chap_t->value.uint_list.nb_items;
ctx->muxer->chapters = av_malloc(sizeof(AVChapter*)* ctx->muxer->nb_chapters);
for (i=0; i<ctx->muxer->nb_chapters; i++) {
AVChapter *ch = av_malloc(sizeof(AVChapter));
memset(ch, 0, sizeof(AVChapter));
ctx->muxer->chapters[i] = ch;
ch->start = chap_t->value.uint_list.vals[i];
if (i+1<ctx->muxer->nb_chapters)
ch->end = chap_t->value.uint_list.vals[i+1];
else if (max_dur)
ch->end = max_dur;
else
ch->end = ch->start;
if (ch->end < ch->start) ch->end = ch->start;
ch->id = i+1;
ch->time_base.num = 1;
ch->time_base.den = 1000;
av_dict_set(&ch->metadata, "title", chap_n->value.string_list.vals[i], 0);
}
}
res = avformat_write_header(ctx->muxer, &options);
if (res<0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to write header for %s - error %s\n", ctx->dst, av_err2str(res) ));
ctx->status = FFMX_STATE_ERROR;
if (options) av_dict_free(&options);
gf_filter_abort(filter);
return GF_SERVICE_ERROR;
}
ffmpeg_report_options(filter, options, ctx->options);
return GF_OK;
}
#if (LIBAVFORMAT_VERSION_MAJOR < 61)
static int ffavio_write_packet(void *opaque, uint8_t *buf, int buf_size)
#else
static int ffavio_write_packet(void *opaque, const uint8_t *buf, int buf_size)
#endif
{
GF_FFMuxCtx *ctx = (GF_FFMuxCtx *)opaque;
return (int) gf_fwrite(buf, buf_size, ctx->gfio);
}
static int64_t ffavio_seek(void *opaque, int64_t offset, int whence)
{
GF_FFMuxCtx *ctx = (GF_FFMuxCtx *)opaque;
if (whence==AVSEEK_SIZE) {
u64 pos = gf_ftell(ctx->gfio);
u64 size = gf_fsize(ctx->gfio);
gf_fseek(ctx->gfio, pos, SEEK_SET);
return size;
}
return (int64_t) gf_fseek(ctx->gfio, offset, whence);
}
static GF_Err ffmx_open_url(GF_FFMuxCtx *ctx, char *final_name)
{
const char *dst;
Bool use_gfio=GF_FALSE;
const AVOutputFormat *ofmt = ctx->muxer->oformat;
dst = final_name ? final_name : ctx->dst;
if (!strncmp(dst, "gfio://", 7)) {
use_gfio = GF_TRUE;
}
if (ofmt) {
//handle local urls some_dir/some_other_dir/file.ext, ffmpeg doesn't create the dirs for us
if (! (ofmt->flags & AVFMT_NOFILE) && gf_url_is_local(dst)) {
FILE *f = gf_fopen(dst, "wb");
gf_fclose(f);
}
}
if (use_gfio) {
ctx->gfio = gf_fopen(dst, "wb");
if (!ctx->gfio) return GF_URL_ERROR;
ctx->avio_ctx_buffer = av_malloc(ctx->block_size);
if (!ctx->avio_ctx_buffer) return GF_OUT_OF_MEM;
ctx->avio_ctx = avio_alloc_context(ctx->avio_ctx_buffer, ctx->block_size,
1, ctx, NULL, &ffavio_write_packet, &ffavio_seek);
if (!ctx->avio_ctx) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Failed to create AVIO context for %s\n", dst));
return GF_OUT_OF_MEM;
}
ctx->muxer->pb = ctx->avio_ctx;
} else if (!ofmt || !(ofmt->flags & AVFMT_NOFILE) ) {
int res = avio_open2(&ctx->muxer->pb, dst, AVIO_FLAG_WRITE, NULL, &ctx->options);
if (res<0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to open AVIO context for %s - error %s\n", dst, av_err2str(res) ));
return GF_FILTER_NOT_SUPPORTED;
}
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
strncpy(ctx->muxer->filename, dst, 1023);
ctx->muxer->filename[1023]=0;
#else
av_freep(&ctx->muxer->url);
ctx->muxer->url = av_strdup(dst);
#endif
}
ctx->status = FFMX_STATE_AVIO_OPEN;
return GF_OK;
}
static GF_Err ffmx_initialize_ex(GF_Filter *filter, Bool use_templates)
{
const char *url, *sep;
const AVOutputFormat *ofmt;
GF_FFMuxCtx *ctx = (GF_FFMuxCtx *) gf_filter_get_udta(filter);
if (!ctx->dst) return GF_BAD_PARAM;
ffmpeg_setup_logs(GF_LOG_CONTAINER);
#if (LIBAVCODEC_VERSION_MAJOR >= 59)
if (!ctx->pkt)
ctx->pkt = av_packet_alloc();
#endif
ctx->muxer = avformat_alloc_context();
if (!ctx->muxer) return GF_OUT_OF_MEM;
if (!ctx->streams)
ctx->streams = gf_list_new();
url = ctx->dst;
if (!strncmp(ctx->dst, "gfio://", 7)) {
url = gf_fileio_translate_url(ctx->dst);
}
sep = strchr(url, '$');
if (sep && strchr(sep+1, '$'))
use_templates = GF_TRUE;
ofmt = av_guess_format(ctx->ext ? ctx->ext : ctx->ffmt, url, ctx->mime);
//if protocol is present, we may fail at guessing the format
if (!ofmt && !ctx->ffmt) {
u32 len;
char szProto[20];
char *proto = strstr(url, "://");
if (!proto)
return GF_FILTER_NOT_SUPPORTED;
szProto[19] = 0;
len = (u32) (proto - url);
if (len>19) len=19;
strncpy(szProto, url, len);
szProto[len] = 0;
ofmt = av_guess_format(szProto, url, ctx->mime);
}
if (!ofmt) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Failed to guess output format for %s, cannot run\n", ctx->dst));
if (!ctx->ffmt) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Try specifying the format through :ffmt option\n"));
}
return GF_SERVICE_ERROR;
}
ctx->muxer->oformat = FF_OFMT_CAST ofmt;
ctx->status = FFMX_STATE_ALLOC;
if (gf_filter_is_temporary(filter)) {
gf_filter_meta_set_instances(filter, ofmt->name);
return GF_OK;
}
//templates are used, we need to postpone opening the url until we have a PID and a first packet
if (use_templates)
return GF_OK;
return ffmx_open_url(ctx, NULL);
}
static GF_Err ffmx_initialize(GF_Filter *filter)
{
GF_FFMuxCtx *ctx = (GF_FFMuxCtx *) gf_filter_get_udta(filter);
if (ctx->ileave.num<=0) {
ctx->ileave.num = 0;
} else {
ctx->ts_regulate = ctx->ileave.num;
}
return ffmx_initialize_ex(filter, GF_FALSE);
}
static GF_Err ffmx_start_seg(GF_Filter *filter, GF_FFMuxCtx *ctx, const char *seg_name)
{
int i;
int res;
/*create a new output context, clone all streams from current one and swap output context*/
if (ctx->ffiles) {
AVFormatContext *segmux;
res = avformat_alloc_output_context2(&segmux, ctx->muxer->oformat, NULL, NULL);
if (res<0) {
return GF_IO_ERR;
}
segmux->interrupt_callback = ctx->muxer->interrupt_callback;
segmux->max_delay = ctx->muxer->max_delay;
av_dict_copy(&segmux->metadata, ctx->muxer->metadata, 0);
segmux->opaque = ctx->muxer->opaque;
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
segmux->io_close = ctx->muxer->io_close;
#else
segmux->io_close2 = ctx->muxer->io_close2;
#endif
segmux->io_open = ctx->muxer->io_open;
segmux->flags = ctx->muxer->flags;
for (i=0; i< (s32) ctx->muxer->nb_streams; i++) {
u32 j;
AVStream *st;
AVCodecParameters *ipar, *opar;
if (!(st = avformat_new_stream(segmux, NULL))) {
avformat_free_context(segmux);
return GF_OUT_OF_MEM;
}
ipar = ctx->muxer->streams[i]->codecpar;
opar = st->codecpar;
avcodec_parameters_copy(opar, ipar);
if (!segmux->oformat->codec_tag ||
av_codec_get_id (segmux->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
av_codec_get_tag(segmux->oformat->codec_tag, ipar->codec_id) <= 0) {
opar->codec_tag = ipar->codec_tag;
} else {
opar->codec_tag = 0;
}
st->sample_aspect_ratio = ctx->muxer->streams[i]->sample_aspect_ratio;
st->time_base = ctx->muxer->streams[i]->time_base;
st->avg_frame_rate = ctx->muxer->streams[i]->avg_frame_rate;
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
if (ctx->muxer->streams[i]->codecpar->codec_tag == MKTAG('t','m','c','d'))
st->codec->time_base = ctx->muxer->streams[i]->codec->time_base;
#endif
av_dict_copy(&st->metadata, ctx->muxer->streams[i]->metadata, 0);
/*reassign stream*/
for (j=0; j<gf_list_count(ctx->streams); j++) {
GF_FFMuxStream *ast = gf_list_get(ctx->streams, j);
if (ast->stream == ctx->muxer->streams[i]) {
ast->stream = st;
break;
}
}
}
//swap context
avformat_free_context(ctx->muxer);
ctx->muxer = segmux;
} else {
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
ctx->muxer->io_close(ctx->muxer, ctx->muxer->pb);
#else
ctx->muxer->io_close2(ctx->muxer, ctx->muxer->pb);
#endif
ctx->muxer->pb = NULL;
}
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
strncpy(ctx->muxer->filename, seg_name, 1023);
ctx->muxer->filename[1023]=0;
#else
av_freep(&ctx->muxer->url);
ctx->muxer->url = av_strdup(seg_name);
#endif
ctx->offset_at_seg_start = 0;
if (!(ctx->muxer->oformat->flags & AVFMT_NOFILE)) {
//handle local urls some_dir/some_other_dir/file.ext, ffmpeg doesn't create the dirs for us
if (gf_url_is_local(seg_name)) {
FILE *f = gf_fopen(seg_name, "wb");
gf_fclose(f);
}
res = ctx->muxer->io_open(ctx->muxer, &ctx->muxer->pb, /*ctx->muxer->url*/seg_name, AVIO_FLAG_WRITE, NULL);
if (res < 0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to open new segment %s - error %s\n", seg_name, av_err2str(res) ));
return GF_IO_ERR;
}
}
ctx->muxer->pb->seekable = 0;
if (ctx->muxer->oformat->priv_class && ctx->muxer->priv_data)
av_opt_set(ctx->muxer->priv_data, "mpegts_flags", "+resend_headers", 0);
if (ctx->ffiles) {
AVDictionary *options = NULL;
av_dict_copy(&options, ctx->options, 0);
av_dict_set(&options, "fflags", "-autobsf", 0);
res = avformat_write_header(ctx->muxer, &options);
if (options) av_dict_free(&options);
if (res < 0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to configure segment %s - error %s\n", seg_name, av_err2str(res) ));
return GF_IO_ERR;
}
}
ctx->status = FFMX_STATE_HDR_DONE;
return GF_OK;
}
static GF_Err ffmx_close_seg(GF_Filter *filter, GF_FFMuxCtx *ctx, Bool send_evt_only)
{
GF_FilterEvent evt;
GF_FilterPid *pid;
av_write_frame(ctx->muxer, NULL);
if (!send_evt_only) {
int res=0;
if (ctx->ffiles) {
if (ctx->status==FFMX_STATE_HDR_DONE) {
res = av_write_trailer(ctx->muxer);
} else {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Invalid state %d for segment %s close\n", ctx->status, AVFMT_URL(ctx->muxer) ));
return GF_SERVICE_ERROR;
}
}
if (res<0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to flush segment %s - error %s\n", AVFMT_URL(ctx->muxer), av_err2str(res) ));
return GF_SERVICE_ERROR;
}
ctx->status = FFMX_STATE_TRAILER_DONE;
}
ctx->nb_pck_in_seg = 0;
pid = gf_filter_get_ipid(filter, 0);
GF_FEVT_INIT(evt, GF_FEVT_SEGMENT_SIZE, pid);
evt.seg_size.seg_url = NULL;
//init seg
if (!ctx->init_done) {
evt.seg_size.is_init = 1;
ctx->init_done = GF_TRUE;
} else {
evt.seg_size.is_init = 0;
}
evt.seg_size.media_range_start = ctx->offset_at_seg_start;
#if LIBAVFORMAT_VERSION_MAJOR < 59
evt.seg_size.media_range_end = ctx->muxer->pb ? (ctx->muxer->pb->written-1) : 0;
#else
evt.seg_size.media_range_end = ctx->muxer->pb ? (ctx->muxer->pb->bytes_written-1) : 0;
#endif
ctx->offset_at_seg_start = evt.seg_size.media_range_end;
gf_filter_pid_send_event(pid, &evt);
return GF_OK;
}
void ffmx_inject_config(GF_FilterPid *pid, GF_FFMuxStream *st, AVPacket *pkt)
{
const GF_PropertyValue *p;
u8 *data;
if (st->reconfig_stream & FFMX_INJECT_DSI) {
p = gf_filter_pid_get_property(pid, GF_PROP_PID_DECODER_CONFIG);
if (p && (p->type==GF_PROP_DATA) && p->value.data.ptr) {
u32 dsi_size = p->value.data.size;
u32 codec_id = 0;
const GF_PropertyValue *cid = gf_filter_pid_get_property(pid, GF_PROP_PID_CODECID);
if (cid) codec_id = cid->value.uint;
if (codec_id==GF_CODECID_FLAC) {
dsi_size-=4;
} else if (codec_id==GF_CODECID_OPUS) {
dsi_size+=8;
}
data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, dsi_size);
if (data) {
if (codec_id==GF_CODECID_FLAC) {
memcpy(data, p->value.data.ptr+4, p->value.data.size-4);
} else if (codec_id==GF_CODECID_OPUS) {
memcpy(data, "OpusHead", 8);
memcpy(data+8, p->value.data.ptr, p->value.data.size);
} else {
memcpy(data, p->value.data.ptr, p->value.data.size);
}
}
}
}
if (st->reconfig_stream & FFMX_INJECT_VID_INFO) {
u32 w, h;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_WIDTH);
w = p ? p->value.uint : st->stream->codecpar->width;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_WIDTH);
h = p ? p->value.uint : st->stream->codecpar->height;
data = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, 12);
if (data) {
GF_BitStream *bs = gf_bs_new(data, 12, GF_BITSTREAM_WRITE);
gf_bs_write_u32_le(bs, AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS);
gf_bs_write_u32_le(bs, w);
gf_bs_write_u32_le(bs, h);
gf_bs_del(bs);
}
}
if (st->reconfig_stream & FFMX_INJECT_AUD_INFO) {
u32 sr, size=12;
u32 flags = AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE;
#ifdef FFMPEG_OLD_CHLAYOUT
flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT;
u64 ch_layout=0;
u32 ch;
#endif
p = gf_filter_pid_get_property(pid, GF_PROP_PID_SAMPLE_RATE);
sr = p ? p->value.uint : st->stream->codecpar->sample_rate;
#ifdef FFMPEG_OLD_CHLAYOUT
p = gf_filter_pid_get_property(pid, GF_PROP_PID_NUM_CHANNELS);
ch = p ? p->value.uint : st->stream->codecpar->channels;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_CHANNEL_LAYOUT);
if (p) {
ch_layout = ffmpeg_channel_layout_from_gpac(p->value.uint);
} else {
ch_layout = st->stream->codecpar->channel_layout;
}
if (ch_layout) {
size += 8;
flags |= AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT;
}
#endif
data = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, size);
if (data) {
GF_BitStream *bs = gf_bs_new(data, 12, GF_BITSTREAM_WRITE);
gf_bs_write_u32_le(bs, flags);
#ifdef FFMPEG_OLD_CHLAYOUT
gf_bs_write_u32_le(bs, ch);
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT)
gf_bs_write_u64_le(bs, ch_layout);
#endif
gf_bs_write_u32_le(bs, sr);
gf_bs_del(bs);
}
}
}
static void ffmx_inject_webvtt(GF_FilterPacket *ipck, AVPacket *pkt)
{
u32 len;
u8 *data;
const GF_PropertyValue *p;
const char *vtt_pre=NULL, *vtt_cueid=NULL, *vtt_settings=NULL;
p = gf_filter_pck_get_property_str(ipck, "vtt_pre");
if (p) vtt_pre = p->value.string;
p = gf_filter_pck_get_property_str(ipck, "vtt_cueid");
if (p) vtt_cueid = p->value.string;
p = gf_filter_pck_get_property_str(ipck, "vtt_settings");
if (p) vtt_settings = p->value.string;
if (vtt_cueid) {
len = (u32) strlen(vtt_cueid);
data = av_packet_new_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER, len);
if (data) memcpy(data, vtt_cueid, len);
}
if (vtt_settings) {
len = (u32) strlen(vtt_settings);
data = av_packet_new_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS, len);
if (data) memcpy(data, vtt_settings, len);
}
if (vtt_pre) {
len = (u32) strlen(vtt_pre);
data = av_packet_new_side_data(pkt, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, len+8);
if (data) {
//set additional blockID = 1 (64 BE int)
memset(data, 0, 7);
data[7] = 1;
memcpy(data+8, vtt_pre, len);
}
}
}
static GF_Err ffmx_process(GF_Filter *filter)
{
GF_Err e = GF_OK;
GF_FFMuxCtx *ctx = (GF_FFMuxCtx *) gf_filter_get_udta(filter);
u32 nb_done, nb_segs_done, nb_suspended, i, nb_pids = gf_filter_get_ipid_count(filter);
if (ctx->status<FFMX_STATE_HDR_DONE) {
Bool all_ready = GF_TRUE;
Bool needs_reinit = (ctx->status==FFMX_STATE_ALLOC) ? GF_TRUE : GF_FALSE;
//potpone until no pending connections so that we don't write header before all streams are declared
if (gf_filter_connections_pending(filter))
return GF_OK;
if (!ctx->probe_init) ctx->probe_init = gf_sys_clock();
else if (gf_sys_clock() - ctx->probe_init > 10000) {
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
gf_filter_pid_set_discard(ipid, GF_TRUE);
}
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Could not initialize stream list in 10s, aborting\n"));
return GF_SERVICE_ERROR;
}
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
GF_FilterPacket *ipck = gf_filter_pid_get_packet(ipid);
GF_FFMuxStream *st = gf_filter_pid_get_udta(ipid);
if (!st) continue;
if (!st->ready) {
const GF_PropertyValue *p = gf_filter_pid_get_property(ipid, GF_PROP_PID_DECODER_CONFIG);
if (!p) return GF_OK;
}
if (needs_reinit || !st->ready) {
e = ffmx_configure_pid(filter, ipid, GF_FALSE);
if (e) return e;
}
if (!ipck) {
//don't wait for subtitles & other streams
if ((st->stream->codecpar->codec_type==AVMEDIA_TYPE_VIDEO)
|| (st->stream->codecpar->codec_type==AVMEDIA_TYPE_VIDEO)
) {
all_ready = GF_FALSE;
}
continue;
}
if (st->stream->codecpar->codec_type==AVMEDIA_TYPE_VIDEO) {
u8 ilced = gf_filter_pck_get_interlaced(ipck);
if (ilced==2) st->stream->codecpar->field_order = AV_FIELD_BB;
else if (ilced==1) st->stream->codecpar->field_order = AV_FIELD_TT;
else st->stream->codecpar->field_order = AV_FIELD_PROGRESSIVE;
}
else if (st->stream->codecpar->codec_type==AVMEDIA_TYPE_AUDIO) {
st->stream->codecpar->seek_preroll = gf_filter_pck_get_roll_info(ipck);
}
}
if (!all_ready || (ctx->status==FFMX_STATE_ALLOC)) return GF_OK;
//good to go, finalize mux
ffmx_init_mux(filter, ctx);
}
if (ctx->status==FFMX_STATE_EOS) return GF_EOS;
else if (ctx->status==FFMX_STATE_ERROR) return GF_SERVICE_ERROR;
u32 nb_skip = 0;
nb_segs_done = 0;
nb_done = 0;
nb_suspended = 0;
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
GF_FFMuxStream *st = gf_filter_pid_get_udta(ipid);
if (!st) continue;
if (st->in_seg_flush) {
nb_segs_done ++;
continue;
}
if (st->suspended) {
nb_suspended++;
continue;
}
while (1) {
u64 cts;
int res;
const GF_PropertyValue *p;
u32 sap;
AVPacket *pkt;
GF_FilterPacket *ipck = gf_filter_pid_get_packet(ipid);
if (!ipck) {
if (gf_filter_pid_is_eos(ipid) && !gf_filter_pid_is_flush_eos(ipid)) {
nb_done++;
}
break;
}
cts = gf_filter_pck_get_cts(ipck);
if (cts == GF_FILTER_NO_TS) {
p = gf_filter_pck_get_property(ipck, GF_PROP_PCK_EODS);
if (p && p->value.boolean) {
st->in_seg_flush = GF_TRUE;
nb_segs_done ++;
nb_done++;
gf_filter_pid_drop_packet(ipid);
break;
}
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Packet with no CTS assigned, cannot store to track, ignoring\n"));
gf_filter_pid_drop_packet(ipid);
continue;
}
//ahead of our interleaving window, don't write yet - av_interleaved_write_frame is not enough for performing regulation
if (ctx->ileave.num && gf_timestamp_greater(cts+st->ts_shift, st->in_timescale, ctx->ts_regulate, ctx->ileave.den)) {
nb_skip++;
break;
}
p = gf_filter_pck_get_property(ipck, GF_PROP_PCK_FILENUM);
if (ctx->dash_mode) {
if (p) {
//flush segment
if (ctx->dash_seg_num && (p->value.uint != ctx->dash_seg_num)) {
st->in_seg_flush = GF_TRUE;
nb_segs_done++;
break;
}
ctx->dash_seg_num = p->value.uint;
//segment done, check if we switch file
p = gf_filter_pck_get_property(ipck, GF_PROP_PCK_FILENAME);
if (p && p->value.string) {
ffmx_close_seg(filter, ctx, GF_FALSE);
ffmx_start_seg(filter, ctx, p->value.string);
}
//otherwise close segment for event processing only
else
ffmx_close_seg(filter, ctx, GF_TRUE);
}
} else if (p) {
if (ctx->cur_file_idx_plus_one == p->value.uint+1) {
} else if (!st->suspended) {
st->suspended = GF_TRUE;
nb_suspended++;
break;
}
}
FF_INIT_PCK(ctx, pkt)
pkt->stream_index = st->stream->index;
pkt->dts = gf_filter_pck_get_dts(ipck);
pkt->pts = gf_filter_pck_get_cts(ipck);
if (st->cts_shift) pkt->pts += st->cts_shift;
if (pkt->dts > pkt->pts) {
st->cts_shift += (u32) (pkt->dts - pkt->pts);
GF_LOG(GF_LOG_INFO, GF_LOG_CONTAINER, ("[FFMux] Negative CTS offset -%d found, adjusting offset\n", st->cts_shift));
pkt->pts = pkt->dts;
}
pkt->duration = gf_filter_pck_get_duration(ipck);
sap = gf_filter_pck_get_sap(ipck);
if (sap==GF_FILTER_SAP_1) pkt->flags = AV_PKT_FLAG_KEY;
pkt->data = (u8 *) gf_filter_pck_get_data(ipck, &pkt->size);
if (st->ts_shift) {
pkt->dts = pkt->dts + st->ts_shift;
pkt->pts = pkt->pts + st->ts_shift;
}
if (st->ts_rescale) {
av_packet_rescale_ts(pkt, st->in_scale, st->stream->time_base);
}
if (st->reconfig_stream) {
ffmx_inject_config(ipid, st, pkt);
} else if (st->webvtt) {
ffmx_inject_webvtt(ipck, pkt);
}
if (ctx->ileave.num) {
res = av_interleaved_write_frame(ctx->muxer, pkt);
} else {
res = av_write_frame(ctx->muxer, pkt);
}
if (pkt->side_data) av_packet_free_side_data(pkt);
st->reconfig_stream = 0;
gf_filter_pid_drop_packet(ipid);
ctx->nb_pck_in_seg++;
FF_RELEASE_PCK(pkt)
if (res<0) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Fail to write packet %sto %s: error %s - aborting\n", st->reconfig_stream ? "with reconfig side data " : "", AVFMT_URL(ctx->muxer), av_err2str(res) ));
e = GF_IO_ERR;
ctx->status = FFMX_STATE_ERROR;
gf_filter_abort(filter);
}
}
}
if (nb_skip + nb_done >= nb_pids) {
ctx->ts_regulate += ctx->ileave.num;
}
//done writing file
if (nb_suspended && (nb_suspended==nb_pids)) {
int res = av_write_trailer(ctx->muxer);
if (res) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Failed to write trailer for %s: %s\n", ctx->dst, av_err2str(res) ));
e = GF_SERVICE_ERROR;
}
if (ctx->muxer) avformat_free_context(ctx->muxer);
ctx->muxer = NULL;
ctx->status = FFMX_STATE_ALLOC;
ffmx_initialize_ex(filter, GF_TRUE);
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
GF_FFMuxStream *st = gf_filter_pid_get_udta(ipid);
if (!st) continue;
st->suspended = GF_FALSE;
st->stream = 0;
ffmx_configure_pid(filter, ipid, GF_FALSE);
}
nb_done = 0;
}
//done writing segment
if (nb_segs_done==nb_pids) {
ctx->dash_seg_num = 0;
for (i=0; i<nb_pids; i++) {
GF_FilterPid *ipid = gf_filter_get_ipid(filter, i);
GF_FFMuxStream *st = gf_filter_pid_get_udta(ipid);
if (!st) continue;
st->in_seg_flush = GF_FALSE;
}
}
if (e) return e;
if (nb_done==nb_pids) {
if (ctx->status==FFMX_STATE_HDR_DONE) {
if (ctx->dash_mode) {
ffmx_close_seg(filter, ctx, GF_FALSE);
} else {
int res = av_write_trailer(ctx->muxer);
if (res) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Failed to write trailer for %s: %s\n", ctx->dst, av_err2str(res) ));
return GF_SERVICE_ERROR;
}
}
ctx->status = FFMX_STATE_EOS;
}
return GF_EOS;
}
return GF_OK;
}
//dovi_meta.h not exported in old releases, just redefine
typedef struct {
u8 dv_version_major;
u8 dv_version_minor;
u8 dv_profile;
u8 dv_level;
u8 rpu_present_flag;
u8 el_present_flag;
u8 bl_present_flag;
u8 dv_bl_signal_compatibility_id;
} Ref_FFAVDoviRecord;
static GF_Err ffmx_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_remove)
{
int res;
AVStream *avst;
Bool check_disc = GF_FALSE;
u32 streamtype, codec_id;
u32 ff_codec_id, ff_st, ff_codec_tag;
GF_Err e;
const GF_PropertyValue *p, *dsi;
GF_FFMuxStream *st;
GF_FFMuxCtx *ctx = (GF_FFMuxCtx *) gf_filter_get_udta(filter);
//disconnect of src pid (not yet supported)
if (is_remove) {
//one in one out, this is simple
return GF_OK;
}
st = gf_filter_pid_get_udta(pid);
if (st) {
if (ctx->status >= FFMX_STATE_HDR_DONE)
check_disc = GF_TRUE;
} else {
if (ctx->status>FFMX_STATE_HDR_DONE) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Cannot dynamically add new stream to %s, not supported\n", ctx->dst ));
return GF_NOT_SUPPORTED;
}
}
p = gf_filter_pid_get_property(pid, GF_PROP_PID_STREAM_TYPE);
if (!p) return GF_NOT_SUPPORTED;
streamtype = p->value.uint;
//not supported yet
if (streamtype==GF_STREAM_ENCRYPTED) {
GF_LOG(GF_LOG_ERROR, GF_LOG_CONTAINER, ("[FFMux] Cannot import protected stream, not supported\n"));
return GF_NOT_SUPPORTED;