|
| 1 | +/* |
| 2 | + * SVAR - writer-opus.c |
| 3 | + * SPDX-FileCopyrightText: 2010-2025 Arkadiusz Bokowy and contributors |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | + |
| 7 | +#include "writer-opus.h" |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | +#include <stdbool.h> |
| 11 | +#include <stdint.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | + |
| 16 | +#include <opusenc.h> |
| 17 | + |
| 18 | +#include "log.h" |
| 19 | +#include "pcm.h" |
| 20 | +#include "writer.h" |
| 21 | + |
| 22 | +struct writer_opus { |
| 23 | + OggOpusEnc * enc; |
| 24 | + OggOpusComments * comments; |
| 25 | + char * pathname; |
| 26 | + int bitrate_min; |
| 27 | + int bitrate_max; |
| 28 | + unsigned int channels; |
| 29 | + unsigned int sampling; |
| 30 | +}; |
| 31 | + |
| 32 | +static int writer_opus_open(struct writer * writer, const char * pathname) { |
| 33 | + struct writer_opus * w = writer->w; |
| 34 | + |
| 35 | + writer->close(writer); |
| 36 | + |
| 37 | + /* Store the pathname for later use when creating the encoder */ |
| 38 | + free(w->pathname); |
| 39 | + if ((w->pathname = strdup(pathname)) == NULL) |
| 40 | + return -1; |
| 41 | + |
| 42 | + int error; |
| 43 | + /* Create encoder with file path, comments, sample rate, channels, and family |
| 44 | + * Family 0 is used for mono/stereo (up to 2 channels) */ |
| 45 | + int family = w->channels <= 2 ? 0 : 1; |
| 46 | + w->enc = ope_encoder_create_file(w->pathname, w->comments, w->sampling, w->channels, family, &error); |
| 47 | + |
| 48 | + if (w->enc == NULL) { |
| 49 | + error("Couldn't create Opus encoder: %s", ope_strerror(error)); |
| 50 | + errno = EINVAL; |
| 51 | + return -1; |
| 52 | + } |
| 53 | + |
| 54 | + /* Set bitrate constraints using encoder control interface */ |
| 55 | + if (w->bitrate_max > 0) { |
| 56 | + if (ope_encoder_ctl(w->enc, OPUS_SET_BITRATE(w->bitrate_max)) != OPE_OK) |
| 57 | + warn("Couldn't set Opus max bitrate"); |
| 58 | + } |
| 59 | + |
| 60 | + writer->opened = true; |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +static void writer_opus_close(struct writer * writer) { |
| 65 | + struct writer_opus * w = writer->w; |
| 66 | + writer->opened = false; |
| 67 | + |
| 68 | + if (w->enc == NULL) |
| 69 | + return; |
| 70 | + |
| 71 | + /* Finalize the stream */ |
| 72 | + ope_encoder_drain(w->enc); |
| 73 | + ope_encoder_destroy(w->enc); |
| 74 | + w->enc = NULL; |
| 75 | +} |
| 76 | + |
| 77 | +static ssize_t writer_opus_write(struct writer * writer, const void * buffer, size_t frames) { |
| 78 | + struct writer_opus * w = writer->w; |
| 79 | + |
| 80 | + /* Write 16-bit PCM samples to the encoder */ |
| 81 | + int ret = ope_encoder_write(w->enc, (const opus_int16 *)buffer, frames); |
| 82 | + |
| 83 | + if (ret != OPE_OK) { |
| 84 | + error("Opus encoding error: %s", ope_strerror(ret)); |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + /* Return the number of frames written (libopusenc doesn't return byte count) */ |
| 89 | + return frames; |
| 90 | +} |
| 91 | + |
| 92 | +static void writer_opus_free(struct writer * writer) { |
| 93 | + if (writer == NULL) |
| 94 | + return; |
| 95 | + struct writer_opus * w = writer->w; |
| 96 | + writer->close(writer); |
| 97 | + if (w->comments != NULL) |
| 98 | + ope_comments_destroy(w->comments); |
| 99 | + free(w->pathname); |
| 100 | + free(writer->w); |
| 101 | + free(writer); |
| 102 | +} |
| 103 | + |
| 104 | +struct writer * writer_opus_new( |
| 105 | + enum pcm_format format, unsigned int channels, unsigned int sampling, |
| 106 | + int bitrate_min, int bitrate_max, const char * comment) { |
| 107 | + |
| 108 | + if (format != PCM_FORMAT_S16LE) { |
| 109 | + error("Opus unsupported PCM format: %s", pcm_format_name(format)); |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + |
| 113 | + struct writer * writer; |
| 114 | + if ((writer = malloc(sizeof(*writer))) == NULL) |
| 115 | + return NULL; |
| 116 | + |
| 117 | + writer->type = WRITER_TYPE_OPUS; |
| 118 | + writer->opened = false; |
| 119 | + writer->open = writer_opus_open; |
| 120 | + writer->write = writer_opus_write; |
| 121 | + writer->close = writer_opus_close; |
| 122 | + writer->free = writer_opus_free; |
| 123 | + |
| 124 | + struct writer_opus * w; |
| 125 | + if ((writer->w = w = calloc(1, sizeof(*w))) == NULL) { |
| 126 | + free(writer); |
| 127 | + return NULL; |
| 128 | + } |
| 129 | + |
| 130 | + w->bitrate_min = bitrate_min; |
| 131 | + w->bitrate_max = bitrate_max; |
| 132 | + w->channels = channels; |
| 133 | + w->sampling = sampling; |
| 134 | + |
| 135 | + /* Create comments object */ |
| 136 | + w->comments = ope_comments_create(); |
| 137 | + if (w->comments == NULL) { |
| 138 | + writer_opus_free(writer); |
| 139 | + return NULL; |
| 140 | + } |
| 141 | + |
| 142 | + if (comment != NULL) { |
| 143 | + char tag[8 + strlen(comment) + 1]; |
| 144 | + snprintf(tag, sizeof(tag), "ENCODER=%s", comment); |
| 145 | + ope_comments_add_string(w->comments, tag); |
| 146 | + } |
| 147 | + |
| 148 | + return writer; |
| 149 | +} |
0 commit comments