-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0001-Add-support-for-server-generated-waveform-data-throu.patch
More file actions
634 lines (613 loc) · 24.7 KB
/
0001-Add-support-for-server-generated-waveform-data-throu.patch
File metadata and controls
634 lines (613 loc) · 24.7 KB
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
From 6f1ab432db539e963918761a8b96043996d6e034 Mon Sep 17 00:00:00 2001
From: "F. Moukayed" <7354509+fmqa@users.noreply.github.com>
Date: Sat, 29 May 2021 12:52:03 +0000
Subject: [PATCH] Add support for server-generated waveform data through a
custom EXT-X-PEAKDATA extension
---
libavformat/dashenc.c | 2 +-
libavformat/hlsenc.c | 381 +++++++++++++++++++++++++++++++++++++-
libavformat/hlsplaylist.c | 49 ++++-
libavformat/hlsplaylist.h | 3 +-
4 files changed, 429 insertions(+), 6 deletions(-)
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 81a5c2b452..ba7b74f5b0 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -575,7 +575,7 @@ static void write_hls_media_playlist(OutputStream *os, AVFormatContext *s,
(double) seg->duration / timescale, 0,
seg->range_length, seg->start_pos, NULL,
c->single_file ? os->initfile : seg->file,
- &prog_date_time, 0, 0, 0);
+ &prog_date_time, 0, 0, 0, NULL, NULL, 0.0);
if (ret < 0) {
av_log(os->ctx, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
}
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 7d97ce1789..8a636d2b37 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -44,6 +44,8 @@
#include "libavutil/time.h"
#include "libavutil/time_internal.h"
+#include "libswresample/swresample.h"
+
#include "avformat.h"
#include "avio_internal.h"
#include "avc.h"
@@ -73,6 +75,16 @@ typedef enum {
#define BUFSIZE (16 * 1024)
#define POSTFIX_PATTERN "_%d"
+typedef struct HLSAudioPeakIO {
+ AVStream *st;
+ AVCodecContext *ctx;
+ AVFrame *frame;
+ SwrContext *swr;
+ char *filename;
+ int fd;
+ int16_t *window;
+} HLSAudioPeakIO;
+
typedef struct HLSSegment {
char filename[MAX_URL_SIZE];
char sub_filename[MAX_URL_SIZE];
@@ -89,6 +101,9 @@ typedef struct HLSSegment {
struct HLSSegment *next;
double discont_program_date_time;
+
+ char *peakfilename;
+ int peakfd;
} HLSSegment;
typedef enum HLSFlags {
@@ -258,6 +273,12 @@ typedef struct HLSContext {
char *headers;
int has_default_key; /* has DEFAULT field of var_stream_map */
int has_video_m3u8; /* has video stream m3u8 list */
+
+ int audio_peaks;
+
+ HLSAudioPeakIO *apio;
+ int apio_winsize;
+ int apio_sr;
} HLSContext;
static int strftime_expand(const char *fmt, char **dest)
@@ -651,6 +672,21 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
}
while (segment) {
+ if (segment->peakfd >= 0) {
+ if (close(segment->peakfd) == 0) {
+ segment->peakfd = -1;
+ } else {
+ av_log(hls, AV_LOG_WARNING, "error closing segment peak buffer fd: %d\n", segment->peakfd);
+ }
+ }
+ if (segment->peakfilename) {
+ if (unlink(segment->peakfilename) == 0) {
+ segment->peakfilename = NULL;
+ } else {
+ av_log(hls, AV_LOG_WARNING, "error closing segment peak file: %s\n", segment->peakfilename);
+ }
+ }
+
av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",
segment->filename);
if (!hls->use_localtime_mkdir) // segment->filename contains basename only
@@ -683,6 +719,7 @@ static int hls_delete_old_segments(AVFormatContext *s, HLSContext *hls,
if (ret = hls_delete_file(hls, vs->vtt_avf, path.str, proto))
goto fail;
}
+
av_bprint_clear(&path);
previous_segment = segment;
segment = previous_segment->next;
@@ -1105,11 +1142,92 @@ static int sls_flag_use_localtime_filename(AVFormatContext *oc, HLSContext *c, V
return 0;
}
+static HLSAudioPeakIO *find_apio(int n, HLSAudioPeakIO *apio, VariantStream *vs)
+{
+ for (int i = 0; i < n; ++i) {
+ for (int j = 0; j < vs->nb_streams; ++j) {
+ if (apio[i].st == vs->streams[j]) {
+ return &apio[i];
+ }
+ }
+ }
+ return NULL;
+}
+
+
+static inline double ssqri16(size_t n, const int16_t *xs)
+{
+ double sigma = 0.0;
+ for (size_t i = 0; i < n; ++i) {
+ sigma += pow(xs[i] / 32767.0, 2);
+ }
+ return sigma;
+}
+
+static void hlsenc_apio_write_segment_peaks(struct AVFormatContext *s, HLSContext *hls, HLSAudioPeakIO *apio, HLSSegment *en)
+{
+ double rmsbuf[BUFSIZ];
+ size_t rmsidx = 0;
+ ssize_t ret = 0;
+ en->peakfd = avpriv_tempfile("ffhlspeak-seg-", &en->peakfilename, 0, s);
+ if (en->peakfd < 0) {
+ av_log(hls, AV_LOG_WARNING, "Could not open segment peak file: %s\n", en->peakfilename ? en->peakfilename : "(unnamed)");
+ return;
+ }
+ if (lseek(apio->fd, 0, SEEK_SET) < 0) {
+ av_log(hls, AV_LOG_WARNING, "Could not rewind peak buffer file: %s\n", apio->filename ? apio->filename : "(unnamed)");
+ if (close(en->peakfd) == 0) {
+ en->peakfd = -1;
+ } else {
+ av_log(hls, AV_LOG_WARNING, "Could not close segment peak file: %s\n", en->peakfilename ? en->peakfilename : "(unnamed)");
+ }
+ if (unlink(en->peakfilename) == 0) {
+ en->peakfilename = NULL;
+ } else {
+ av_log(hls, AV_LOG_WARNING, "error closing segment peak file: %s\n", en->peakfilename);
+ }
+ return;
+ }
+ while ((ret = read(apio->fd, apio->window, sizeof(int16_t) * hls->apio_winsize)) > 0) {
+ size_t n = ret / sizeof(int16_t);
+ /* Compute window dBFS */
+ rmsbuf[rmsidx++] = fmax(-96.329599, 20 * log10(sqrt(ssqri16(n, apio->window) / n))) + 3.0103;
+ if (rmsidx >= BUFSIZ) {
+ ret = write(en->peakfd, rmsbuf, rmsidx * sizeof(double));
+ if (ret != rmsidx * sizeof(double)) {
+ av_log(hls, AV_LOG_WARNING, "Could not write %ld peaks to file: %s", rmsidx, en->peakfilename ? en->peakfilename : "(unnamed)");
+ }
+ rmsidx = 0;
+ }
+ }
+ if (rmsidx > 0) {
+ ret = write(en->peakfd, rmsbuf, rmsidx * sizeof(double));
+ if (ret != rmsidx * sizeof(double)) {
+ av_log(hls, AV_LOG_WARNING, "Could not write %ld peaks to file: %s", rmsidx, en->peakfilename ? en->peakfilename : "(unnamed)");
+ }
+ }
+ if (ret < 0) {
+ av_log(hls, AV_LOG_WARNING, "Could not read peak buffer file: %s\n", apio->filename ? apio->filename : "(unnamed)");
+ }
+ if (lseek(apio->fd, 0, SEEK_SET) < 0) {
+ av_log(hls, AV_LOG_WARNING, "Could not rewind peak buffer file: %s\n", apio->filename ? apio->filename : "(unnamed)");
+ }
+ if (truncate(apio->filename, 0) < 0) {
+ av_log(hls, AV_LOG_WARNING, "Could not truncate peak buffer file: %s\n", apio->filename ? apio->filename : "(unnamed)");
+ }
+ if (close(en->peakfd) == 0) {
+ en->peakfd = -1;
+ } else {
+ av_log(hls, AV_LOG_WARNING, "Could not close segment peak file: %s\n", en->peakfilename ? en->peakfilename : "(unnamed)");
+ }
+}
+
/* Create a new segment and append it to the segment list */
static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
VariantStream *vs, double duration, int64_t pos,
int64_t size)
{
+ HLSAudioPeakIO *apio = hls->apio ? find_apio(s->nb_streams, hls->apio, vs) : NULL;
HLSSegment *en = av_malloc(sizeof(*en));
const char *filename;
int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0);
@@ -1149,6 +1267,12 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
en->next = NULL;
en->discont = 0;
en->discont_program_date_time = 0;
+ en->peakfilename = NULL;
+ en->peakfd = -1;
+
+ if (apio) {
+ hlsenc_apio_write_segment_peaks(s, hls, apio, en);
+ }
if (vs->discontinuity) {
en->discont = 1;
@@ -1186,8 +1310,23 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls,
vs->old_segments = en;
if ((ret = hls_delete_old_segments(s, hls, vs)) < 0)
return ret;
- } else
+ } else {
+ if (en->peakfd >= 0) {
+ if (close(en->peakfd) == 0) {
+ en->peakfd = -1;
+ } else {
+ av_log(NULL, AV_LOG_WARNING, "error closing segment peak buffer fd: %d\n", en->peakfd);
+ }
+ }
+ if (en->peakfilename) {
+ if (unlink(en->peakfilename) == 0) {
+ en->peakfilename = NULL;
+ } else {
+ av_log(NULL, AV_LOG_WARNING, "error closing segment peak file: %s\n", en->peakfilename);
+ }
+ }
av_freep(&en);
+ }
} else
vs->nb_entries++;
@@ -1316,6 +1455,20 @@ static void hls_free_segments(HLSSegment *p)
while (p) {
en = p;
+ if (en->peakfd >= 0) {
+ if (close(en->peakfd) == 0) {
+ en->peakfd = -1;
+ } else {
+ av_log(NULL, AV_LOG_WARNING, "error closing segment peak buffer fd: %d\n", en->peakfd);
+ }
+ }
+ if (en->peakfilename) {
+ if (unlink(en->peakfilename) == 0) {
+ en->peakfilename = NULL;
+ } else {
+ av_log(NULL, AV_LOG_WARNING, "error closing segment peak file: %s\n", en->peakfilename);
+ }
+ }
p = p->next;
av_freep(&en);
}
@@ -1624,7 +1777,9 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
en->size, en->pos, hls->baseurl,
en->filename,
en->discont_program_date_time ? &en->discont_program_date_time : prog_date_time_p,
- en->keyframe_size, en->keyframe_pos, hls->flags & HLS_I_FRAMES_ONLY);
+ en->keyframe_size, en->keyframe_pos, hls->flags & HLS_I_FRAMES_ONLY,
+ &en->peakfd, en->peakfilename,
+ hls->audio_peaks ? hls->apio_winsize / (double)hls->apio_sr : 0.0);
if (en->discont_program_date_time)
en->discont_program_date_time -= en->duration;
if (ret < 0) {
@@ -1647,7 +1802,9 @@ static int hls_window(AVFormatContext *s, int last, VariantStream *vs)
for (en = vs->segments; en; en = en->next) {
ret = ff_hls_write_file_entry(hls->sub_m3u8_out, 0, byterange_mode,
en->duration, 0, en->size, en->pos,
- hls->baseurl, en->sub_filename, NULL, 0, 0, 0);
+ hls->baseurl, en->sub_filename, NULL, 0, 0, 0,
+ &en->peakfd, en->peakfilename,
+ hls->audio_peaks ? hls->apio_winsize / (double)hls->apio_sr : 0.0);
if (ret < 0) {
av_log(s, AV_LOG_WARNING, "ff_hls_write_file_entry get error\n");
}
@@ -2415,6 +2572,60 @@ static int64_t append_single_file(AVFormatContext *s, VariantStream *vs)
return ret;
}
+
+static int hlsenc_apio_write_samples(HLSAudioPeakIO *apio, int dst_sr, AVPacket *pkt)
+{
+ int ret = 0;
+ int16_t *buffer = NULL;
+ int max_out_samples = 0;
+ if (!apio->frame) {
+ apio->frame = av_frame_alloc();
+ if (!apio->frame) {
+ ret = AVERROR(ENOMEM);
+ goto cleanup;
+ }
+ }
+ ret = avcodec_send_packet(apio->ctx, pkt);
+ if (ret < 0) {
+ goto cleanup;
+ }
+ while (ret >= 0) {
+ int out_samples;
+ ret = avcodec_receive_frame(apio->ctx, apio->frame);
+ if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
+ ret = 0;
+ goto cleanup;
+ } else if (ret < 0) {
+ goto cleanup;
+ }
+ out_samples = av_rescale_rnd(apio->frame->nb_samples, dst_sr, apio->st->codecpar->sample_rate, AV_ROUND_UP);
+ if (out_samples > max_out_samples) {
+ max_out_samples = out_samples;
+ av_freep(&buffer);
+ ret = av_samples_alloc((uint8_t**)&buffer, NULL, 1, max_out_samples, AV_SAMPLE_FMT_S16P, 0);
+ if (ret < 0) {
+ goto cleanup;
+ }
+ }
+ ret = swr_convert(
+ apio->swr,
+ (uint8_t**)&buffer,
+ out_samples,
+ (const uint8_t**)apio->frame->data,
+ apio->frame->nb_samples);
+ if (ret < 0) {
+ goto cleanup;
+ }
+ if (write(apio->fd, buffer, sizeof(int16_t) * ret) < 0) {
+ ret = AVERROR(errno);
+ goto cleanup;
+ }
+ }
+cleanup:
+ av_freep(&buffer);
+ return ret;
+}
+
static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
{
HLSContext *hls = s->priv_data;
@@ -2673,6 +2884,12 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
vs->packets_written++;
if (oc->pb) {
int64_t keyframe_pre_pos = avio_tell(oc->pb);
+ if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && hls->apio && hls->apio[pkt->stream_index].st == st) {
+ ret = hlsenc_apio_write_samples(&hls->apio[pkt->stream_index], hls->apio_sr, pkt);
+ if (ret < 0) {
+ return ret;
+ }
+ }
ret = ff_write_chained(oc, stream_index, pkt, s, 0);
if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
(pkt->flags & AV_PKT_FLAG_KEY) && !keyframe_pre_pos) {
@@ -2712,6 +2929,35 @@ static void hls_deinit(AVFormatContext *s)
av_freep(&vs->streams);
}
+ if (hls->apio) {
+ for (int i = 0; i < s->nb_streams; ++i) {
+ if (hls->apio[i].frame) {
+ av_frame_free(&hls->apio[i].frame);
+ }
+ if (hls->apio[i].ctx) {
+ avcodec_free_context(&hls->apio[i].ctx);
+ }
+ if (hls->apio[i].swr) {
+ swr_free(&hls->apio[i].swr);
+ hls->apio[i].swr = NULL;
+ }
+ if (hls->apio[i].window) {
+ av_freep(&hls->apio[i].window);
+ }
+ if (hls->apio[i].fd >= 0) {
+ if (close(hls->apio[i].fd) == 0) {
+ hls->apio[i].fd = -1;
+ }
+ }
+ if (hls->apio[i].filename) {
+ if (unlink(hls->apio[i].filename) == 0) {
+ hls->apio[i].filename = NULL;
+ }
+ }
+ }
+ av_freep(&hls->apio);
+ }
+
ff_format_io_close(s, &hls->m3u8_out);
ff_format_io_close(s, &hls->sub_m3u8_out);
av_freep(&hls->key_basename);
@@ -2858,6 +3104,51 @@ failed:
return 0;
}
+static int supports_sample_format(const AVCodec *codec, enum AVSampleFormat fmt)
+{
+ for (const enum AVSampleFormat *p = codec->sample_fmts; *p != -1; ++p) {
+ if (*p == fmt) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int supports_sample_rate(const AVCodec *codec, int rate)
+{
+ for (const int *p = codec->supported_samplerates; *p != 0; ++p) {
+ if (*p == rate) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int supports_channel_layout(const AVCodec *codec, uint64_t layout)
+{
+ for (const uint64_t *p = codec->channel_layouts; *p != 0; ++p) {
+ if (*p == layout) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static const AVCodec *find_best_decoder(const AVCodecParameters *params)
+{
+ void *cursor = NULL;
+ const AVCodec *codec = NULL;
+ while (codec = av_codec_iterate(&cursor)) {
+ if (!av_codec_is_decoder(codec)) continue;
+ if (codec->type != params->codec_type) continue;
+ if (codec->id != params->codec_id) continue;
+ if (codec->supported_samplerates && !supports_sample_rate(codec, params->sample_rate)) continue;
+ if (codec->sample_fmts && !supports_sample_format(codec, params->format)) continue;
+ if (codec->channel_layouts && !supports_channel_layout(codec, params->channel_layout)) continue;
+ break;
+ }
+ return codec;
+}
static int hls_init(AVFormatContext *s)
{
@@ -2925,6 +3216,87 @@ static int hls_init(AVFormatContext *s)
}
}
+ if (hls->audio_peaks) {
+ hls->apio = av_mallocz(sizeof(*hls->apio) * s->nb_streams);
+ if (!hls->apio) {
+ return AVERROR(ENOMEM);
+ }
+ for (int i = 0; i < s->nb_streams; ++i) {
+ if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+ const AVCodec *codec = find_best_decoder(s->streams[i]->codecpar);
+ if (!codec) {
+ av_log(s, AV_LOG_ERROR, "Could not find input codec\n");
+ return -1;
+ }
+ hls->apio[i].ctx = avcodec_alloc_context3(codec);
+ if (!hls->apio[i].ctx) {
+ return AVERROR(ENOMEM);
+ }
+ ret = avcodec_parameters_to_context(hls->apio[i].ctx, s->streams[i]->codecpar);
+ if (ret < 0) {
+ avcodec_free_context(&hls->apio[i].ctx);
+ return ret;
+ }
+ ret = avcodec_open2(hls->apio[i].ctx, codec, NULL);
+ if (ret < 0) {
+ avcodec_free_context(&hls->apio[i].ctx);
+ return ret;
+ }
+ hls->apio[i].swr = swr_alloc();
+ if (!hls->apio[i].swr) {
+ avcodec_free_context(&hls->apio[i].ctx);
+ return AVERROR(ENOMEM);
+ }
+ if ((ret = av_opt_set_int(hls->apio[i].swr, "in_channel_count", s->streams[i]->codecpar->channels, 0)) ||
+ (ret = av_opt_set_int(hls->apio[i].swr, "out_channel_count", 1, 0)) ||
+ (ret = av_opt_set_int(hls->apio[i].swr, "in_channel_layout", s->streams[i]->codecpar->channel_layout, 0)) ||
+ (ret = av_opt_set_int(hls->apio[i].swr, "out_channel_layout", AV_CH_LAYOUT_MONO, 0)) ||
+ (ret = av_opt_set_int(hls->apio[i].swr, "in_sample_rate", s->streams[i]->codecpar->sample_rate, 0)) ||
+ (ret = av_opt_set_int(hls->apio[i].swr, "out_sample_rate", hls->apio_sr, 0)) ||
+ (ret = av_opt_set_sample_fmt(hls->apio[i].swr, "in_sample_fmt", s->streams[i]->codecpar->format, 0)) ||
+ (ret = av_opt_set_sample_fmt(hls->apio[i].swr, "out_sample_fmt", AV_SAMPLE_FMT_S16P, 0)))
+ {
+ swr_free(&hls->apio[i].swr);
+ hls->apio[i].swr = NULL;
+ avcodec_free_context(&hls->apio[i].ctx);
+ return ret;
+ }
+ ret = swr_init(hls->apio[i].swr);
+ if (ret < 0) {
+ swr_free(&hls->apio[i].swr);
+ hls->apio[i].swr = NULL;
+ avcodec_free_context(&hls->apio[i].ctx);
+ return ret;
+ }
+ hls->apio[i].fd = avpriv_tempfile("ffhlspeak-", &hls->apio[i].filename, 0, s);
+ if (hls->apio[i].fd < 0) {
+ ret = errno;
+ av_log(s, AV_LOG_ERROR, "Could not create temporary file for peaks: %s\n", strerror(ret));
+ swr_free(&hls->apio[i].swr);
+ hls->apio[i].swr = NULL;
+ avcodec_free_context(&hls->apio[i].ctx);
+ return AVERROR(ret);
+ }
+ hls->apio[i].window = av_malloc(hls->apio_winsize * sizeof(int16_t));
+ if (!hls->apio[i].window) {
+ if (close(hls->apio[i].fd) == 0) {
+ hls->apio[i].fd = -1;
+ }
+ if (unlink(hls->apio[i].filename) == 0) {
+ hls->apio[i].filename = NULL;
+ }
+ swr_free(&hls->apio[i].swr);
+ hls->apio[i].swr = NULL;
+ avcodec_free_context(&hls->apio[i].ctx);
+ return AVERROR(ENOMEM);
+ }
+ hls->apio[i].st = s->streams[i];
+ }
+ }
+ } else {
+ hls->apio = NULL;
+ }
+
if ((hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_SECONDS_SINCE_EPOCH) ||
(hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_MICROSECONDS_SINCE_EPOCH) ||
(hls->start_sequence_source_type == HLS_START_SEQUENCE_AS_FORMATTED_DATETIME)) {
@@ -3180,6 +3552,9 @@ static const AVOption options[] = {
{"timeout", "set timeout for socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT_MAX, .flags = E },
{"ignore_io_errors", "Ignore IO errors for stable long-duration runs with network output", OFFSET(ignore_io_errors), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
{"headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
+ {"hls_audio_peaks", "Add per-segment EXT-X-LOUDNESS time series data to playlist", OFFSET(audio_peaks), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
+ {"hls_audio_peaks_window", "EXT-X-LOUDNESS window size", OFFSET(apio_winsize), AV_OPT_TYPE_INT, {.i64 = 1000}, 1, INT_MAX, E},
+ {"hls_audio_peaks_rate", "Sampling rate for loudness calculation", OFFSET(apio_sr), AV_OPT_TYPE_INT, {.i64 = 8000}, 1, INT_MAX, E},
{ NULL },
};
diff --git a/libavformat/hlsplaylist.c b/libavformat/hlsplaylist.c
index 0e1dcc087f..d49cf3be9d 100644
--- a/libavformat/hlsplaylist.c
+++ b/libavformat/hlsplaylist.c
@@ -28,6 +28,17 @@
#include "avformat.h"
#include "hlsplaylist.h"
+#include <fcntl.h>
+#if HAVE_IO_H
+#include <io.h>
+#endif
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <sys/stat.h>
+#include <stdlib.h>
+#include "os_support.h"
+
void ff_hls_write_playlist_version(AVIOContext *out, int version)
{
if (!out)
@@ -136,7 +147,8 @@ int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
const char *baseurl /* Ignored if NULL */,
const char *filename, double *prog_date_time,
int64_t video_keyframe_size, int64_t video_keyframe_pos,
- int iframe_mode)
+ int iframe_mode, int *peakfd, const char *peakfilename,
+ double spp)
{
if (!out || !filename)
return AVERROR(EINVAL);
@@ -179,6 +191,41 @@ int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
avio_printf(out, "#EXT-X-PROGRAM-DATE-TIME:%s.%03d%s\n", buf0, milli, buf1);
*prog_date_time += duration;
}
+
+ if (peakfilename && peakfd) {
+ ssize_t ret;
+ double buffer[BUFSIZ];
+ int header = 0;
+
+ if (*peakfd < 0) {
+ *peakfd = avpriv_open(peakfilename, O_RDONLY);
+ if (*peakfd < 0) {
+ av_log(NULL, AV_LOG_WARNING, "open() error in ff_hls_write_file_entry\n");
+ goto post;
+ }
+ }
+
+ while ((ret = read(*peakfd, buffer, sizeof(buffer))) > 0) {
+ if (!header) {
+ avio_printf(out, "#EXT-X-LOUDNESS:UNIT=dBFS,SPP=%.4lf,PEAKS=", spp);
+ header = 1;
+ }
+ for (size_t i = 0, n = ret / sizeof(double); i < n; ++i) {
+ avio_printf(out, "%.1lf%c", buffer[i], i + 1 < n ? ',' : '\n');
+ }
+ }
+
+ if (close(*peakfd) == 0) {
+ *peakfd = -1;
+ } else {
+ av_log(NULL, AV_LOG_WARNING, "close error in ff_hls_write_file_entry\n");
+ }
+
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_WARNING, "read error in ff_hls_write_file_entry\n");
+ }
+ }
+post:
if (baseurl)
avio_printf(out, "%s", baseurl);
avio_printf(out, "%s\n", filename);
diff --git a/libavformat/hlsplaylist.h b/libavformat/hlsplaylist.h
index 29487da3ed..ee4e50cab2 100644
--- a/libavformat/hlsplaylist.h
+++ b/libavformat/hlsplaylist.h
@@ -59,7 +59,8 @@ int ff_hls_write_file_entry(AVIOContext *out, int insert_discont,
const char *baseurl /* Ignored if NULL */,
const char *filename, double *prog_date_time,
int64_t video_keyframe_size, int64_t video_keyframe_pos,
- int iframe_mode);
+ int iframe_mode, int *peakfd, const char *peakfilename,
+ double spp);
void ff_hls_write_end_list (AVIOContext *out);
#endif /* AVFORMAT_HLSPLAYLIST_H_ */
--
2.25.1