-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibffmpegaacsucks.c
137 lines (106 loc) · 3.06 KB
/
libffmpegaacsucks.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
#include <libavcodec/codec_id.h>
#include <libavcodec/packet.h>
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ffmpegaacsucks.h"
#include "version.h"
#define FFAACSUCKS_LAVC_SIGNATURE "Lavc"
#define NOT_FOUND_STR "no Lavc/FFmpeg AAC stream was found\n"
const char *ffaacsucks_version(void) { return FFMPEGAACSUCKS_VERSION; }
bool ffaacsucks_check_aac_stream_packet(AVPacket *pkt) {
int pkt_type, skip, namelen;
uint8_t b = pkt->buf->data[0];
pkt_type = (b & 0xe0) >> 5;
if (pkt_type != 6) {
if (getenv("FFMPEGAACSUCKS_DEBUG") != NULL)
fprintf(stderr,
"unexpected packet type found for Lavc/FFMPEG AAC in stream %d "
"(%d)\n",
pkt->stream_index, pkt_type);
return false;
}
namelen = (b & 0x1e) >> 1;
if (namelen == 15)
skip = 3;
else
skip = 2;
char *comment = (char *)pkt->buf->data + skip;
return (strncmp(comment, FFAACSUCKS_LAVC_SIGNATURE, 4)) == 0;
}
static void ffaacsucks_priv_check_avfcontext(AVFormatContext *s,
struct ffaacsucks_result *res) {
unsigned int i, aac_streams = 0;
int ret;
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
aac_streams++;
} else {
st->discard = AVDISCARD_ALL;
}
}
if (aac_streams == 0)
return;
res->streams = malloc(sizeof(int) * aac_streams);
if (res->streams == NULL) {
perror("failed to allocate memory");
res->n_streams = -1;
return;
}
AVPacket *pkt = av_packet_alloc();
while (aac_streams > 0) {
ret = av_read_frame(s, pkt);
if (ret < 0)
perror("av_read_frame");
aac_streams--;
if (ffaacsucks_check_aac_stream_packet(pkt))
res->streams[res->n_streams++] = pkt->stream_index;
s->streams[pkt->stream_index]->discard = AVDISCARD_ALL;
av_packet_unref(pkt);
}
av_packet_free(&pkt);
}
struct ffaacsucks_result *ffaacsucks_check_avfcontext(AVFormatContext *s) {
struct ffaacsucks_result *res = malloc(sizeof(struct ffaacsucks_result));
if (res == NULL) {
perror("failed to allocate memory");
return NULL;
}
res->n_streams = 0;
res->streams = NULL;
ffaacsucks_priv_check_avfcontext(s, res);
if(res->n_streams < 0) {
ffaacsucks_result_free(res);
return NULL;
}
return res;
}
struct ffaacsucks_result *ffaacsucks_check(char *filepath) {
AVFormatContext *s = NULL;
int ret;
struct ffaacsucks_result *res = malloc(sizeof(struct ffaacsucks_result));
if (res == NULL) {
perror("failed to allocate memory");
return NULL;
}
res->n_streams = 0;
res->streams = NULL;
ret = avformat_open_input(&s, filepath, NULL, NULL);
if (ret < 0) {
perror("failed to load file");
res->n_streams = -1;
return res;
}
ffaacsucks_priv_check_avfcontext(s, res);
avformat_close_input(&s);
avformat_free_context(s);
if (res->n_streams < 0) {
ffaacsucks_result_free(res);
return NULL;
}
return res;
}