-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexample4.c
318 lines (253 loc) · 12.4 KB
/
example4.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
// Copyright (c) 2013, Tuukka Pasanen <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "example1.h"
#include "example2.h"
#include "example3.h"
#include "example4.h"
int64_t m_iSeekOffset = -1;
int64_t m_iLastFirstFfmpegByteOffset = -1;
int64_t m_iCurrentTs = -1;
int64_t m_iOffset = -1;
int m_bIsSeeked = 0;
long fe_read_seek(long filepos) {
int ret = 0;
int64_t fspos = 0;
int64_t l_lSeekPos = 0;
int64_t minus = filepos;
AVRational time_base = m_pFormatCtx->streams[m_iAudioStream]->time_base;
minus = (int64_t)((double)filepos / 2304);
minus *= 2304;
if( minus >= (2304 * 4) ) {
minus -= (2304 * 4);
} else {
minus = 0;
}
fspos = (int64_t) round(fe_convert_fromByteOffset((double)minus, 44100, &time_base));
m_iCurrentTs = filepos;
m_iOffset = 0;
avcodec_flush_buffers(m_pCodecCtx);
ret = avformat_seek_file(m_pFormatCtx,
m_iAudioStream,
0,
fspos,
fspos,
AVSEEK_FLAG_BACKWARD);
avcodec_flush_buffers(m_pCodecCtx);
if (ret < 0) {
printf("fe_read_seek: Seek ERROR ret(%d) filepos(%ld)\n",
ret,
filepos);
return 0;
}
l_lSeekPos = (int64_t) round(fe_convert_toByteOffset(
m_pFormatCtx->streams[m_iAudioStream]->cur_dts, 44100, &time_base));
m_iLastFirstFfmpegByteOffset = l_lSeekPos;
m_iSeekOffset = m_iOffset = (int64_t) round(fe_convert_toByteOffset((double)(fspos - m_pFormatCtx->streams[m_iAudioStream]->cur_dts),
44100, &time_base));
m_iSeekOffset = (int64_t) round((double)l_lSeekPos / 2304);
m_iSeekOffset *= 2304;
printf("fe_read_seek: (RBP: %ld/%.4f) Req Byte: %ld (Sec: %.4f PTS %ld) Real PTS %ld (Sec: %.4f Byte: %ld)\n",
filepos,
(double)((double)filepos / (double)88200),
minus,
(double)((double)minus / (double)88200),
fspos,
m_pFormatCtx->streams[m_iAudioStream]->cur_dts, m_pFormatCtx->streams[m_iAudioStream]->cur_dts * av_q2d(m_pFormatCtx->streams[m_iAudioStream]->time_base),
(int64_t) round(fe_convert_toByteOffset((double)m_pFormatCtx->streams[m_iAudioStream]->cur_dts, 44100, &time_base)));
m_bIsSeeked = 1;
return filepos;
}
unsigned int fe_read_frame(char *buffer, int size) {
AVPacket l_SPacket;
#if LIBAVCODEC_VERSION_INT >= 3616100
AVFrame *l_pFrame = av_frame_alloc();
#else
AVFrame *l_pFrame = avcodec_alloc_frame();
#endif
int l_iCopySize = size * 2;
int m_bReadLoop = 0;
int l_iReadBytes = 0;
double l_fCurrentFFMPEGPosSec = 0;
double l_fCurrentFFMPEGPosByte = 0;
int ret = 0;
int64_t l_iOffset = 0;
int64_t l_iReadedBytes = 0;
int64_t l_iCopiedBytes = 0;
// Just make sure everything is zeroed before use
// Needless but..
memset(buffer, 0x00, size);
l_SPacket.data = NULL;
l_SPacket.size = 0;
av_init_packet(&l_SPacket);
//while (readByteArray.size() < needed)
while (!m_bReadLoop) {
if (av_read_frame(m_pFormatCtx, &l_SPacket) >= 0) {
if (l_SPacket.stream_index == m_iAudioStream) {
// If we have FFMpeg version which is less than 3.2 then we use older implementation
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 0)
ret = avcodec_decode_audio4(m_pCodecCtx, l_pFrame, &l_iFrameFinished, &l_SPacket);
if (ret <= 0) {
#else
// AVERROR(EAGAIN) means that we need to feed more
// That we can decode Frame or Packet
do {
do {
ret = avcodec_send_packet(m_pCodecCtx, &l_SPacket);
} while(ret == AVERROR(EAGAIN));
if(ret == AVERROR_EOF || ret == AVERROR(EINVAL)) {
printf("AVERROR(EAGAIN): %d, AVERROR_EOF: %d, AVERROR(EINVAL): %d\n", AVERROR(EAGAIN), AVERROR_EOF, AVERROR(EINVAL));
printf("fe_read_frame: Frame getting error (%d)!\n", ret);
return 0;
}
ret = avcodec_receive_frame(m_pCodecCtx, l_pFrame);
} while(ret == AVERROR(EAGAIN));
if(ret == AVERROR_EOF || ret == AVERROR(EINVAL)) {
#endif
// An error or EOF occured,index break out and return what
// we have so far.
printf("AVERROR(EAGAIN): %d, AVERROR_EOF: %d, AVERROR(EINVAL): %d\n", AVERROR(EAGAIN), AVERROR_EOF, AVERROR(EINVAL));
printf("fe_read_frame: EOF or some othere decoding error (%d)!\n", ret);
return 0;
}
// If we have FFMpeg version which is less than 3.2 then we use older implementation
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 0)
//frame->
if (l_iFrameFinished) {
#endif
fe_resample_do(l_pFrame);
l_iReadBytes = av_samples_get_buffer_size(NULL, m_pCodecCtx->channels,
l_pFrame->nb_samples,
m_pCodecCtx->sample_fmt, 1);
l_fCurrentFFMPEGPosSec = l_SPacket.pts * av_q2d(m_pFormatCtx->streams[m_iAudioStream]->time_base);
l_fCurrentFFMPEGPosByte = fe_convert_toByteOffset((double)l_SPacket.pts, 44100,
&m_pFormatCtx->streams[m_iAudioStream]->time_base);
if( m_iCurrentTs > l_fCurrentFFMPEGPosByte && l_iOffset == 0) {
l_iOffset = m_iCurrentTs - (int64_t) round(l_fCurrentFFMPEGPosByte);
printf("fe_read_frame: Offset %ld = %ld - %ld\n",
l_iOffset,
m_iCurrentTs,
(int64_t) round(l_fCurrentFFMPEGPosByte));
l_iOffset *= 2;
}
if( l_iOffset >= l_iReadBytes && m_pOut == NULL) {
l_iOffset -= l_iReadBytes;
printf("fe_read_frame: Offset left/readed: %ld/%ld (was: %ld) (PTS: %ld/%ld)\n",
l_iOffset,
(long int)l_iReadBytes,
(l_iOffset + l_iReadBytes),
(long int)l_SPacket.pts,
(int64_t) round(l_fCurrentFFMPEGPosByte));
continue;
} else if( l_iOffset >= m_pOutSize) {
l_iOffset -= m_pOutSize;
printf("fe_read_frame: Resample Offset left/readed: %ld/%ld (was: %ld) (PTS: %ld/%ld)\n",
l_iOffset,
(long int) m_pOutSize,
(long int) (l_iOffset + m_pOutSize),
(long int) l_SPacket.pts,
(long int) round(l_fCurrentFFMPEGPosByte));
continue;
}
printf("fe_read_frame: Cur Sec %.4f (Byte: %ld) Got bytes: (%d @ PTS %ld) (buffered: %ld/%ld)\n",
l_fCurrentFFMPEGPosSec,
(long int) round(l_fCurrentFFMPEGPosByte),
l_iReadBytes,
(long int) l_SPacket.pts,
(long int) l_iReadedBytes,
(long int) l_iCopySize);
l_iReadedBytes += (l_iReadBytes / 2);
if( m_pOut != NULL ) {
if( (m_pOutSize - l_iOffset) < l_iCopySize ) {
printf("fe_read_frame: Resampled Copy Size: %ld Readed: %ld Offset: %ld Copied: %ld\n",
(long int)l_iCopySize,
(long int)m_pOutSize,
(long int)l_iOffset,
(long int)(m_pOutSize - l_iOffset));
memcpy(buffer, m_pOut + l_iOffset, (m_pOutSize - l_iOffset));
buffer += (m_pOutSize - l_iOffset);
l_iCopiedBytes += (m_pOutSize - l_iOffset);
} else {
printf("fe_read_frame: Resampled Last Copy Size: %ld Readed: %ld Offset: %ld\n",
(long int)l_iCopySize,
(long int)m_pOutSize,
(long int)l_iOffset);
memcpy(buffer, m_pOut, l_iCopySize);
l_iCopiedBytes += l_iCopySize;
}
l_iCopySize -= m_pOutSize - l_iOffset;
m_pOutSize = 0;
free(m_pOut);
m_pOut = NULL;
} else {
if( (l_iReadBytes - l_iOffset) < l_iCopySize ) {
printf("fe_read_frame: Copy Size: %ld Readed: %ld Offset: %ld Copied: %ld\n",
(long int)l_iCopySize,
(long int)l_iReadBytes,
(long int)l_iOffset,
(long int)(l_iReadBytes - l_iOffset));
memcpy(buffer, l_pFrame->data[0] + l_iOffset, (l_iReadBytes - l_iOffset));
buffer += (l_iReadBytes - l_iOffset);
l_iCopiedBytes += (l_iReadBytes - l_iOffset);
} else {
printf("fe_read_frame: Last Copy Size: %ld Readed: %ld Offset: %ld\n",
(long int)l_iCopySize,
(long int)l_iReadBytes,
(long int)l_iOffset);
memcpy(buffer, l_pFrame->data[0], l_iCopySize);
l_iCopiedBytes += l_iCopySize;
}
l_iCopySize -= l_iReadBytes - l_iOffset;
}
l_iOffset = 0;
if( l_iCopySize <= 0) {
m_bReadLoop = 1;
}
// If we have FFMpeg version which is less than 3.2 then we use older implementation
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 0)
} else {
printf("fe_read_frame: libavcodec 'avcodec_decode_audio4' didn't succeed or frame not finished (File could also just end!)\n");
}
#endif
}
} else {
printf("fe_read_frame: libavcodec 'av_read_frame' didn't succeed!\n");
//break;
}
// If we have FFMpeg version which is less than 3.2 then we use older implementation
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 48, 0)
av_free_packet(&l_SPacket);
#else
av_packet_unref(&l_SPacket);
#endif
l_SPacket.data = NULL;
l_SPacket.size = 0;
av_init_packet(&l_SPacket);
}
av_free( l_SPacket.data );
l_SPacket.data = NULL;
l_SPacket.size = 0;
#if LIBAVFORMAT_VERSION_INT < 3613028
avcodec_get_frame_defaults(l_pFrame);
#else
av_frame_unref(l_pFrame);
#endif
printf("fe_read_frame: Readed bytes %ld Copied bytes: %ld\n", l_iReadedBytes, l_iCopiedBytes);
return l_iCopiedBytes;
}