+
+/* returns > 1 if packet was decoded, 0 if it was ignored, negative on errors. */
+static int decode_packet(struct opusdec_context *ctx, ogg_packet *op,
+ struct btr_node *btrn)
+{
+ int ret;
+ short *output;
+
+ /*
+ * OggOpus streams are identified by a magic string in the initial
+ * stream header.
+ */
+ if (op->b_o_s && op->bytes >= 8 && !memcmp(op->packet, "OpusHead", 8)) {
+ if (!ctx->have_opus_stream) {
+ ctx->opus_serialno = ctx->os.serialno;
+ ctx->have_opus_stream = true;
+ ctx->packet_count = 0;
+ ctx->eos = false;
+ ctx->total_links++;
+ } else
+ PARA_NOTICE_LOG("ignoring opus stream %llu\n",
+ (long long unsigned)ctx->os.serialno);
+ }
+ if (!ctx->have_opus_stream || ctx->os.serialno != ctx->opus_serialno)
+ return 0;
+ /* If first packet in a logical stream, process the Opus header. */
+ if (ctx->packet_count == 0)
+ return opusdec_init(op, ctx);
+ if (ctx->packet_count == 1)
+ return 1;
+ /* don't care for anything except opus eos */
+ if (op->e_o_s && ctx->os.serialno == ctx->opus_serialno)
+ ctx->eos = true;
+ output = para_malloc(sizeof(short) * MAX_FRAME_SIZE * ctx->channels);
+ ret = opus_multistream_decode(ctx->st, (unsigned char *)op->packet,
+ op->bytes, output, MAX_FRAME_SIZE, 0);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", opus_strerror(ret));
+ free(output);
+ return -E_OPUS_DECODE;
+ }
+ opusdec_add_output(output, ret, btrn, ctx);
+ return 1;
+}
+
+#define OPUSDEC_MAX_OUTPUT_SIZE (1024 * 1024)
+
+static int opusdec_post_select(__a_unused struct sched *s, void *context)
+{
+ struct filter_node *fn = context;
+ struct opusdec_context *ctx = fn->private_data;
+ struct btr_node *btrn = fn->btrn;
+ int ret;
+ ogg_packet op;
+
+ ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
+ if (ret < 0) {
+ if (ret != -E_BTR_EOF) /* fatal error */
+ goto out;
+ if (!ctx->have_more) /* EOF */
+ goto out;
+ } else if (ret == 0 && !ctx->have_more) /* nothing to do */
+ goto out;
+ if (btr_get_output_queue_size(btrn) > OPUSDEC_MAX_OUTPUT_SIZE)
+ return 0;
+ for (;;) {
+ int serial;
+ if (ctx->stream_init) {
+ ret = ogg_stream_packetout(&ctx->os, &op);
+ if (ret == 1)
+ break;
+ }
+ while (ogg_sync_pageout(&ctx->oy, &ctx->ogg_page) != 1) {
+ char *btr_buf, *data;
+ size_t nbytes = btr_next_buffer(btrn, &btr_buf);
+ nbytes = PARA_MIN(nbytes, (size_t)24 * 1024);
+ //PARA_CRIT_LOG("nbytes: %d\n", nbytes);
+ ctx->have_more = false;
+ if (nbytes == 0)
+ return 0;
+ data = ogg_sync_buffer(&ctx->oy, nbytes);
+ memcpy(data, btr_buf, nbytes);
+ btr_consume(btrn, nbytes);
+ ogg_sync_wrote(&ctx->oy, nbytes);
+ }
+ ctx->have_more = true;
+ serial = ogg_page_serialno(&ctx->ogg_page);
+ if (ctx->stream_init) {
+ if (serial != ctx->os.serialno)
+ ogg_stream_reset_serialno(&ctx->os, serial);
+ } else {
+ ogg_stream_init(&ctx->os, serial);
+ ctx->stream_init = true;
+ }
+ /* Add page to the bitstream */
+ ogg_stream_pagein(&ctx->os, &ctx->ogg_page);
+ }
+ ret = decode_packet(ctx, &op, btrn);
+ if (ret < 0)
+ goto out;
+ ctx->packet_count++;
+ if (ctx->eos)
+ ctx->have_opus_stream = false;
+out:
+ if (ret < 0)
+ btr_remove_node(&fn->btrn);
+ return ret;
+}
+
+static void opusdec_pre_select(struct sched *s, void *context)
+{
+ struct filter_node *fn = context;
+ struct opusdec_context *ctx = fn->private_data;
+ int ret = btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL);
+
+ if (ret != 0)
+ return sched_min_delay(s);
+ if (!ctx->have_more)
+ return;
+ if (btr_get_output_queue_size(fn->btrn) <= OPUSDEC_MAX_OUTPUT_SIZE)
+ return sched_min_delay(s);
+}
+
+const struct filter lsg_filter_cmd_com_opusdec_user_data = {
+ .open = opusdec_open,
+ .close = opusdec_close,
+ .pre_select = opusdec_pre_select,
+ .post_select = opusdec_post_select,
+ .execute = opusdec_execute,
+};