Commit 81d070dd authored by James Almer's avatar James Almer

avcodec/dolby_e: split decoder and parser more thoroughly

Neither module should depend on the other.

Move shared functions to its own file for this purpose, and ensure
source files are compiled only when the required modules are enabled.
Signed-off-by: 's avatarJames Almer <jamrial@gmail.com>
parent 12c8aeb2
......@@ -40,7 +40,6 @@ OBJS = ac3_parser.o \
d3d11va.o \
decode.o \
dirac.o \
dolby_e_parser.o \
dv_profile.o \
encode.o \
imgconvert.o \
......@@ -286,7 +285,7 @@ OBJS-$(CONFIG_DIRAC_DECODER) += diracdec.o dirac.o diracdsp.o diractab
OBJS-$(CONFIG_DFA_DECODER) += dfa.o
OBJS-$(CONFIG_DNXHD_DECODER) += dnxhddec.o dnxhddata.o
OBJS-$(CONFIG_DNXHD_ENCODER) += dnxhdenc.o dnxhddata.o
OBJS-$(CONFIG_DOLBY_E_DECODER) += dolby_e.o kbdwin.o
OBJS-$(CONFIG_DOLBY_E_DECODER) += dolby_e.o dolby_e_parse.o kbdwin.o
OBJS-$(CONFIG_DPX_DECODER) += dpx.o
OBJS-$(CONFIG_DPX_ENCODER) += dpxenc.o
OBJS-$(CONFIG_DSD_LSBF_DECODER) += dsddec.o dsd.o
......@@ -1087,6 +1086,7 @@ OBJS-$(CONFIG_COOK_PARSER) += cook_parser.o
OBJS-$(CONFIG_DCA_PARSER) += dca_parser.o dca_exss.o dca.o
OBJS-$(CONFIG_DIRAC_PARSER) += dirac_parser.o
OBJS-$(CONFIG_DNXHD_PARSER) += dnxhd_parser.o dnxhddata.o
OBJS-$(CONFIG_DOLBY_E_PARSER) += dolby_e_parser.o dolby_e_parse.o
OBJS-$(CONFIG_DPX_PARSER) += dpx_parser.o
OBJS-$(CONFIG_DVAUDIO_PARSER) += dvaudio_parser.o
OBJS-$(CONFIG_DVBSUB_PARSER) += dvbsub_parser.o
......
This diff is collapsed.
......@@ -21,6 +21,7 @@
#ifndef AVCODEC_DOLBY_E_H
#define AVCODEC_DOLBY_E_H
#include <stdint.h>
#include "get_bits.h"
#define FRAME_SAMPLES 1792
......@@ -81,5 +82,23 @@ typedef struct DolbyEHeaderInfo {
static const uint16_t sample_rate_tab[16] = {
0, 42965, 43008, 44800, 53706, 53760
};
/**
* Initialize DBEContext.
* Set word_bits/word_bytes, input, input_size, key_present.
* @param[out] s DBEContext.
* @param[in] buf raw input buffer.
* @param[in] buf_size must be 3 bytes at least.
* @return Returns 0 on success, AVERROR_INVALIDDATA on error
*/
int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size);
/**
* Parse Dolby E metadata.
* Parse the header up to the end_gain element.
* @param[in] s DBEContext .
* @param[out] hdr Pointer to struct where header info is written.
* @return Returns 0 on success, AVERROR_INVALIDDATA on error
*/
int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr);
#endif
/*
* Copyright (C) 2017 foo86
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "get_bits.h"
#include "put_bits.h"
#include "dolby_e.h"
static const uint8_t nb_programs_tab[MAX_PROG_CONF + 1] = {
2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 8, 1, 2, 3, 3, 4, 5, 6, 1, 2, 3, 4, 1, 1
};
static const uint8_t nb_channels_tab[MAX_PROG_CONF + 1] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8
};
static int skip_input(DBEContext *s, int nb_words)
{
if (nb_words > s->input_size) {
return AVERROR_INVALIDDATA;
}
s->input += nb_words * s->word_bytes;
s->input_size -= nb_words;
return 0;
}
static int parse_key(DBEContext *s)
{
if (s->key_present) {
const uint8_t *key = s->input;
int ret = skip_input(s, 1);
if (ret < 0)
return ret;
return AV_RB24(key) >> 24 - s->word_bits;
}
return 0;
}
static int convert_input(DBEContext *s, int nb_words, int key)
{
const uint8_t *src = s->input;
uint8_t *dst = s->buffer;
PutBitContext pb;
int i;
av_assert0(nb_words <= 1024u);
if (nb_words > s->input_size) {
return AVERROR_INVALIDDATA;
}
switch (s->word_bits) {
case 16:
for (i = 0; i < nb_words; i++, src += 2, dst += 2)
AV_WB16(dst, AV_RB16(src) ^ key);
break;
case 20:
init_put_bits(&pb, s->buffer, sizeof(s->buffer));
for (i = 0; i < nb_words; i++, src += 3)
put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
flush_put_bits(&pb);
break;
case 24:
for (i = 0; i < nb_words; i++, src += 3, dst += 3)
AV_WB24(dst, AV_RB24(src) ^ key);
break;
default:
av_assert0(0);
}
return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
}
int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size)
{
int hdr;
if (buf_size < 3)
return AVERROR_INVALIDDATA;
hdr = AV_RB24(buf);
if ((hdr & 0xfffffe) == 0x7888e) {
s->word_bits = 24;
} else if ((hdr & 0xffffe0) == 0x788e0) {
s->word_bits = 20;
} else if ((hdr & 0xfffe00) == 0x78e00) {
s->word_bits = 16;
} else {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
return AVERROR_INVALIDDATA;
}
s->word_bytes = s->word_bits + 7 >> 3;
s->input = buf + s->word_bytes;
s->input_size = buf_size / s->word_bytes - 1;
s->key_present = hdr >> 24 - s->word_bits & 1;
return 0;
}
int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr)
{
int i, ret, key, mtd_size;
if ((key = parse_key(s)) < 0)
return key;
if ((ret = convert_input(s, 1, key)) < 0)
return ret;
skip_bits(&s->gb, 4);
mtd_size = get_bits(&s->gb, 10);
if (!mtd_size) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
return AVERROR_INVALIDDATA;
}
if ((ret = convert_input(s, mtd_size, key)) < 0)
return ret;
skip_bits(&s->gb, 14);
hdr->prog_conf = get_bits(&s->gb, 6);
if (hdr->prog_conf > MAX_PROG_CONF) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
return AVERROR_INVALIDDATA;
}
hdr->nb_channels = nb_channels_tab[hdr->prog_conf];
hdr->nb_programs = nb_programs_tab[hdr->prog_conf];
hdr->fr_code = get_bits(&s->gb, 4);
hdr->fr_code_orig = get_bits(&s->gb, 4);
if (!sample_rate_tab[hdr->fr_code] ||
!sample_rate_tab[hdr->fr_code_orig]) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
return AVERROR_INVALIDDATA;
}
skip_bits_long(&s->gb, 88);
for (i = 0; i < hdr->nb_channels; i++)
hdr->ch_size[i] = get_bits(&s->gb, 10);
hdr->mtd_ext_size = get_bits(&s->gb, 8);
hdr->meter_size = get_bits(&s->gb, 8);
skip_bits_long(&s->gb, 10 * hdr->nb_programs);
for (i = 0; i < hdr->nb_channels; i++) {
hdr->rev_id[i] = get_bits(&s->gb, 4);
skip_bits1(&s->gb);
hdr->begin_gain[i] = get_bits(&s->gb, 10);
hdr->end_gain[i] = get_bits(&s->gb, 10);
}
if (get_bits_left(&s->gb) < 0) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
return AVERROR_INVALIDDATA;
}
return skip_input(s, mtd_size + 1);
}
......@@ -20,164 +20,16 @@
#include "parser.h"
#if CONFIG_DOLBY_E_PARSER | CONFIG_DOLBY_E_DECODER
#include "dolby_e.h"
#include "get_bits.h"
#include "put_bits.h"
#include "dolby_e_parser.h"
#include "dolby_e_parser_internal.h"
static int skip_input(DBEContext *s, int nb_words)
{
if (nb_words > s->input_size) {
return AVERROR_INVALIDDATA;
}
s->input += nb_words * s->word_bytes;
s->input_size -= nb_words;
return 0;
}
static int parse_key(DBEContext *s)
{
if (s->key_present) {
const uint8_t *key = s->input;
int ret = skip_input(s, 1);
if (ret < 0)
return ret;
return AV_RB24(key) >> 24 - s->word_bits;
}
return 0;
}
static int convert_input(DBEContext *s, int nb_words, int key)
{
const uint8_t *src = s->input;
uint8_t *dst = s->buffer;
PutBitContext pb;
int i;
av_assert0(nb_words <= 1024u);
if (nb_words > s->input_size) {
return AVERROR_INVALIDDATA;
}
switch (s->word_bits) {
case 16:
for (i = 0; i < nb_words; i++, src += 2, dst += 2)
AV_WB16(dst, AV_RB16(src) ^ key);
break;
case 20:
init_put_bits(&pb, s->buffer, sizeof(s->buffer));
for (i = 0; i < nb_words; i++, src += 3)
put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
flush_put_bits(&pb);
break;
case 24:
for (i = 0; i < nb_words; i++, src += 3, dst += 3)
AV_WB24(dst, AV_RB24(src) ^ key);
break;
default:
av_assert0(0);
}
return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
}
int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size)
{
int hdr;
if (buf_size < 3)
return AVERROR_INVALIDDATA;
hdr = AV_RB24(buf);
if ((hdr & 0xfffffe) == 0x7888e) {
s->word_bits = 24;
} else if ((hdr & 0xffffe0) == 0x788e0) {
s->word_bits = 20;
} else if ((hdr & 0xfffe00) == 0x78e00) {
s->word_bits = 16;
} else {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
return AVERROR_INVALIDDATA;
}
s->word_bytes = s->word_bits + 7 >> 3;
s->input = buf + s->word_bytes;
s->input_size = buf_size / s->word_bytes - 1;
s->key_present = hdr >> 24 - s->word_bits & 1;
return 0;
}
int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr)
{
int i, ret, key, mtd_size;
if ((key = parse_key(s)) < 0)
return key;
if ((ret = convert_input(s, 1, key)) < 0)
return ret;
skip_bits(&s->gb, 4);
mtd_size = get_bits(&s->gb, 10);
if (!mtd_size) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
return AVERROR_INVALIDDATA;
}
if ((ret = convert_input(s, mtd_size, key)) < 0)
return ret;
skip_bits(&s->gb, 14);
hdr->prog_conf = get_bits(&s->gb, 6);
if (hdr->prog_conf > MAX_PROG_CONF) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
return AVERROR_INVALIDDATA;
}
hdr->nb_channels = nb_channels_tab[hdr->prog_conf];
hdr->nb_programs = nb_programs_tab[hdr->prog_conf];
hdr->fr_code = get_bits(&s->gb, 4);
hdr->fr_code_orig = get_bits(&s->gb, 4);
if (!sample_rate_tab[hdr->fr_code] ||
!sample_rate_tab[hdr->fr_code_orig]) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
return AVERROR_INVALIDDATA;
}
skip_bits_long(&s->gb, 88);
for (i = 0; i < hdr->nb_channels; i++)
hdr->ch_size[i] = get_bits(&s->gb, 10);
hdr->mtd_ext_size = get_bits(&s->gb, 8);
hdr->meter_size = get_bits(&s->gb, 8);
skip_bits_long(&s->gb, 10 * hdr->nb_programs);
for (i = 0; i < hdr->nb_channels; i++) {
hdr->rev_id[i] = get_bits(&s->gb, 4);
skip_bits1(&s->gb);
hdr->begin_gain[i] = get_bits(&s->gb, 10);
hdr->end_gain[i] = get_bits(&s->gb, 10);
}
if (get_bits_left(&s->gb) < 0) {
if (s->avctx)
av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
return AVERROR_INVALIDDATA;
}
return skip_input(s, mtd_size + 1);
}
#endif /* CONFIG_DOLBY_E_PARSER | CONFIG_DOLBY_E_DECODER */
typedef struct DBEParseContext {
ParseContext pc;
DBEContext dectx;
#if CONFIG_DOLBY_E_PARSER
DolbyEHeaderInfo metadata;
} DBEParseContext;
static int dolby_e_parse(AVCodecParserContext *s2, AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
......@@ -224,4 +76,3 @@ AVCodecParser ff_dolby_e_parser = {
.parser_parse = dolby_e_parse,
.parser_close = ff_parse_close,
};
#endif /* CONFIG_DOLBY_E_PARSER */
/*
* Copyright (C) 2017 foo86
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_DOLBY_E_PARSER_H
#define AVCODEC_DOLBY_E_PARSER_H
#include "dolby_e.h"
typedef struct DBEParseContext {
ParseContext pc;
DBEContext dectx;
DolbyEHeaderInfo metadata;
} DBEParseContext;
static const uint8_t nb_programs_tab[MAX_PROG_CONF + 1] = {
2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 8, 1, 2, 3, 3, 4, 5, 6, 1, 2, 3, 4, 1, 1
};
static const uint8_t nb_channels_tab[MAX_PROG_CONF + 1] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8
};
#endif
/*
* Dolby E parser internal code
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVCODEC_DOLBY_E_PARSER_INTERNAL_H
#define AVCODEC_DOLBY_E_PARSER_INTERNAL_H
#include "dolby_e.h"
/**
* Initialize DBEContext.
* Set word_bits/word_bytes, input, input_size, key_present.
* @param[out] s DBEContext.
* @param[in] buf raw input buffer.
* @param[in] buf_size must be 3 bytes at least.
* @return Returns 0 on success, AVERROR_INVALIDDATA on error
*/
int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size);
/**
* Parse Dolby E metadata.
* Parse the header up to the end_gain element.
* @param[in] s DBEContext .
* @param[out] hdr Pointer to struct where header info is written.
* @return Returns 0 on success, AVERROR_INVALIDDATA on error
*/
int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr);
#endif /* AVCODEC_DOLBY_E_PARSER_INTERNAL_H */
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment