FFmpeg
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
26 extern const FFPsyModel ff_aac_psy_model;
27 
28 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
29  const uint8_t **bands, const int* num_bands,
30  int num_groups, const uint8_t *group_map)
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 = avctx->cutoff;
40 
41  if (!ctx->ch || !ctx->group || !ctx->bands || !ctx->num_bands) {
42  ff_psy_end(ctx);
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 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
ff_psy_end
av_cold void ff_psy_end(FFPsyContext *ctx)
Cleanup model context at the end.
Definition: psymodel.c:77
ff_aac_psy_model
const FFPsyModel ff_aac_psy_model
Definition: aacpsy.c:1015
av_cold
#define av_cold
Definition: attributes.h:90
ctx
AVFormatContext * ctx
Definition: movenc.c:49
bands
static const float bands[]
Definition: af_superequalizer.c:56
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:451
ff_psy_init
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)
Initialize psychoacoustic model.
Definition: psymodel.c:28
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::cutoff
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:1064
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
avcodec.h
FFPsyChannelGroup
psychoacoustic information for an arbitrary group of channels
Definition: psymodel.h:68
AVCodecContext
main external API structure.
Definition: avcodec.h:431
FFPsyModel
codec-specific psychoacoustic model implementation
Definition: psymodel.h:114
ff_psy_find_group
FFPsyChannelGroup * ff_psy_find_group(FFPsyContext *ctx, int channel)
Determine what group a channel belongs to.
Definition: psymodel.c:67
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
FFPsyContext
context used by psychoacoustic model
Definition: psymodel.h:89
psymodel.h
channel
channel
Definition: ebur128.h:39