3 * Copyright (c) 2023 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/internal.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
26 #include "codec_internal.h"
29 #define BITSTREAM_READER_LE
35 typedef struct OSQChannel
{
38 unsigned residue_parameter
;
39 unsigned residue_bits
;
46 typedef struct OSQContext
{
52 size_t bitstream_size
;
59 int32_t *decode_buffer
[2];
65 static void osq_flush(AVCodecContext
*avctx
)
67 OSQContext
*s
= avctx
->priv_data
;
69 s
->bitstream_size
= 0;
73 static av_cold
int osq_close(AVCodecContext
*avctx
)
75 OSQContext
*s
= avctx
->priv_data
;
77 av_freep(&s
->bitstream
);
78 s
->bitstream_size
= 0;
80 for (int ch
= 0; ch
< FF_ARRAY_ELEMS(s
->decode_buffer
); ch
++)
81 av_freep(&s
->decode_buffer
[ch
]);
86 static av_cold
int osq_init(AVCodecContext
*avctx
)
88 OSQContext
*s
= avctx
->priv_data
;
90 if (avctx
->extradata_size
< 48)
91 return AVERROR(EINVAL
);
93 if (avctx
->extradata
[0] != 1) {
94 av_log(avctx
, AV_LOG_ERROR
, "Unsupported version.\n");
95 return AVERROR_INVALIDDATA
;
98 avctx
->sample_rate
= AV_RL32(avctx
->extradata
+ 4);
99 if (avctx
->sample_rate
< 1)
100 return AVERROR_INVALIDDATA
;
102 av_channel_layout_uninit(&avctx
->ch_layout
);
103 avctx
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
104 avctx
->ch_layout
.nb_channels
= avctx
->extradata
[3];
105 if (avctx
->ch_layout
.nb_channels
< 1)
106 return AVERROR_INVALIDDATA
;
107 if (avctx
->ch_layout
.nb_channels
> FF_ARRAY_ELEMS(s
->decode_buffer
))
108 return AVERROR_INVALIDDATA
;
111 switch (avctx
->extradata
[2]) {
112 case 8: avctx
->sample_fmt
= AV_SAMPLE_FMT_U8P
; break;
113 case 16: avctx
->sample_fmt
= AV_SAMPLE_FMT_S16P
; break;
115 case 24: s
->factor
= 256;
116 avctx
->sample_fmt
= AV_SAMPLE_FMT_S32P
; break;
117 default: return AVERROR_INVALIDDATA
;
120 avctx
->bits_per_raw_sample
= avctx
->extradata
[2];
121 s
->nb_samples
= AV_RL64(avctx
->extradata
+ 16);
122 s
->frame_samples
= AV_RL16(avctx
->extradata
+ 8);
123 s
->max_framesize
= (s
->frame_samples
* 16 + 1024) * avctx
->ch_layout
.nb_channels
;
125 s
->bitstream
= av_calloc(s
->max_framesize
+ AV_INPUT_BUFFER_PADDING_SIZE
, sizeof(*s
->bitstream
));
127 return AVERROR(ENOMEM
);
129 for (int ch
= 0; ch
< avctx
->ch_layout
.nb_channels
; ch
++) {
130 s
->decode_buffer
[ch
] = av_calloc(s
->frame_samples
+ OFFSET
,
131 sizeof(*s
->decode_buffer
[ch
]));
132 if (!s
->decode_buffer
[ch
])
133 return AVERROR(ENOMEM
);
136 s
->pkt
= avctx
->internal
->in_pkt
;
141 static void reset_stats(OSQChannel
*cb
)
143 memset(cb
->history
, 0, sizeof(cb
->history
));
144 cb
->pos
= cb
->count
= cb
->sum
= 0;
147 static void update_stats(OSQChannel
*cb
, int val
)
149 cb
->sum
+= FFABS(val
) - cb
->history
[cb
->pos
];
150 cb
->history
[cb
->pos
] = FFABS(val
);
153 if (cb
->pos
>= FF_ARRAY_ELEMS(cb
->history
))
157 static int update_residue_parameter(OSQChannel
*cb
)
166 rice_k
= ceil(log2(x
));
168 double f
= floor(sum
/ 1.4426952 + 0.5);
171 } else if (f
>= 31) {
180 static uint32_t get_urice(GetBitContext
*gb
, int k
)
184 x
= get_unary(gb
, 1, 512);
185 b
= get_bits_long(gb
, k
);
191 static int32_t get_srice(GetBitContext
*gb
, int x
)
193 int32_t y
= get_urice(gb
, x
);
194 return get_bits1(gb
) ? -y
: y
;
197 static int osq_channel_parameters(AVCodecContext
*avctx
, int ch
)
199 OSQContext
*s
= avctx
->priv_data
;
200 OSQChannel
*cb
= &s
->ch
[ch
];
201 GetBitContext
*gb
= &s
->gb
;
204 cb
->prediction
= get_urice(gb
, 5);
205 cb
->coding_mode
= get_urice(gb
, 3);
206 if (cb
->prediction
>= 15)
207 return AVERROR_INVALIDDATA
;
208 if (cb
->coding_mode
> 0 && cb
->coding_mode
< 3) {
209 cb
->residue_parameter
= get_urice(gb
, 4);
210 if (!cb
->residue_parameter
|| cb
->residue_parameter
>= 31)
211 return AVERROR_INVALIDDATA
;
212 } else if (cb
->coding_mode
== 3) {
213 cb
->residue_bits
= get_urice(gb
, 4);
214 if (!cb
->residue_bits
|| cb
->residue_bits
>= 31)
215 return AVERROR_INVALIDDATA
;
216 } else if (cb
->coding_mode
) {
217 return AVERROR_INVALIDDATA
;
220 if (cb
->coding_mode
== 2)
231 #define P2 (((unsigned)dst[A] + dst[A]) - dst[B])
232 #define P3 (((unsigned)dst[A] - dst[B]) * 3 + dst[C])
234 static int do_decode(AVCodecContext
*avctx
, AVFrame
*frame
, int decorrelate
, int downsample
)
236 OSQContext
*s
= avctx
->priv_data
;
237 const int nb_channels
= avctx
->ch_layout
.nb_channels
;
238 const int nb_samples
= frame
->nb_samples
;
239 GetBitContext
*gb
= &s
->gb
;
241 for (int n
= 0; n
< nb_samples
; n
++) {
242 for (int ch
= 0; ch
< nb_channels
; ch
++) {
243 OSQChannel
*cb
= &s
->ch
[ch
];
244 int32_t *dst
= s
->decode_buffer
[ch
] + OFFSET
;
245 int32_t p
, prev
= cb
->prev
;
247 if (nb_channels
== 2 && ch
== 1 && decorrelate
!= s
->decorrelate
) {
249 s
->decode_buffer
[1][OFFSET
+A
] += s
->decode_buffer
[0][OFFSET
+B
];
250 s
->decode_buffer
[1][OFFSET
+B
] += s
->decode_buffer
[0][OFFSET
+C
];
251 s
->decode_buffer
[1][OFFSET
+C
] += s
->decode_buffer
[0][OFFSET
+D
];
252 s
->decode_buffer
[1][OFFSET
+D
] += s
->decode_buffer
[0][OFFSET
+E
];
254 s
->decode_buffer
[1][OFFSET
+A
] -= s
->decode_buffer
[0][OFFSET
+B
];
255 s
->decode_buffer
[1][OFFSET
+B
] -= s
->decode_buffer
[0][OFFSET
+C
];
256 s
->decode_buffer
[1][OFFSET
+C
] -= s
->decode_buffer
[0][OFFSET
+D
];
257 s
->decode_buffer
[1][OFFSET
+D
] -= s
->decode_buffer
[0][OFFSET
+E
];
259 s
->decorrelate
= decorrelate
;
262 if (!cb
->coding_mode
) {
264 } else if (cb
->coding_mode
== 3) {
265 dst
[n
] = get_sbits_long(gb
, cb
->residue_bits
);
267 dst
[n
] = get_srice(gb
, cb
->residue_parameter
);
270 if (get_bits_left(gb
) < 0) {
271 av_log(avctx
, AV_LOG_ERROR
, "overread!\n");
272 return AVERROR_INVALIDDATA
;
278 switch (cb
->prediction
) {
282 dst
[n
] += (unsigned)dst
[A
];
285 dst
[n
] += (unsigned)dst
[A
] + p
;
300 dst
[n
] += (int)(P2
+ P3
) / 2 + (unsigned)p
;
303 dst
[n
] += (int)(P2
+ P3
) / 2 + 0U;
306 dst
[n
] += (int)(P2
* 2 + P3
) / 3 + (unsigned)p
;
309 dst
[n
] += (int)(P2
+ P3
* 2) / 3 + (unsigned)p
;
312 dst
[n
] += (int)((unsigned)dst
[A
] + dst
[B
]) / 2 + 0U;
315 dst
[n
] += (unsigned)dst
[B
];
318 dst
[n
] += (int)((unsigned)dst
[D
] + dst
[B
]) / 2 + 0U;
321 dst
[n
] += (int)((unsigned)P2
+ dst
[A
]) / 2 + (unsigned)p
;
324 return AVERROR_INVALIDDATA
;
338 if (cb
->coding_mode
== 2) {
339 update_stats(cb
, dst
[n
]);
340 cb
->residue_parameter
= update_residue_parameter(cb
);
343 if (nb_channels
== 2 && ch
== 1) {
345 dst
[n
] += (unsigned)s
->decode_buffer
[0][OFFSET
+n
];
356 static int osq_decode_block(AVCodecContext
*avctx
, AVFrame
*frame
)
358 const int nb_channels
= avctx
->ch_layout
.nb_channels
;
359 const int nb_samples
= frame
->nb_samples
;
360 OSQContext
*s
= avctx
->priv_data
;
361 const unsigned factor
= s
->factor
;
362 int ret
, decorrelate
, downsample
;
363 GetBitContext
*gb
= &s
->gb
;
366 decorrelate
= get_bits1(gb
);
367 downsample
= get_bits1(gb
);
369 for (int ch
= 0; ch
< nb_channels
; ch
++) {
370 if ((ret
= osq_channel_parameters(avctx
, ch
)) < 0) {
371 av_log(avctx
, AV_LOG_ERROR
, "invalid channel parameters\n");
376 if ((ret
= do_decode(avctx
, frame
, decorrelate
, downsample
)) < 0)
381 switch (avctx
->sample_fmt
) {
382 case AV_SAMPLE_FMT_U8P
:
383 for (int ch
= 0; ch
< nb_channels
; ch
++) {
384 uint8_t *dst
= (uint8_t *)frame
->extended_data
[ch
];
385 int32_t *src
= s
->decode_buffer
[ch
] + OFFSET
;
387 for (int n
= 0; n
< nb_samples
; n
++)
388 dst
[n
] = av_clip_uint8(src
[n
] + 0x80);
391 case AV_SAMPLE_FMT_S16P
:
392 for (int ch
= 0; ch
< nb_channels
; ch
++) {
393 int16_t *dst
= (int16_t *)frame
->extended_data
[ch
];
394 int32_t *src
= s
->decode_buffer
[ch
] + OFFSET
;
396 for (int n
= 0; n
< nb_samples
; n
++)
397 dst
[n
] = (int16_t)src
[n
];
400 case AV_SAMPLE_FMT_S32P
:
401 for (int ch
= 0; ch
< nb_channels
; ch
++) {
402 int32_t *dst
= (int32_t *)frame
->extended_data
[ch
];
403 int32_t *src
= s
->decode_buffer
[ch
] + OFFSET
;
405 for (int n
= 0; n
< nb_samples
; n
++)
406 dst
[n
] = src
[n
] * factor
;
416 static int osq_receive_frame(AVCodecContext
*avctx
, AVFrame
*frame
)
418 OSQContext
*s
= avctx
->priv_data
;
419 GetBitContext
*gb
= &s
->gb
;
422 while (s
->bitstream_size
< s
->max_framesize
) {
426 ret
= ff_decode_get_packet(avctx
, s
->pkt
);
427 if (ret
== AVERROR_EOF
&& s
->bitstream_size
> 0)
429 if (ret
== AVERROR_EOF
|| ret
== AVERROR(EAGAIN
))
435 size
= FFMIN(s
->pkt
->size
- s
->pkt_offset
, s
->max_framesize
- s
->bitstream_size
);
436 memcpy(s
->bitstream
+ s
->bitstream_size
, s
->pkt
->data
+ s
->pkt_offset
, size
);
437 s
->bitstream_size
+= size
;
438 s
->pkt_offset
+= size
;
440 if (s
->pkt_offset
== s
->pkt
->size
) {
441 av_packet_unref(s
->pkt
);
446 frame
->nb_samples
= FFMIN(s
->frame_samples
, s
->nb_samples
);
447 if (frame
->nb_samples
<= 0)
450 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
453 if ((ret
= init_get_bits8(gb
, s
->bitstream
, s
->bitstream_size
)) < 0)
456 if ((ret
= osq_decode_block(avctx
, frame
)) < 0)
459 s
->nb_samples
-= frame
->nb_samples
;
461 n
= get_bits_count(gb
) / 8;
462 if (n
> s
->bitstream_size
) {
463 ret
= AVERROR_INVALIDDATA
;
467 memmove(s
->bitstream
, &s
->bitstream
[n
], s
->bitstream_size
- n
);
468 s
->bitstream_size
-= n
;
473 s
->bitstream_size
= 0;
475 av_packet_unref(s
->pkt
);
480 const FFCodec ff_osq_decoder
= {
482 CODEC_LONG_NAME("OSQ (Original Sound Quality)"),
483 .p
.type
= AVMEDIA_TYPE_AUDIO
,
484 .p
.id
= AV_CODEC_ID_OSQ
,
485 .priv_data_size
= sizeof(OSQContext
),
487 FF_CODEC_RECEIVE_FRAME_CB(osq_receive_frame
),
489 .p
.capabilities
= AV_CODEC_CAP_CHANNEL_CONF
|
491 .caps_internal
= FF_CODEC_CAP_INIT_CLEANUP
,
492 .p
.sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_U8P
,
495 AV_SAMPLE_FMT_NONE
},