aboutsummaryrefslogtreecommitdiffstats
path: root/src/inputPlugins/ffmpeg_plugin.c
blob: 7d3c243605f8d0cc54872d7fb10ba03181e7522a (plain) (blame)
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
/* the Music Player Daemon (MPD)
 * Copyright (C) 2008 Viliam Mateicka <viliam.mateicka@gmail.com>
 * This project's homepage is: http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "../decoder_api.h"
#include "../log.h"
#include "../utils.h"
#include "../log.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <avcodec.h>
#include <avformat.h>
#include <avio.h>

typedef struct {
	int audioStream;
	AVFormatContext *pFormatCtx;
	AVCodecContext *aCodecCtx;
	AVCodec *aCodec;
	struct decoder *decoder;
	InputStream *input;
	struct tag *tag;
} BasePtrs;

typedef struct {
	struct decoder *decoder;
	InputStream *input;
} FopsHelper;

static int mpdurl_open(URLContext *h, const char *filename, int flags)
{
	uint32_t ptr;
	FopsHelper *base;
	if (strlen(filename) == (8+8)) {
		errno = 0;
		ptr = (uint32_t) strtoll(filename + 6, NULL, 16);
		if (errno == 0 && ptr != 0) {
			base = (FopsHelper *) ptr;
			h->priv_data = base;
			h->is_streamed = (base->input->seekable ? 0 : 1);
			return 0;
		}
	}
	FATAL("Invalid format %s:%d\n", filename, flags);
	return -1;
}

static int mpdurl_read(URLContext *h, unsigned char *buf, int size)
{
	int ret;
	FopsHelper *base = (FopsHelper *) h->priv_data;
	while (1) {
		ret = readFromInputStream(base->input, (void *)buf, size);
		if (ret == 0) {
			DEBUG("ret 0\n");
			if (inputStreamAtEOF(base->input) ||
			    (base->decoder &&
			     decoder_get_command(base->decoder) != DECODE_COMMAND_NONE)) {
				DEBUG("eof stream\n");
				return ret;
			} else {
				my_usleep(10000);
			}
		} else {
			break;
		}
	}
	return ret;
}

static int64_t mpdurl_seek(URLContext *h, int64_t pos, int whence)
{
	FopsHelper *base = (FopsHelper *) h->priv_data;
	if (whence != AVSEEK_SIZE) { //only ftell
		(void) seekInputStream(base->input, pos, whence);
	}
	return base->input->offset;
}

static int mpdurl_close(URLContext *h)
{
	FopsHelper *base = (FopsHelper *) h->priv_data;
	if (base && base->input->seekable) {
		(void) seekInputStream(base->input, 0, SEEK_SET);
	}
	h->priv_data = 0;
	return 0;
}

static URLProtocol mpdurl_fileops = {
	.name = "mpd",
	.url_open = mpdurl_open,
	.url_read = mpdurl_read,
	.url_seek = mpdurl_seek,
	.url_close = mpdurl_close,
};

static int ffmpeg_init(void)
{
	av_register_all();
	register_protocol(&mpdurl_fileops);
	return 0;
}

static int ffmpeg_helper(InputStream *input, int (*callback)(BasePtrs *ptrs),
			 BasePtrs *ptrs)
{
	AVFormatContext *pFormatCtx;
	AVCodecContext *aCodecCtx;
	AVCodec *aCodec;
	int ret, audioStream;
	unsigned i;
	FopsHelper fopshelp;
	char url[24];

	fopshelp.input = input;
	if (ptrs && ptrs->decoder) {
		fopshelp.decoder = ptrs->decoder; //are we in decoding loop ?
	} else {
		fopshelp.decoder = NULL;
	}

	//ffmpeg works with ours "fileops" helper
	sprintf(url, "mpd://0x%8x", (unsigned int) &fopshelp);

	if (av_open_input_file(&pFormatCtx, url, NULL, 0, NULL)!=0) {
		ERROR("Open failed!\n");
		return -1;
	}

	if (av_find_stream_info(pFormatCtx)<0) {
		ERROR("Couldn't find stream info!\n");
		return -1;
	}

	audioStream = -1;
	for(i=0; i<pFormatCtx->nb_streams; i++) {
		if (pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO &&
		    audioStream < 0) {
			audioStream=i;
		}
	}

	if(audioStream==-1) {
		ERROR("No audio stream inside!\n");
		return -1;
	}

	aCodecCtx = pFormatCtx->streams[audioStream]->codec;
	aCodec = avcodec_find_decoder(aCodecCtx->codec_id);

	if (!aCodec) {
		ERROR("Unsupported audio codec!\n");
		return -1;
	}

	if (avcodec_open(aCodecCtx, aCodec)<0) {
		ERROR("Could not open codec!\n");
		return -1;
	}

	if (callback) {
		ptrs->audioStream = audioStream;
		ptrs->pFormatCtx = pFormatCtx;
		ptrs->aCodecCtx = aCodecCtx;
		ptrs->aCodec = aCodec;

		ret = (*callback)( ptrs );
	} else {
		ret = 0;
		DEBUG("playable\n");
	}

	avcodec_close(aCodecCtx);
	av_close_input_file(pFormatCtx);

	return ret;
}

static bool ffmpeg_try_decode(InputStream *input)
{
	int ret;
	if (input->seekable) {
		ret = ffmpeg_helper(input, NULL, NULL);
	} else {
		ret = 0;
	}
	return (ret == -1 ? 0 : 1);
}

static int ffmpeg_decode_internal(BasePtrs *base)
{
	struct decoder *decoder = base->decoder;
	AVCodecContext *aCodecCtx = base->aCodecCtx;
	AVFormatContext *pFormatCtx = base->pFormatCtx;
	AVPacket packet;
	int len, audio_size;
	int position;
	struct audio_format audio_format;
	int current, total_time;
	uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];

	total_time = 0;

	DEBUG("decoder_start\n");

	if (aCodecCtx->channels > 2) {
		aCodecCtx->channels = 2;
	}

	audio_format.bits = (uint8_t)16;
	audio_format.sample_rate = (unsigned int)aCodecCtx->sample_rate;
	audio_format.channels = aCodecCtx->channels;

	//    frame_count = afGetFrameCount(af_fp, AF_DEFAULT_TRACK);
	//    total_time = ((float)frame_count / (float)audio_format.sample_rate);

	//there is some problem with this on some demux (mp3 at least)
	if (pFormatCtx->duration != (int)AV_NOPTS_VALUE) {
		total_time = pFormatCtx->duration / AV_TIME_BASE;
	}

	DEBUG("ffmpeg sample rate: %dHz %d channels\n",
	      aCodecCtx->sample_rate, aCodecCtx->channels);

	decoder_initialized(decoder, &audio_format, total_time);

	position = 0;

	DEBUG("duration:%d (%d secs)\n", (int) pFormatCtx->duration,
	      (int) total_time);

	do {

		if (decoder_get_command(decoder) == DECODE_COMMAND_SEEK) {

			DEBUG("seek\n");
			decoder_clear(decoder);
			current = decoder_seek_where(decoder) * AV_TIME_BASE;

			if (av_seek_frame(pFormatCtx, -1, current , 0) < 0) {
				WARNING("seek to %d failed\n", current);
			}

			decoder_command_finished(decoder);
		}

		if (av_read_frame(pFormatCtx, &packet) >= 0) {
			if(packet.stream_index == base->audioStream) {

				position = av_rescale_q(packet.pts, pFormatCtx->streams[base->audioStream]->time_base,
							(AVRational){1, 1});

				audio_size = sizeof(audio_buf);
				len = avcodec_decode_audio2(aCodecCtx,
							    (int16_t *)audio_buf,
							    &audio_size,
							    packet.data,
							    packet.size);

				if(len >= 0) {
					if(audio_size >= 0) {
						// DEBUG("sending data %d/%d\n", audio_size, len);

						decoder_data(decoder, NULL, 1,
							     audio_buf, audio_size,
							     position, //(float)current / (float)audio_format.sample_rate,
							     aCodecCtx->bit_rate / 1000, NULL);

					}
				} else {
					WARNING("skiping frame!\n");
				}
			}
			av_free_packet(&packet);
		} else {
			//end of file
			break;
		}
	} while (decoder_get_command(decoder) != DECODE_COMMAND_STOP);

	decoder_flush(decoder);

	DEBUG("decoder finish\n");

	return 0;
}

static int ffmpeg_decode(struct decoder *decoder, InputStream *input)
{
	BasePtrs base;
	int ret;

	DEBUG("decode start\n");

	base.input = input;
	base.decoder = decoder;

	ret = ffmpeg_helper(input, ffmpeg_decode_internal, &base);

	DEBUG("decode finish\n");

	return ret;
}

static int ffmpeg_tag_internal(BasePtrs *base)
{
	struct tag *tag = (struct tag *) base->tag;

	if (base->pFormatCtx->duration != (int)AV_NOPTS_VALUE) {
		tag->time = base->pFormatCtx->duration / AV_TIME_BASE;
	} else {
		tag->time = 0;
	}
	return 0;
}

//no tag reading in ffmpeg, check if playable
static struct tag *ffmpeg_tag(char *file)
{
	InputStream input;
	BasePtrs base;
	int ret;
	struct tag *tag = NULL;

	if (openInputStream(&input, file) < 0) {
		ERROR("failed to open %s\n", file);
		return NULL;
	}

	tag = tag_new();

	base.tag = tag;
	ret = ffmpeg_helper(&input, ffmpeg_tag_internal, &base);

	if (ret != 0) {
		free(tag);
		tag = NULL;
	}

	closeInputStream(&input);

	return tag;
}

/**
 * ffmpeg can decode almost everything from open codecs
 * and also some of propietary codecs
 * its hard to tell what can ffmpeg decode
 * we can later put this into configure script
 * to be sure ffmpeg is used to handle
 * only that files
 */

