+static void mp3dec_post_select(__a_unused struct sched *s, struct task *t)
+{
+ struct filter_node *fn = container_of(t, struct filter_node, task);
+ int i, ret;
+ struct private_mp3dec_data *pmd = fn->private_data;
+ struct btr_node *btrn = fn->btrn;
+ size_t loaded, used, len, iqs;
+ char *inbuffer, *outbuffer;
+
+next_buffer:
+ pmd->stream.error = 0;
+ t->error = 0;
+ iqs = btr_get_input_queue_size(btrn);
+ if (need_bad_data_delay(pmd, iqs))
+ return;
+ ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
+ if (ret < 0)
+ goto err;
+ if (ret == 0)
+ return;
+ btr_merge(btrn, fn->min_iqs);
+ len = btr_next_buffer(btrn, &inbuffer);
+ mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
+next_frame:
+ ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
+ if (ret < 0) {
+ used = used_mad_buffer_bytes(&pmd->stream, len);
+ btr_consume(btrn, used);
+ if (pmd->stream.error == MAD_ERROR_BUFLEN) {
+ if (len == iqs && btr_no_parent(btrn)) {
+ ret = -E_MP3DEC_EOF;
+ goto err;
+ }
+ fn->min_iqs += 100;
+ goto next_buffer;
+ } else if (pmd->stream.error != MAD_ERROR_LOSTSYNC)
+ PARA_DEBUG_LOG("header decode: %s\n",
+ mad_stream_errorstr(&pmd->stream));
+ goto next_buffer;
+ }
+ fn->min_iqs = 0;
+ pmd->samplerate = pmd->frame.header.samplerate;
+ pmd->channels = MAD_NCHANNELS(&pmd->frame.header);
+ ret = mad_frame_decode(&pmd->frame, &pmd->stream);
+ if (ret != 0) {
+ PARA_CRIT_LOG("frame decode: %d\n", ret);
+ used = used_mad_buffer_bytes(&pmd->stream, len);
+ ret = handle_decode_error(pmd, used);
+ btr_consume(btrn, used);
+ if (ret < 0)
+ goto err;
+ if (ret == 0)
+ goto next_buffer;
+ return;
+ }
+ mad_synth_frame(&pmd->synth, &pmd->frame);
+ pmd->flags |= MP3DEC_FLAG_DECODE_STARTED;
+
+ outbuffer = para_malloc(pmd->synth.pcm.length * 4);
+ loaded = 0;
+ for (i = 0; i < pmd->synth.pcm.length; i++) {
+ int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
+ write_int16_host_endian(outbuffer + loaded, sample);
+ loaded += 2;
+ if (MAD_NCHANNELS(&pmd->frame.header) == 2) { /* stereo */
+ sample = MAD_TO_SHORT(pmd->synth.pcm.samples[1][i]);
+ write_int16_host_endian(outbuffer + loaded, sample);
+ loaded += 2;
+ }
+ }
+ btr_add_output(outbuffer, loaded, btrn);
+ goto next_frame;
+err:
+ assert(ret < 0);
+ t->error = ret;
+ btr_remove_node(btrn);
+}
+