FFmpeg
Loading...
Searching...
No Matches
psymodel.c
Go to the documentation of this file.
1/*
2 * audio encoder psychoacoustic model
3 * Copyright (C) 2008 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include "avcodec.h"
23#include "psymodel.h"
24#include "libavutil/mem.h"
25
26extern const FFPsyModel ff_aac_psy_model;
27
29 const uint8_t **bands, const int* num_bands,
30 int num_groups, const uint8_t *group_map, int cutoff)
31{
32 int i, j, k = 0;
33
34 ctx->avctx = avctx;
35 ctx->ch = av_calloc(avctx->ch_layout.nb_channels, 2 * sizeof(ctx->ch[0]));
36 ctx->group = av_calloc(num_groups, sizeof(ctx->group[0]));
37 ctx->bands = av_memdup(bands, num_lens * sizeof(ctx->bands[0]));
38 ctx->num_bands = av_memdup(num_bands, num_lens * sizeof(ctx->num_bands[0]));
39 ctx->cutoff = cutoff ? cutoff : avctx->cutoff;
40
41 if (!ctx->ch || !ctx->group || !ctx->bands || !ctx->num_bands) {
43 return AVERROR(ENOMEM);
44 }
45
46 /* assign channels to groups (with virtual channels for coupling) */
47 for (i = 0; i < num_groups; i++) {
48 /* NOTE: Add 1 to handle the AAC chan_config without modification.
49 * This has the side effect of allowing an array of 0s to map
50 * to one channel per group.
51 */
52 ctx->group[i].num_ch = group_map[i] + 1;
53 for (j = 0; j < ctx->group[i].num_ch * 2; j++)
54 ctx->group[i].ch[j] = &ctx->ch[k++];
55 }
56
57 switch (ctx->avctx->codec_id) {
58 case AV_CODEC_ID_AAC:
59 ctx->model = &ff_aac_psy_model;
60 break;
61 }
62 if (ctx->model->init)
63 return ctx->model->init(ctx);
64 return 0;
65}
66
68{
69 int i = 0, ch = 0;
70
71 while (ch <= channel)
72 ch += ctx->group[i++].num_ch;
73
74 return &ctx->group[i-1];
75}
76
78{
79 if (ctx->model && ctx->model->end)
80 ctx->model->end(ctx);
81 av_freep(&ctx->bands);
82 av_freep(&ctx->num_bands);
83 av_freep(&ctx->group);
84 av_freep(&ctx->ch);
85}
const FFPsyModel ff_aac_psy_model
Definition aacpsy.c:1211
static const float bands[]
static AVFormatContext * ctx
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
#define AVERROR(e)
Definition error.h:45
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:302
#define av_cold
Definition attributes.h:117
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
FFPsyChannelGroup * ff_psy_find_group(FFPsyContext *ctx, int channel)
Determine what group a channel belongs to.
Definition psymodel.c:67
av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens, const uint8_t **bands, const int *num_bands, int num_groups, const uint8_t *group_map, int cutoff)
Initialize psychoacoustic model.
Definition psymodel.c:28
av_cold void ff_psy_end(FFPsyContext *ctx)
Cleanup model context at the end.
Definition psymodel.c:77
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition avcodec.h:1082
psychoacoustic information for an arbitrary group of channels
Definition psymodel.h:68
context used by psychoacoustic model
Definition psymodel.h:89
codec-specific psychoacoustic model implementation
Definition psymodel.h:116
#define av_freep(p)