forked from livepeer/lpms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextras.c
492 lines (451 loc) · 16.9 KB
/
extras.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
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <stdbool.h>
#include <libavutil/md5.h>
#include "extras.h"
#include "logging.h"
#define MAX_AMISMATCH 10
#define INC_MD5_COUNT 300
#define MAX_MD5_COUNT 30000
#define MD5_SIZE 16 //sizeof(int)*4 byte
struct match_info {
int width;
int height;
uint64_t bit_rate;
int packetcount; //video total packet count
uint64_t timestamp; //XOR sum of avpacket pts
int md5allocsize;
int apacketcount; //audio packet count
int *pmd5array;
};
struct buffer_data {
uint8_t *ptr;
size_t size; ///< size left in the buffer
};
//
// Segmenter
//
int lpms_rtmp2hls(char *listen, char *outf, char *ts_tmpl, char* seg_time, char *seg_start)
{
#define r2h_err(str) {\
if (!ret) ret = 1; \
errstr = str; \
goto handle_r2h_err; \
}
char *errstr = NULL;
int ret = 0;
AVFormatContext *ic = NULL;
AVFormatContext *oc = NULL;
AVOutputFormat *ofmt = NULL;
AVStream *ist = NULL;
AVStream *ost = NULL;
AVDictionary *md = NULL;
AVCodec *codec = NULL;
AVPacket *pkt = NULL;
int64_t prev_ts[2] = {AV_NOPTS_VALUE, AV_NOPTS_VALUE};
int stream_map[2] = {-1, -1};
int got_video_kf = 0;
ret = avformat_open_input(&ic, listen, NULL, NULL);
if (ret < 0) r2h_err("segmenter: Unable to open input\n");
ret = avformat_find_stream_info(ic, NULL);
if (ret < 0) r2h_err("segmenter: Unable to find any input streams\n");
ofmt = av_guess_format(NULL, outf, NULL);
if (!ofmt) r2h_err("Could not deduce output format from file extension\n");
ret = avformat_alloc_output_context2(&oc, ofmt, NULL, outf);
if (ret < 0) r2h_err("Unable to allocate output context\n");
// XXX accommodate cases where audio or video is empty
stream_map[0] = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
if (stream_map[0] < 0) r2h_err("segmenter: Unable to find video stream\n");
stream_map[1] = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
if (stream_map[1] < 0) r2h_err("segmenter: Unable to find audio stream\n");
ist = ic->streams[stream_map[0]];
ost = avformat_new_stream(oc, NULL);
if (!ost) r2h_err("segmenter: Unable to allocate output video stream\n");
avcodec_parameters_copy(ost->codecpar, ist->codecpar);
ist = ic->streams[stream_map[1]];
ost = avformat_new_stream(oc, NULL);
if (!ost) r2h_err("segmenter: Unable to allocate output audio stream\n");
avcodec_parameters_copy(ost->codecpar, ist->codecpar);
av_dict_set(&md, "hls_time", seg_time, 0);
av_dict_set(&md, "hls_segment_filename", ts_tmpl, 0);
av_dict_set(&md, "start_number", seg_start, 0);
av_dict_set(&md, "hls_flags", "delete_segments", 0);
ret = avformat_write_header(oc, &md);
if (ret < 0) r2h_err("Error writing header\n");
pkt = av_packet_alloc();
if (!pkt) r2h_err("Error allocating packet\n");
while (1) {
ret = av_read_frame(ic, pkt);
if (ret == AVERROR_EOF) {
av_interleaved_write_frame(oc, NULL); // flush
break;
} else if (ret < 0) r2h_err("Error reading\n");
// rescale timestamps
if (pkt->stream_index == stream_map[0]) pkt->stream_index = 0;
else if (pkt->stream_index == stream_map[1]) pkt->stream_index = 1;
else goto r2hloop_end;
ist = ic->streams[stream_map[pkt->stream_index]];
ost = oc->streams[pkt->stream_index];
int64_t dts_next = pkt->dts, dts_prev = prev_ts[pkt->stream_index];
if (oc->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
AV_NOPTS_VALUE == dts_prev &&
(pkt->flags & AV_PKT_FLAG_KEY)) got_video_kf = 1;
if (!got_video_kf) goto r2hloop_end; // skip everyting until first video KF
if (AV_NOPTS_VALUE == dts_prev) dts_prev = dts_next;
else if (dts_next <= dts_prev) goto r2hloop_end; // drop late packets
pkt->pts = av_rescale_q_rnd(pkt->pts, ist->time_base, ost->time_base,
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
pkt->dts = av_rescale_q_rnd(pkt->dts, ist->time_base, ost->time_base,
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
if (!pkt->duration) pkt->duration = dts_next - dts_prev;
pkt->duration = av_rescale_q(pkt->duration, ist->time_base, ost->time_base);
prev_ts[pkt->stream_index] = dts_next;
// write the thing
ret = av_interleaved_write_frame(oc, pkt);
if (ret < 0) r2h_err("segmenter: Unable to write output frame\n");
r2hloop_end:
av_packet_unref(pkt);
}
ret = av_write_trailer(oc);
if (ret < 0) r2h_err("segmenter: Unable to write trailer\n");
handle_r2h_err:
if (errstr) fprintf(stderr, "%s", errstr);
if (pkt) av_packet_free(&pkt);
if (ic) avformat_close_input(&ic);
if (oc) avformat_free_context(oc);
if (md) av_dict_free(&md);
return ret == AVERROR_EOF ? 0 : ret;
}
double calculate_duration(AVFormatContext *ic, int astream) {
AVPacket pkt;
av_init_packet(&pkt);
double duration = 0;
int64_t last_pts = AV_NOPTS_VALUE;
while (av_read_frame(ic, &pkt) >= 0) {
if (pkt.stream_index == astream) {
if (pkt.pts != AV_NOPTS_VALUE) {
if (last_pts != AV_NOPTS_VALUE) {
// Calculate the difference between the current and last PTS
int64_t pts_diff = pkt.pts - last_pts;
// Convert the PTS difference to seconds and add to duration
duration += pts_diff * av_q2d(ic->streams[astream]->time_base);
}
last_pts = pkt.pts;
}
av_packet_unref(&pkt);
}
}
return duration;
}
#define GET_CODEC_INTERNAL_ERROR -1
#define GET_CODEC_OK 0
#define GET_CODEC_NEEDS_BYPASS 1
#define GET_CODEC_STREAMS_MISSING 2
//
// Gets codec names for best video and audio streams
// Also detects if bypass is needed for first few segments that are
// audio-only (i.e. have a video stream but no frames)
// If codec name can't be determined string is truncated to 0 size
// returns: 0 if both audio/video streams valid
// 1 for video with 0-frame, that needs bypass
// 2 if audio or video stream is missing
// <0 invalid stream(s) or internal error
//
int lpms_get_codec_info(char *fname, pcodec_info out)
{
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
AVFormatContext *ic = NULL;
AVCodec *ac, *vc;
int64_t last_pts = AV_NOPTS_VALUE;
int ret = GET_CODEC_OK, vstream = 0, astream = 0;
ret = avformat_open_input(&ic, fname, NULL, NULL);
if (ret < 0) { ret = GET_CODEC_INTERNAL_ERROR; goto close_format_context; }
ret = avformat_find_stream_info(ic, NULL);
if (ret < 0) { ret = GET_CODEC_INTERNAL_ERROR; goto close_format_context; }
vstream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, &vc, 0);
astream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, &ac, 0);
bool audio_present = astream >= 0;
bool video_present = vstream >= 0;
if(!audio_present && !video_present) {
// instead of returning -1
ret = GET_CODEC_STREAMS_MISSING;
} else if (video_present) {
out->dur = ic->duration / AV_TIME_BASE;
} else if (audio_present && !video_present) {
out->dur = calculate_duration(ic, astream);
if (out->dur == 0) {
out->dur = ic->duration / AV_TIME_BASE;
}
}
// Return
if (video_present && vc->name) {
strncpy(out->video_codec, vc->name, MIN(strlen(out->video_codec), strlen(vc->name))+1);
// If video track is present extract pixel format info
out->pixel_format = ic->streams[vstream]->codecpar->format;
bool pixel_format_missing = AV_PIX_FMT_NONE == out->pixel_format;
bool no_picture_height = 0 == ic->streams[vstream]->codecpar->height;
if(audio_present && pixel_format_missing && no_picture_height) {
ret = GET_CODEC_NEEDS_BYPASS;
}
out->width = ic->streams[vstream]->codecpar->width;
out->height = ic->streams[vstream]->codecpar->height;
out->fps = av_q2d(ic->streams[vstream]->avg_frame_rate);
} else {
// Indicate failure to extract video codec from given container
out->video_codec[0] = 0;
}
if (audio_present && ac->name) {
strncpy(out->audio_codec, ac->name, MIN(strlen(out->audio_codec), strlen(ac->name))+1);
} else {
// Indicate failure to extract audio codec from given container
out->audio_codec[0] = 0;
}
#undef MIN
close_format_context:
if (ic) avformat_close_input(&ic);
return ret;
}
//// compare two signature files whether those matches or not.
//// @param signpath1 full path of the first signature file.
//// @param signpath2 full path of the second signature file.
//// @return <0: error 0: no matchiing 1: partial matching 2: whole matching.
int lpms_compare_sign_bypath(char *signpath1, char *signpath2)
{
int ret = avfilter_compare_sign_bypath(signpath1, signpath2);
return ret;
}
// compare two signature buffers whether those matches or not.
// @param signbuf1 the pointer of the first signature buffer.
// @param signbuf2 the pointer of the second signature buffer.
// @param len1 the length of the first signature buffer.
// @param len2 the length of the second signature buffer.
// @return <0: error =0: no matchiing 1: partial matching 2: whole matching.
int lpms_compare_sign_bybuffer(void *buffer1, int len1, void *buffer2, int len2)
{
int ret = avfilter_compare_sign_bybuff(buffer1, len1, buffer2, len2);
return ret;
}
static int get_filesize(const char *filename)
{
int fileLength = 0;
FILE *f = NULL;
f = fopen(filename, "rb");
if(f != NULL) {
fseek(f, 0, SEEK_END);
fileLength = ftell(f);
fclose(f);
}
return fileLength;
}
static uint8_t * get_filebuffer(const char *filename, int* fileLength)
{
FILE *f = NULL;
unsigned int readLength, paddedLength = 0;
uint8_t *buffer = NULL;
//check input parameters
if (strlen(filename) <= 0) return buffer;
f = fopen(filename, "rb");
if (f == NULL) {
av_log(NULL, AV_LOG_ERROR, "Could not open the file %s\n", filename);
return buffer;
}
*fileLength = get_filesize(filename);
if(*fileLength > 0) {
// Cast to float is necessary to avoid int division
paddedLength = ceil(*fileLength / (float)AV_INPUT_BUFFER_PADDING_SIZE)*AV_INPUT_BUFFER_PADDING_SIZE + AV_INPUT_BUFFER_PADDING_SIZE;
buffer = (uint8_t*)av_calloc(paddedLength, sizeof(uint8_t));
if (!buffer) {
av_log(NULL, AV_LOG_ERROR, "Could not allocate memory for reading signature file\n");
fclose(f);
return NULL;
}
// Read entire file into memory
readLength = fread(buffer, sizeof(uint8_t), *fileLength, f);
if(readLength != *fileLength) {
av_log(NULL, AV_LOG_ERROR, "Could not read the file %s\n", filename);
free(buffer);
buffer = NULL;
}
}
fclose(f);
return buffer;
}
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
struct buffer_data *bd = (struct buffer_data *)opaque;
buf_size = FFMIN(buf_size, bd->size);
if (!buf_size)
return AVERROR_EOF;
/* copy internal buffer data to buf */
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;
return buf_size;
}
static int get_matchinfo(void *buffer, int len, struct match_info* info)
{
int ret = 0;
AVFormatContext* ifmt_ctx = NULL;
AVIOContext *avio_in = NULL;
AVPacket *packet = NULL;
int audioid = -1;
int md5tmp[4];
uint8_t *avio_ctx_buffer = NULL;
size_t avio_ctx_buffer_size = 4096;
struct buffer_data bd = { 0 };
//initialize matching information
memset(info, 0x00, sizeof(struct match_info));
/* fill opaque structure used by the AVIOContext read callback */
bd.ptr = buffer;
bd.size = len;
avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
if (!avio_ctx_buffer) {
ret = AVERROR(ENOMEM);
LPMS_ERR(clean, "Error allocating buffer");
}
avio_in = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, &read_packet, NULL, NULL);
if (!avio_ctx_buffer) {
ret = AVERROR(ENOMEM);
LPMS_ERR(clean, "Error allocating context");
}
ifmt_ctx = avformat_alloc_context();
if (!ifmt_ctx) {
ret = AVERROR(ENOMEM);
LPMS_ERR(clean, "Error allocating avformat context");
}
ifmt_ctx->pb = avio_in;
ifmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO;
if ((ret = avformat_open_input(&ifmt_ctx, "", NULL, NULL)) < 0) {
LPMS_ERR(clean, "Cannot open input video file\n");
}
if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
LPMS_ERR(clean, "Cannot find stream information\n");
}
for (int i = 0; i < ifmt_ctx->nb_streams; i++) {
AVStream *stream;
stream = ifmt_ctx->streams[i];
AVCodecParameters *in_codecpar = stream->codecpar;
if (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
info->width = in_codecpar->width;
info->height = in_codecpar->height;
info->bit_rate = in_codecpar->bit_rate;
}
else if (in_codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioid = i;
}
}
packet = av_packet_alloc();
if (!packet) LPMS_ERR(clean, "Error allocating packet");
while (1) {
ret = av_read_frame(ifmt_ctx, packet);
if (ret == AVERROR_EOF) {
ret = 0;
break;
}
else if (ret < 0) {
LPMS_ERR(clean, "Unable to read input");
}
info->packetcount++;
info->timestamp ^= packet->pts;
if (packet->stream_index == audioid && packet->size > 0) {
if (info->apacketcount < MAX_MD5_COUNT) {
info->apacketcount++;
if (info->apacketcount > info->md5allocsize) {
info->md5allocsize += INC_MD5_COUNT;
if (info->pmd5array == NULL) {
info->pmd5array = (int*)av_malloc(info->md5allocsize*MD5_SIZE);
} else {
int* tmp = info->pmd5array;
info->pmd5array = (int*)av_malloc(info->md5allocsize*MD5_SIZE);
memcpy(info->pmd5array, tmp, (info->md5allocsize-INC_MD5_COUNT)*MD5_SIZE);
av_free(tmp);
}
}
int* pint = info->pmd5array + (info->apacketcount-1) * 4;
av_md5_sum((uint8_t*)(pint), packet->data, packet->size);
}
}
av_packet_unref(packet);
}
clean:
if(packet)
av_packet_free(&packet);
/* note: the internal buffer could have changed, and be != avio_ctx_buffer */
if(avio_in)
av_freep(&avio_in->buffer);
avio_context_free(&avio_in);
avformat_close_input(&ifmt_ctx);
return ret;
}
// check validity for audio md5 data
bool is_valid_md5data(struct match_info* info1, struct match_info *info2)
{
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
struct match_info* first = info1->apacketcount < info2->apacketcount? info1: info2;
struct match_info* second = info1->apacketcount >= info2->apacketcount? info1: info2;
int packetdiff = second->apacketcount - first->apacketcount;
if (packetdiff > MAX_AMISMATCH) return false;
int matchingcount = 0;
for (int i = 0; i < first->apacketcount; i++) {
int scanscope = packetdiff + 1;
int *psrcmpd = first->pmd5array + (i*4);
int nstart = max(0,i-scanscope);
int nend = min(second->apacketcount,i+scanscope);
for (int j = nstart; j < nend; j++) {
if(memcmp(psrcmpd, second->pmd5array + (j*4), MD5_SIZE) == 0){
matchingcount++;
break;
}
}
}
int realdiff = first->apacketcount - matchingcount;
return realdiff < MAX_AMISMATCH ? true: false;
}
// compare two video buffers whether those matches or not.
// @param buffer1 the pointer of the first video buffer.
// @param buffer2 the pointer of the second video buffer.
// @param len1 the length of the first video buffer.
// @param len2 the length of the second video buffer.
// @return <0: error =0: matching 1: no matching
int lpms_compare_video_bybuffer(void *buffer1, int len1, void *buffer2, int len2)
{
int ret = 0;
struct match_info info1 = {0,}, info2 = {0,};
ret = get_matchinfo(buffer1,len1,&info1);
if(ret < 0) goto clean;
ret = get_matchinfo(buffer2,len2,&info2);
if(ret < 0) goto clean;
//compare two matching information
if (info1.width != info2.width || info1.height != info2.height || !is_valid_md5data(&info1, &info2)) {
ret = 1;
}
clean:
if(info1.pmd5array) av_free(info1.pmd5array);
if(info2.pmd5array) av_free(info2.pmd5array);
return ret;
}
// compare two video files whether those matches or not.
// @param vpath1 full path of the first video file.
// @param vpath2 full path of the second video file.
// @return <0: error =0: matching 1: no matching
int lpms_compare_video_bypath(char *vpath1, char *vpath2)
{
int ret = 0;
int len1, len2;
uint8_t *buffer1, *buffer2;
buffer1 = get_filebuffer(vpath1, &len1);
if(buffer1 == NULL) return AVERROR(ENOMEM);
buffer2 = get_filebuffer(vpath2, &len2);
if(buffer2 == NULL) {
av_freep(&buffer1);
return AVERROR(ENOMEM);
}
ret = lpms_compare_video_bybuffer(buffer1, len1, buffer2, len2);
if(buffer1 != NULL)
av_freep(&buffer1);
if(buffer2 != NULL)
av_freep(&buffer2);
return ret;
}