Mulberry/Native/ffdecode.c
2026-10-27 20:22:19 -05:00

187 lines
6.8 KiB
C

#include "ffdecode.h"
#include <stdlib.h>
#include <string.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#define OUT_RATE 48000
struct ff_dec {
AVFormatContext *fmt;
AVCodecContext *codec;
SwrContext *swr;
AVPacket *pkt;
AVFrame *frame;
int stream;
int eof;
/* converted samples not yet handed out */
float *bufL, *bufR;
int bufLen, bufPos, bufCap;
/* frames to drop after a seek so we land exactly */
int64_t skip;
};
static int setup_swr(ff_dec *d) {
AVChannelLayout out = AV_CHANNEL_LAYOUT_STEREO;
if (d->swr) swr_free(&d->swr);
AVChannelLayout in;
if (d->codec->ch_layout.nb_channels > 0) av_channel_layout_copy(&in, &d->codec->ch_layout);
else av_channel_layout_default(&in, 2);
int r = swr_alloc_set_opts2(&d->swr, &out, AV_SAMPLE_FMT_FLTP, OUT_RATE,
&in, d->codec->sample_fmt, d->codec->sample_rate, 0, NULL);
av_channel_layout_uninit(&in);
if (r < 0) return r;
return swr_init(d->swr);
}
ff_dec *ff_open(const char *path) {
ff_dec *d = calloc(1, sizeof *d);
if (!d) return NULL;
if (avformat_open_input(&d->fmt, path, NULL, NULL) < 0) goto fail;
if (avformat_find_stream_info(d->fmt, NULL) < 0) goto fail;
const AVCodec *dec = NULL;
d->stream = av_find_best_stream(d->fmt, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
if (d->stream < 0 || !dec) goto fail;
d->codec = avcodec_alloc_context3(dec);
if (!d->codec) goto fail;
if (avcodec_parameters_to_context(d->codec, d->fmt->streams[d->stream]->codecpar) < 0) goto fail;
if (avcodec_open2(d->codec, dec, NULL) < 0) goto fail;
if (setup_swr(d) < 0) goto fail;
d->pkt = av_packet_alloc();
d->frame = av_frame_alloc();
if (!d->pkt || !d->frame) goto fail;
for (unsigned i = 0; i < d->fmt->nb_streams; i++)
if ((int)i != d->stream) d->fmt->streams[i]->discard = AVDISCARD_ALL;
return d;
fail:
ff_close(d);
return NULL;
}
int64_t ff_frames(ff_dec *d) {
AVStream *st = d->fmt->streams[d->stream];
if (st->duration > 0 && st->time_base.den)
return av_rescale_q(st->duration, st->time_base, (AVRational){1, OUT_RATE});
if (d->fmt->duration > 0) return av_rescale(d->fmt->duration, OUT_RATE, AV_TIME_BASE);
return 0;
}
static int ensure_cap(ff_dec *d, int n) {
if (n <= d->bufCap) return 0;
float *l = realloc(d->bufL, sizeof(float) * n);
if (!l) return -1;
d->bufL = l;
float *r = realloc(d->bufR, sizeof(float) * n);
if (!r) return -1;
d->bufR = r;
d->bufCap = n;
return 0;
}
/* Decodes the next chunk into the internal buffer. Returns >0 frames, 0 at EOF, <0 on error. */
static int refill(ff_dec *d) {
for (;;) {
int r = avcodec_receive_frame(d->codec, d->frame);
if (r == 0) {
if (d->frame->sample_rate != d->codec->sample_rate ||
av_channel_layout_compare(&d->frame->ch_layout, &d->codec->ch_layout)) {
d->codec->sample_rate = d->frame->sample_rate;
av_channel_layout_uninit(&d->codec->ch_layout);
av_channel_layout_copy(&d->codec->ch_layout, &d->frame->ch_layout);
d->codec->sample_fmt = d->frame->format;
if (setup_swr(d) < 0) return -1;
}
int want = swr_get_out_samples(d->swr, d->frame->nb_samples);
if (ensure_cap(d, want + 64) < 0) return -1;
uint8_t *out[2] = {(uint8_t *)d->bufL, (uint8_t *)d->bufR};
int n = swr_convert(d->swr, out, d->bufCap, (const uint8_t **)d->frame->extended_data, d->frame->nb_samples);
av_frame_unref(d->frame);
if (n < 0) return n;
d->bufLen = n; d->bufPos = 0;
if (n > 0) return n;
continue;
}
if (r == AVERROR_EOF) {
/* flush the resampler tail */
if (ensure_cap(d, 4096) < 0) return -1;
uint8_t *out[2] = {(uint8_t *)d->bufL, (uint8_t *)d->bufR};
int n = swr_convert(d->swr, out, d->bufCap, NULL, 0);
d->bufLen = n > 0 ? n : 0; d->bufPos = 0;
return d->bufLen;
}
if (r != AVERROR(EAGAIN)) return r;
if (d->eof) { avcodec_send_packet(d->codec, NULL); continue; }
r = av_read_frame(d->fmt, d->pkt);
if (r < 0) {
d->eof = 1;
avcodec_send_packet(d->codec, NULL);
continue;
}
if (d->pkt->stream_index == d->stream) avcodec_send_packet(d->codec, d->pkt);
av_packet_unref(d->pkt);
}
}
int ff_read(ff_dec *d, float *left, float *right, int max) {
int done = 0;
while (done < max) {
if (d->bufPos >= d->bufLen) {
int r = refill(d);
if (r < 0) return done > 0 ? done : r;
if (r == 0) break;
}
int avail = d->bufLen - d->bufPos;
if (d->skip > 0) {
int drop = avail < d->skip ? avail : (int)d->skip;
d->bufPos += drop; d->skip -= drop;
continue;
}
int n = avail < max - done ? avail : max - done;
memcpy(left + done, d->bufL + d->bufPos, sizeof(float) * n);
memcpy(right + done, d->bufR + d->bufPos, sizeof(float) * n);
d->bufPos += n; done += n;
}
return done;
}
void ff_seek(ff_dec *d, int64_t frame) {
AVStream *st = d->fmt->streams[d->stream];
int64_t ts = av_rescale_q(frame, (AVRational){1, OUT_RATE}, st->time_base);
if (st->start_time != AV_NOPTS_VALUE) ts += st->start_time;
if (av_seek_frame(d->fmt, d->stream, ts, AVSEEK_FLAG_BACKWARD) < 0)
av_seek_frame(d->fmt, -1, av_rescale(frame, AV_TIME_BASE, OUT_RATE), AVSEEK_FLAG_BACKWARD);
avcodec_flush_buffers(d->codec);
setup_swr(d);
d->eof = 0;
d->bufLen = d->bufPos = 0;
/* Decode forward to the exact frame: find where we landed from the first packet's pts. */
d->skip = 0;
for (;;) {
int r = av_read_frame(d->fmt, d->pkt);
if (r < 0) { d->eof = 1; break; }
if (d->pkt->stream_index != d->stream) { av_packet_unref(d->pkt); continue; }
if (d->pkt->pts != AV_NOPTS_VALUE) {
int64_t at = d->pkt->pts - (st->start_time != AV_NOPTS_VALUE ? st->start_time : 0);
int64_t landed = av_rescale_q(at, st->time_base, (AVRational){1, OUT_RATE});
if (frame > landed) d->skip = frame - landed;
}
avcodec_send_packet(d->codec, d->pkt);
av_packet_unref(d->pkt);
break;
}
}
void ff_close(ff_dec *d) {
if (!d) return;
if (d->swr) swr_free(&d->swr);
if (d->codec) avcodec_free_context(&d->codec);
if (d->fmt) avformat_close_input(&d->fmt);
if (d->pkt) av_packet_free(&d->pkt);
if (d->frame) av_frame_free(&d->frame);
free(d->bufL);
free(d->bufR);
free(d);
}