-
Notifications
You must be signed in to change notification settings - Fork 5
/
ffs.c
147 lines (135 loc) · 2.94 KB
/
ffs.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
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include "ffs.h"
/* ffmpeg stream */
struct ffs {
AVCodecContext *cc;
AVFormatContext *fc;
AVPacket pkt;
int si; /* stream index */
long ts; /* frame timestamp (ms) */
long seq; /* current position in this stream */
long seq_all; /* seen packets after ffs_seek() */
long seq_cur; /* decoded packet after ffs_seek() */
/* decoding video frames */
struct SwsContext *swsc;
AVFrame *dst;
AVFrame *tmp;
};
struct ffs *ffs_alloc(char *path, int video)
{
struct ffs *ffs;
int codec_type = CODEC_TYPE_AUDIO;
int i;
ffs = malloc(sizeof(*ffs));
memset(ffs, 0, sizeof(*ffs));
ffs->si = -1;
if (av_open_input_file(&ffs->fc, path, NULL, 0, NULL))
goto failed;
if (av_find_stream_info(ffs->fc) < 0)
goto failed;
for (i = 0; i < ffs->fc->nb_streams; i++)
if (ffs->fc->streams[i]->codec->codec_type == codec_type)
ffs->si = i;
if (ffs->si == -1)
goto failed;
ffs->cc = ffs->fc->streams[ffs->si]->codec;
avcodec_open(ffs->cc, avcodec_find_decoder(ffs->cc->codec_id));
return ffs;
failed:
ffs_free(ffs);
return NULL;
}
void ffs_free(struct ffs *ffs)
{
if (ffs->swsc)
sws_freeContext(ffs->swsc);
if (ffs->dst)
av_free(ffs->dst);
if (ffs->tmp)
av_free(ffs->tmp);
if (ffs->cc)
avcodec_close(ffs->cc);
if (ffs->fc)
av_close_input_file(ffs->fc);
free(ffs);
}
static AVPacket *ffs_pkt(struct ffs *ffs)
{
AVPacket *pkt = &ffs->pkt;
while (av_read_frame(ffs->fc, pkt) >= 0) {
ffs->seq_all++;
if (pkt->stream_index == ffs->si) {
ffs->seq_cur++;
ffs->seq++;
return pkt;
}
av_free_packet(pkt);
}
return NULL;
}
static long ts_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
static void wait(long ts, int vdelay)
{
int nts = ts_ms();
if (ts + vdelay > nts)
usleep((ts + vdelay - nts) * 1000);
}
void ffs_wait(struct ffs *ffs)
{
long ts = ts_ms();
if (ts && ffs->ts) {
AVRational *r = &ffs->fc->streams[ffs->si]->time_base;
int vdelay = 1000 * r->num / r->den;
wait(ffs->ts, vdelay < 20 ? 20 : vdelay);
}
ffs->ts = ts_ms();
}
long ffs_pos(struct ffs *ffs, int diff)
{
return (ffs->si << 28) | (ffs->seq + diff);
}
void ffs_ainfo(struct ffs *ffs, int *rate, int *bps, int *ch)
{
*rate = ffs->cc->sample_rate;
*ch = ffs->cc->channels;
*bps = 16;
}
int ffs_adec(struct ffs *ffs, void *buf, int blen)
{
int rdec = 0;
AVPacket tmppkt;
AVPacket *pkt = ffs_pkt(ffs);
if (!pkt)
return -1;
tmppkt.size = pkt->size;
tmppkt.data = pkt->data;
while (tmppkt.size > 0) {
int size = blen - rdec;
int len = avcodec_decode_audio3(ffs->cc, (int16_t *) (buf + rdec),
&size, &tmppkt);
if (len < 0)
break;
tmppkt.size -= len;
tmppkt.data += len;
if (size > 0)
rdec += size;
}
av_free_packet(pkt);
return rdec;
}
void ffs_globinit(void)
{
av_register_all();
}