aboutsummaryrefslogtreecommitdiffstats
path: root/src/pq_format.c
blob: c2bb52e7ca742ced46ce11919073512bcbf8e68b (plain)
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
/* Process Queue: Format handling tasks */

/*
 * This file is part of gapk (GSM Audio Pocket Knife).
 *
 * gapk 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 3 of the License, or
 * (at your option) any later version.
 *
 * gapk 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 gapk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <stdint.h>

#include <osmocom/gapk/logging.h>
#include <osmocom/gapk/codecs.h>
#include <osmocom/gapk/formats.h>
#include <osmocom/gapk/procqueue.h>


static int
pq_cb_fmt_convert(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_len)
{
	osmo_gapk_fmt_conv_cb_t f = _state;
	return f(out, in, in_len);
}

/*! Add format conversion to processing queue
 *  \param pq Processing Queue to add conversion to
 *  \param[in] fmt Format description for conversion
 *  \param[in] to_from_n convert to (0) or from (1) specified format */
int
osmo_gapk_pq_queue_fmt_convert(struct osmo_gapk_pq *pq, const struct osmo_gapk_format_desc *fmt, int to_from_n)
{
	const struct osmo_gapk_codec_desc *codec;
	struct osmo_gapk_pq_item *item;

	codec = osmo_gapk_codec_get_from_type(fmt->codec_type);
	if (!codec) {
		LOGPGAPK(LOGL_ERROR, "Cannot determine codec from "
			"format %s\n", fmt->name);
		return -EINVAL;
	}


	item = osmo_gapk_pq_add_item(pq);
	if (!item)
		return -ENOMEM;

	if (to_from_n) {
		LOGPGAPK(LOGL_DEBUG, "PQ: Adding conversion from canon "
			"to %s (for codec %s)\n", fmt->name, codec->name);
		item->len_in  = codec->canon_frame_len;
		item->len_out = fmt->frame_len;
		item->state   = fmt->conv_from_canon;
	} else {
		LOGPGAPK(LOGL_DEBUG, "PQ: Adding conversion from %s "
			"to canon (for codec %s)\n", fmt->name, codec->name);
		item->len_in  = fmt->frame_len;
		item->len_out = codec->canon_frame_len;
		item->state   = fmt->conv_to_canon;
	}

	item->type = OSMO_GAPK_ITEM_TYPE_PROC;
	item->proc = pq_cb_fmt_convert;
	item->wait = NULL;

	/* Meta information */
	item->cat_name = "format";
	item->sub_name = fmt->name;

	return 0;
}