Skip to content

Commit

Permalink
Announcing MP4 audio bitrates, adjustments for MPEG-2 Layer 3 (lower …
Browse files Browse the repository at this point in the history
…samplerates)
  • Loading branch information
wberube committed Jul 10, 2024
1 parent 07f7cb0 commit 06beff3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ _* At the moment, only text, 24-bit and 32-bit RGB overlays are handled, matrici

### Roadmap

- [ ] Alternative audio codecs
- [ ] Assorted WebUI to handle media reconfiguration and live preview
- [ ] Motion detection reimplementation
- [ ] Hardware support improvement (older SoCs, general usage chips)
- [ ] Alternative audio codecs


### Technical support and donations
Expand Down
3 changes: 2 additions & 1 deletion divinus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ http_post:

audio:
enable: false
srate: 8000
srate: 48000
bitrate: 128

rtsp:
enable: true
Expand Down
7 changes: 5 additions & 2 deletions src/media.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ unsigned int pcmSamp;
short pcmSrc[SHINE_MAX_SAMPLES];

void *aenc_thread(void) {
const uint32_t mp3FrmSize = 1152;
const uint32_t mp3FrmSize =
(app_config.audio_srate >= 32000 ? 144 : 72) *
(app_config.audio_bitrate * 1000) /
app_config.audio_srate;

while (keepRunning) {
pthread_mutex_lock(&aencMtx);
Expand Down Expand Up @@ -514,7 +517,7 @@ int enable_mp4(void) {

mp4_set_config(app_config.mp4_width, app_config.mp4_height, app_config.mp4_fps,
app_config.audio_enable ? HAL_AUDCODEC_MP3 : HAL_AUDCODEC_UNSPEC,
app_config.audio_srate);
app_config.audio_bitrate, app_config.audio_srate);
}

if (ret = bind_channel(index, app_config.mp4_fps, 0)) {
Expand Down
14 changes: 7 additions & 7 deletions src/mp4/moov.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum BufError write_vmhd(struct BitBuf *ptr);
enum BufError write_smhd(struct BitBuf *ptr);
enum BufError write_stbl(struct BitBuf *ptr, const struct MoovInfo *moov_info, char is_audio);
enum BufError write_stsd(struct BitBuf *ptr, const struct MoovInfo *moov_info, char is_audio);
enum BufError write_btrt(struct BitBuf *ptr);
enum BufError write_btrt(struct BitBuf *ptr, const struct MoovInfo *moov_info);
enum BufError write_esds(struct BitBuf *ptr, const struct MoovInfo *moov_info);
enum BufError write_mp4a(struct BitBuf *ptr, const struct MoovInfo *moov_info);
enum BufError write_avc1_hev1(struct BitBuf *ptr, const struct MoovInfo *moov_info);
Expand Down Expand Up @@ -541,7 +541,7 @@ enum BufError write_stsd(struct BitBuf *ptr, const struct MoovInfo *moov_info, c
return BUF_OK;
}

enum BufError write_btrt(struct BitBuf *ptr) {
enum BufError write_btrt(struct BitBuf *ptr, const struct MoovInfo *moov_info) {
enum BufError err;
uint32_t start_atom = ptr->offset;
err = put_u32_be(ptr, 0);
Expand All @@ -551,9 +551,9 @@ enum BufError write_btrt(struct BitBuf *ptr) {
chk_err;
err = put_u32_be(ptr, 0);
chk_err; // 4 Buffer size
err = put_u32_be(ptr, 25636);
err = put_u32_be(ptr, moov_info->audio_bitrate * 1000);
chk_err; // 4 Max bitrate
err = put_u32_be(ptr, 25636);
err = put_u32_be(ptr, moov_info->audio_bitrate * 1000);
chk_err; // 4 Avg bitrate
err = put_u32_be_to_offset(ptr, start_atom, ptr->offset - start_atom);
chk_err;
Expand Down Expand Up @@ -617,9 +617,9 @@ enum BufError write_DecoderConfig(struct BitBuf *ptr, const struct MoovInfo *moo
chk_err; // streamType
err = put_skip(ptr, 3);
chk_err; // 3 bufferSize
err = put_u32_be(ptr, 128000);
err = put_u32_be(ptr, moov_info->audio_bitrate * 1000);
chk_err; // 4 Max bitrate
err = put_u32_be(ptr, 128000);
err = put_u32_be(ptr, moov_info->audio_bitrate * 1000);
chk_err; // 4 Avg bitrate
err = put_u32_le_to_offset(ptr, var_len, varint32(ptr->offset - var_len - 4));
chk_err;
Expand Down Expand Up @@ -780,7 +780,7 @@ enum BufError write_mp4a(struct BitBuf *ptr, const struct MoovInfo *moov_info) {
chk_err; // 2 reserved
err = write_esds(ptr, moov_info);
chk_err;
err = write_btrt(ptr);
err = write_btrt(ptr, moov_info);
chk_err;
err = put_u32_be_to_offset(ptr, start_atom, ptr->offset - start_atom);
chk_err;
Expand Down
1 change: 1 addition & 0 deletions src/mp4/moov.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

struct MoovInfo {
char audio_codec;
unsigned short audio_bitrate;
unsigned int audio_samplerate;
char is_h265;
uint8_t profile_idc;
Expand Down
9 changes: 6 additions & 3 deletions src/mp4/mp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
uint32_t default_sample_size = 40000;

unsigned int aud_samplerate = 0;
unsigned short aud_bitrate = 0;
short vid_width = 1920, vid_height = 1080;
char aud_codec = 0, vid_framerate = 30;

Expand Down Expand Up @@ -58,12 +59,13 @@ enum BufError create_header(char is_h265) {
chk_err return BUF_OK;
}

void mp4_set_config(short width, short height, char framerate, char acodec, int srate)
{
void mp4_set_config(short width, short height, char framerate,
char acodec, unsigned short bitrate, unsigned int srate) {
vid_width = width;
vid_height = height;
vid_framerate = framerate;
aud_codec = acodec;
aud_bitrate = bitrate;
aud_samplerate = srate;
}

Expand Down Expand Up @@ -103,7 +105,8 @@ enum BufError mp4_set_slice(const char *nal_data, const uint32_t nal_len,
buf_moof.offset = 0;
err = write_moof(
&buf_moof, 0, 0, 0, default_sample_size, samples_info,
samples_info_len, samples_info + 1, buf_aud.offset >= 2304 ? 1 : 0);
samples_info_len, samples_info + 1,
buf_aud.offset >= 2304 ? 1 : 0);
chk_err;

buf_mdat.offset = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/mp4/mp4.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct Mp4State {
uint32_t nals_count;
};

void mp4_set_config(short width, short height, char framerate, char acodec, int srate);
void mp4_set_config(short width, short height, char framerate,
char acodec, unsigned short bitrate, unsigned int srate);

void mp4_set_sps(const char *nal_data, const uint32_t nal_len, char is_h265);
void mp4_set_pps(const char *nal_data, const uint32_t nal_len, char is_h265);
Expand Down

0 comments on commit 06beff3

Please sign in to comment.