// SPDX-License-Identifier: GPL-2.0
/*
* Vidtv serves as a reference DVB driver and helps validate the existing APIs
* in the media subsystem. It can also aid developers working on userspace
* applications.
*
* This file contains the code for an AES3 (also known as AES/EBU) encoder.
* It is based on EBU Tech 3250 and SMPTE 302M technical documents.
*
* This encoder currently supports 16bit AES3 subframes using 16bit signed
* integers.
*
* Note: AU stands for Access Unit, and AAU stands for Audio Access Unit
*
* Copyright (C) 2020 Daniel W. S. Almeida
*/
#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
#include <linux/bug.h>
#include <linux/crc32.h>
#include <linux/fixp-arith.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/math64.h>
#include <linux/printk.h>
#include <linux/ratelimit.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/vmalloc.h>
#include "vidtv_common.h"
#include "vidtv_encoder.h"
#include "vidtv_s302m.h"
#define S302M_SAMPLING_RATE_HZ 48000
#define PES_PRIVATE_STREAM_1 0 xbd /* PES: private_stream_1 */
#define S302M_BLOCK_SZ 192
#define S302M_SIN_LUT_NUM_ELEM 1024
/* these are retrieved empirically from ffmpeg/libavcodec */
#define FF_S302M_DEFAULT_NUM_FRAMES 1115
#define FF_S302M_DEFAULT_PTS_INCREMENT 2090
#define FF_S302M_DEFAULT_PTS_OFFSET 100000
/* Used by the tone generator: number of samples for PI */
#define PI 180
static const u8 reverse[256 ] = {
/* from ffmpeg */
0 x00, 0 x80, 0 x40, 0 xC0, 0 x20, 0 xA0, 0 x60, 0 xE0, 0 x10, 0 x90, 0 x50, 0 xD0,
0 x30, 0 xB0, 0 x70, 0 xF0, 0 x08, 0 x88, 0 x48, 0 xC8, 0 x28, 0 xA8, 0 x68, 0 xE8,
0 x18, 0 x98, 0 x58, 0 xD8, 0 x38, 0 xB8, 0 x78, 0 xF8, 0 x04, 0 x84, 0 x44, 0 xC4,
0 x24, 0 xA4, 0 x64, 0 xE4, 0 x14, 0 x94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 x23, 0 xA3, 0 x63, 0 xE3, 0 x13, 0 x93, 0 x53, 0 xD3,
0 x33, 0 xB3, 0 x73, 0 xF3, 0 x0B, 0 x8B, 0 x4B, 0 xCB, 0 x2B, 0 xAB, 0 x6B, 0 xEB,
0 x1B, 0 x9B, 0 x5B, 0 xDB, 0 x3B, 0 xBB, 0 x7B, 0 xFB, 0 x07, 0 x87, 0 x47, 0 xC7,
0 x27, 0 xA7, 0 x67, 0 xE7, 0 x17, 0 x97, 0 x57, 0 xD7, 0 x37, 0 xB7, 0 x77, 0 xF7,
0 x0F, 0 x8F, 0 x4F, 0 xCF, 0 x2F, 0 xAF, 0 x6F, 0 xEF, 0 x1F, 0 x9F, 0 x5F, 0 xDF,
0 x3F, 0 xBF, 0 x7F, 0 xFF,
};
struct tone_duration {
enum musical_notes note;
int duration;
};
#define COMPASS 100 /* beats per minute */
static const struct tone_duration beethoven_fur_elise[] = {
{ NOTE_SILENT, 512 },
{ NOTE_E_6, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_6, 128 }, { NOTE_E_6, 128 }, { NOTE_B_5, 128 },
{ NOTE_D_6, 128 }, { NOTE_C_6, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_C_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_A_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_GS_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_GS_5, 128 }, { NOTE_B_5, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_E_6, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_6, 128 }, { NOTE_E_6, 128 }, { NOTE_B_5, 128 },
{ NOTE_D_6, 128 }, { NOTE_C_6, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_C_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_A_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_GS_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_C_6, 128 }, { NOTE_B_5, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_SILENT, 128 },
{ NOTE_E_6, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_6, 128 }, { NOTE_E_6, 128 }, { NOTE_B_5, 128 },
{ NOTE_D_6, 128 }, { NOTE_C_6, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_C_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_A_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_GS_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_GS_5, 128 }, { NOTE_B_5, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_E_6, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_6, 128 }, { NOTE_E_6, 128 }, { NOTE_B_5, 128 },
{ NOTE_D_6, 128 }, { NOTE_C_6, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_C_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_A_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_GS_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_C_6, 128 }, { NOTE_B_5, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_B_4, 128 },
{ NOTE_C_5, 128 }, { NOTE_D_5, 128 }, { NOTE_C_4, 128 },
{ NOTE_G_4, 128 }, { NOTE_C_5, 128 }, { NOTE_G_4, 128 },
{ NOTE_F_5, 128 }, { NOTE_E_5, 128 }, { NOTE_G_3, 128 },
{ NOTE_G_4, 128 }, { NOTE_B_3, 128 }, { NOTE_F_4, 128 },
{ NOTE_E_5, 128 }, { NOTE_D_5, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_E_4, 128 },
{ NOTE_D_5, 128 }, { NOTE_C_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_E_5, 128 }, { NOTE_E_5, 128 },
{ NOTE_E_6, 128 }, { NOTE_E_5, 128 }, { NOTE_E_6, 128 },
{ NOTE_E_5, 128 }, { NOTE_E_5, 128 }, { NOTE_DS_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_5, 128 }, { NOTE_E_5, 128 }, { NOTE_DS_6, 128 },
{ NOTE_E_6, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_6, 128 }, { NOTE_E_6, 128 }, { NOTE_B_5, 128 },
{ NOTE_D_6, 128 }, { NOTE_C_6, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_C_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_A_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_GS_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_GS_5, 128 }, { NOTE_B_5, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_E_6, 128 }, { NOTE_DS_6, 128 }, { NOTE_E_6, 128 },
{ NOTE_DS_6, 128 }, { NOTE_E_6, 128 }, { NOTE_B_5, 128 },
{ NOTE_D_6, 128 }, { NOTE_C_6, 128 }, { NOTE_A_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_A_4, 128 }, { NOTE_C_5, 128 },
{ NOTE_E_5, 128 }, { NOTE_A_5, 128 }, { NOTE_E_3, 128 },
{ NOTE_E_4, 128 }, { NOTE_GS_4, 128 }, { NOTE_E_5, 128 },
{ NOTE_C_6, 128 }, { NOTE_B_5, 128 }, { NOTE_A_5, 512 },
{ NOTE_SILENT, 256 },
};
static struct vidtv_access_unit *vidtv_s302m_access_unit_init(struct vidtv_access_unit *head)
{
struct vidtv_access_unit *au;
au = kzalloc(sizeof (*au), GFP_KERNEL);
if (!au)
return NULL;
if (head) {
while (head->next)
head = head->next;
head->next = au;
}
return au;
}
static void vidtv_s302m_access_unit_destroy(struct vidtv_encoder *e)
{
struct vidtv_access_unit *head = e->access_units;
struct vidtv_access_unit *tmp = NULL;
while (head) {
tmp = head;
head = head->next;
kfree(tmp);
}
e->access_units = NULL;
}
static void vidtv_s302m_alloc_au(struct vidtv_encoder *e)
{
struct vidtv_access_unit *sync_au = NULL;
struct vidtv_access_unit *temp = NULL;
if (e->sync && e->sync->is_video_encoder) {
sync_au = e->sync->access_units;
while (sync_au) {
temp = vidtv_s302m_access_unit_init(e->access_units);
if (!e->access_units)
e->access_units = temp;
sync_au = sync_au->next;
}
return ;
}
e->access_units = vidtv_s302m_access_unit_init(NULL);
}
static void
vidtv_s302m_compute_sample_count_from_video(struct vidtv_encoder *e)
{
struct vidtv_access_unit *sync_au = e->sync->access_units;
struct vidtv_access_unit *au = e->access_units;
u32 sample_duration_usecs;
u32 vau_duration_usecs;
u32 s;
vau_duration_usecs = USEC_PER_SEC / e->sync->sampling_rate_hz;
sample_duration_usecs = USEC_PER_SEC / e->sampling_rate_hz;
while (au && sync_au) {
s = DIV_ROUND_UP(vau_duration_usecs, sample_duration_usecs);
au->num_samples = s;
au = au->next;
sync_au = sync_au->next;
}
}
static void vidtv_s302m_compute_pts_from_video(struct vidtv_encoder *e)
{
struct vidtv_access_unit *au = e->access_units;
struct vidtv_access_unit *sync_au = e->sync->access_units;
/* use the same pts from the video access unit*/
while (au && sync_au) {
au->pts = sync_au->pts;
au = au->next;
sync_au = sync_au->next;
}
}
static u16 vidtv_s302m_get_sample(struct vidtv_encoder *e)
{
u16 sample;
int pos;
struct vidtv_s302m_ctx *ctx = e->ctx;
if (!e->src_buf) {
/*
* Simple tone generator: play the tones at the
* beethoven_fur_elise array.
*/
if (ctx->last_duration <= 0 ) {
if (e->src_buf_offset >= ARRAY_SIZE(beethoven_fur_elise))
e->src_buf_offset = 0 ;
ctx->last_tone = beethoven_fur_elise[e->src_buf_offset].note;
ctx->last_duration = beethoven_fur_elise[e->src_buf_offset].duration *
S302M_SAMPLING_RATE_HZ / COMPASS / 5 ;
e->src_buf_offset++;
ctx->note_offset = 0 ;
} else {
ctx->last_duration--;
}
/* Handle pause notes */
if (!ctx->last_tone)
return 0 x8000;
pos = (2 * PI * ctx->note_offset * ctx->last_tone) / S302M_SAMPLING_RATE_HZ;
ctx->note_offset++;
return (fixp_sin32(pos % (2 * PI)) >> 16 ) + 0 x8000;
}
/* bug somewhere */
if (e->src_buf_offset > e->src_buf_sz) {
pr_err_ratelimited("overflow detected: %d > %d, wrapping.\n" ,
e->src_buf_offset,
e->src_buf_sz);
e->src_buf_offset = 0 ;
}
if (e->src_buf_offset >= e->src_buf_sz) {
/* let the source know we are out of data */
if (e->last_sample_cb)
e->last_sample_cb(e->sample_count);
e->src_buf_offset = 0 ;
}
sample = *(u16 *)(e->src_buf + e->src_buf_offset);
return sample;
}
static u32 vidtv_s302m_write_frame(struct vidtv_encoder *e,
u16 sample)
{
struct vidtv_s302m_ctx *ctx = e->ctx;
struct vidtv_s302m_frame_16 f = {};
u32 nbytes = 0 ;
/* from ffmpeg: see s302enc.c */
u8 vucf = ctx->frame_index == 0 ? 0 x10 : 0 ;
f.data[0 ] = sample & 0 xFF;
f.data[1 ] = (sample & 0 xFF00) >> 8 ;
f.data[2 ] = ((sample & 0 x0F) << 4 ) | vucf;
f.data[3 ] = (sample & 0 x0FF0) >> 4 ;
f.data[4 ] = (sample & 0 xF000) >> 12 ;
f.data[0 ] = reverse[f.data[0 ]];
f.data[1 ] = reverse[f.data[1 ]];
f.data[2 ] = reverse[f.data[2 ]];
f.data[3 ] = reverse[f.data[3 ]];
f.data[4 ] = reverse[f.data[4 ]];
nbytes += vidtv_memcpy(e->encoder_buf,
e->encoder_buf_offset,
VIDTV_S302M_BUF_SZ,
&f,
sizeof (f));
e->encoder_buf_offset += nbytes;
ctx->frame_index++;
if (ctx->frame_index >= S302M_BLOCK_SZ)
ctx->frame_index = 0 ;
return nbytes;
}
static u32 vidtv_s302m_write_h(struct vidtv_encoder *e, u32 p_sz)
{
struct vidtv_smpte_s302m_es h = {};
u32 nbytes = 0 ;
/* 2 channels, ident: 0, 16 bits per sample */
h.bitfield = cpu_to_be32((p_sz << 16 ));
nbytes += vidtv_memcpy(e->encoder_buf,
e->encoder_buf_offset,
e->encoder_buf_sz,
&h,
sizeof (h));
e->encoder_buf_offset += nbytes;
return nbytes;
}
static void vidtv_s302m_write_frames(struct vidtv_encoder *e)
{
struct vidtv_access_unit *au = e->access_units;
struct vidtv_s302m_ctx *ctx = e->ctx;
u32 nbytes_per_unit = 0 ;
u32 nbytes = 0 ;
u32 au_sz = 0 ;
u16 sample;
u32 j;
while (au) {
au_sz = au->num_samples *
sizeof (struct vidtv_s302m_frame_16);
nbytes_per_unit = vidtv_s302m_write_h(e, au_sz);
for (j = 0 ; j < au->num_samples; ++j) {
sample = vidtv_s302m_get_sample(e);
nbytes_per_unit += vidtv_s302m_write_frame(e, sample);
if (e->src_buf)
e->src_buf_offset += sizeof (u16);
e->sample_count++;
}
au->nbytes = nbytes_per_unit;
if (au_sz + sizeof (struct vidtv_smpte_s302m_es) != nbytes_per_unit) {
pr_warn_ratelimited("write size was %u, expected %zu\n" ,
nbytes_per_unit,
au_sz + sizeof (struct vidtv_smpte_s302m_es));
}
nbytes += nbytes_per_unit;
au->offset = nbytes - nbytes_per_unit;
nbytes_per_unit = 0 ;
ctx->au_count++;
au = au->next;
}
}
static void *vidtv_s302m_encode(struct vidtv_encoder *e)
{
struct vidtv_s302m_ctx *ctx = e->ctx;
/*
* According to SMPTE 302M, an audio access unit is specified as those
* AES3 words that are associated with a corresponding video frame.
* Therefore, there is one audio access unit for every video access unit
* in the corresponding video encoder ('sync'), using the same values
* for PTS as used by the video encoder.
*
* Assuming that it is also possible to send audio without any
* associated video, as in a radio-like service, a single audio access unit
* is created with values for 'num_samples' and 'pts' taken empirically from
* ffmpeg
*/
vidtv_s302m_access_unit_destroy(e);
vidtv_s302m_alloc_au(e);
if (e->sync && e->sync->is_video_encoder) {
vidtv_s302m_compute_sample_count_from_video(e);
vidtv_s302m_compute_pts_from_video(e);
} else {
e->access_units->num_samples = FF_S302M_DEFAULT_NUM_FRAMES;
e->access_units->pts = (ctx->au_count * FF_S302M_DEFAULT_PTS_INCREMENT) +
FF_S302M_DEFAULT_PTS_OFFSET;
}
vidtv_s302m_write_frames(e);
return e->encoder_buf;
}
static u32 vidtv_s302m_clear(struct vidtv_encoder *e)
{
struct vidtv_access_unit *au = e->access_units;
u32 count = 0 ;
while (au) {
count++;
au = au->next;
}
vidtv_s302m_access_unit_destroy(e);
memset(e->encoder_buf, 0 , VIDTV_S302M_BUF_SZ);
e->encoder_buf_offset = 0 ;
return count;
}
struct vidtv_encoder
*vidtv_s302m_encoder_init(struct vidtv_s302m_encoder_init_args args)
{
u32 priv_sz = sizeof (struct vidtv_s302m_ctx);
struct vidtv_s302m_ctx *ctx;
struct vidtv_encoder *e;
e = kzalloc(sizeof (*e), GFP_KERNEL);
if (!e)
return NULL;
e->id = S302M;
if (args.name)
e->name = kstrdup(args.name, GFP_KERNEL);
e->encoder_buf = vzalloc(VIDTV_S302M_BUF_SZ);
if (!e->encoder_buf)
goto out_kfree_e;
e->encoder_buf_sz = VIDTV_S302M_BUF_SZ;
e->encoder_buf_offset = 0 ;
e->sample_count = 0 ;
e->src_buf = (args.src_buf) ? args.src_buf : NULL;
e->src_buf_sz = (args.src_buf) ? args.src_buf_sz : 0 ;
e->src_buf_offset = 0 ;
e->is_video_encoder = false ;
ctx = kzalloc(priv_sz, GFP_KERNEL);
if (!ctx)
goto out_kfree_buf;
e->ctx = ctx;
ctx->last_duration = 0 ;
e->encode = vidtv_s302m_encode;
e->clear = vidtv_s302m_clear;
e->es_pid = cpu_to_be16(args.es_pid);
e->stream_id = cpu_to_be16(PES_PRIVATE_STREAM_1);
e->sync = args.sync;
e->sampling_rate_hz = S302M_SAMPLING_RATE_HZ;
e->last_sample_cb = args.last_sample_cb;
e->destroy = vidtv_s302m_encoder_destroy;
if (args.head) {
while (args.head->next)
args.head = args.head->next;
args.head->next = e;
}
e->next = NULL;
return e;
out_kfree_buf:
vfree(e->encoder_buf);
out_kfree_e:
kfree(e->name);
kfree(e);
return NULL;
}
void vidtv_s302m_encoder_destroy(struct vidtv_encoder *e)
{
if (e->id != S302M) {
pr_err_ratelimited("Encoder type mismatch, skipping.\n" );
return ;
}
vidtv_s302m_access_unit_destroy(e);
kfree(e->name);
vfree(e->encoder_buf);
kfree(e->ctx);
kfree(e);
}
Messung V0.5 in Prozent C=93 H=91 G=91
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-05)
¤
*© Formatika GbR, Deutschland