static const char *ffmpeg_Suffixes[] = {
	"wma", "asf", "wmv", "mpeg", "mpg", "avi", "vob", "mov", "qt", "swf", "rm", "swf",
	"mp1", "mp2", "mp3", "mp4", "m4a", "flac", "ogg", "wav", "au", "aiff", "aif", "ac3", "aac", "mpc",
	NULL
};

//not sure if this is correct...
static const char *ffmpeg_Mimetypes[] = {
	"video/x-ms-asf",
	"audio/x-ms-wma",
	"audio/x-ms-wax",
	"video/x-ms-wmv",
	"video/x-ms-wvx",
	"video/x-ms-wm",
	"video/x-ms-wmx",
	"application/x-ms-wmz",
	"application/x-ms-wmd",
	"audio/mpeg",
	NULL
};

struct decoder_plugin ffmpegPlugin = {
	.name = "ffmpeg",
	.init = ffmpeg_init,
	.try_decode = ffmpeg_try_decode,
	.stream_decode = ffmpeg_decode,
	.tag_dup = ffmpeg_tag,
	.stream_types = INPUT_PLUGIN_STREAM_URL | INPUT_PLUGIN_STREAM_FILE,
	.suffixes = ffmpeg_Suffixes,
	.mime_types = ffmpeg_Mimetypes
};