avformat: add SMPTE 337M demuxer

This commit is contained in:
foo86 2017-07-01 17:06:27 +03:00 committed by Paul B Mahol
parent 930fe4b1f7
commit f04ef26816
6 changed files with 211 additions and 1 deletions

View File

@ -28,6 +28,7 @@ version <next>:
- support for decoding through D3D11VA in ffmpeg
- limiter video filter
- libvmaf video filter
- Dolby E decoder and SMPTE 337M demuxer
version 3.3:
- CrystalHD decoder moved to new decode API

View File

@ -520,6 +520,7 @@ library:
@tab Multimedia format used by many games.
@item SMJPEG @tab X @tab X
@tab Used in certain Loki game ports.
@item SMPTE 337M encapsulation @tab @tab X
@item Smush @tab @tab X
@tab Multimedia format used in some LucasArts games.
@item Sony OpenMG (OMA) @tab X @tab X

View File

@ -432,6 +432,7 @@ OBJS-$(CONFIG_RTSP_DEMUXER) += rtsp.o rtspdec.o httpauth.o \
urldecode.o
OBJS-$(CONFIG_RTSP_MUXER) += rtsp.o rtspenc.o httpauth.o \
urldecode.o
OBJS-$(CONFIG_S337M_DEMUXER) += s337m.o spdif.o
OBJS-$(CONFIG_SAMI_DEMUXER) += samidec.o subtitles.o
OBJS-$(CONFIG_SAP_DEMUXER) += sapdec.o
OBJS-$(CONFIG_SAP_MUXER) += sapenc.o

View File

@ -267,6 +267,7 @@ static void register_all(void)
REGISTER_MUXDEMUX(RTP, rtp);
REGISTER_MUXER (RTP_MPEGTS, rtp_mpegts);
REGISTER_MUXDEMUX(RTSP, rtsp);
REGISTER_DEMUXER (S337M, s337m);
REGISTER_DEMUXER (SAMI, sami);
REGISTER_MUXDEMUX(SAP, sap);
REGISTER_DEMUXER (SBG, sbg);

206
libavformat/s337m.c Normal file
View File

@ -0,0 +1,206 @@
/*
* 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 "libavutil/intreadwrite.h"
#include "avformat.h"
#include "spdif.h"
#define MARKER_16LE 0x72F81F4E
#define MARKER_20LE 0x20876FF0E154
#define MARKER_24LE 0x72F8961F4EA5
#define IS_16LE_MARKER(state) ((state & 0xFFFFFFFF) == MARKER_16LE)
#define IS_20LE_MARKER(state) ((state & 0xF0FFFFF0FFFF) == MARKER_20LE)
#define IS_24LE_MARKER(state) ((state & 0xFFFFFFFFFFFF) == MARKER_24LE)
#define IS_LE_MARKER(state) (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state))
static int s337m_get_offset_and_codec(AVFormatContext *s,
uint64_t state,
int data_type, int data_size,
int *offset, enum AVCodecID *codec)
{
int word_bits;
if (IS_16LE_MARKER(state)) {
word_bits = 16;
} else if (IS_20LE_MARKER(state)) {
data_type >>= 8;
data_size >>= 4;
word_bits = 20;
} else {
data_type >>= 8;
word_bits = 24;
}
if ((data_type & 0x1F) != 0x1C) {
if (s)
avpriv_report_missing_feature(s, "Data type %#x in SMPTE 337M", data_type & 0x1F);
return AVERROR_PATCHWELCOME;
}
if (codec)
*codec = AV_CODEC_ID_DOLBY_E;
switch (data_size / word_bits) {
case 3648:
*offset = 1920;
break;
case 3644:
*offset = 2002;
break;
case 3640:
*offset = 2000;
break;
case 3040:
*offset = 1601;
break;
default:
if (s)
avpriv_report_missing_feature(s, "Dolby E data size %d in SMPTE 337M", data_size);
return AVERROR_PATCHWELCOME;
}
*offset -= 4;
*offset *= (word_bits + 7 >> 3) * 2;
return 0;
}
static int s337m_probe(AVProbeData *p)
{
uint64_t state = 0;
int markers[3] = { 0 };
int i, sum, max, data_type, data_size, offset;
uint8_t *buf;
for (buf = p->buf; buf < p->buf + p->buf_size; buf++) {
state = (state << 8) | *buf;
if (!IS_LE_MARKER(state))
continue;
if (IS_16LE_MARKER(state)) {
data_type = AV_RL16(buf + 1);
data_size = AV_RL16(buf + 3);
buf += 4;
} else {
data_type = AV_RL24(buf + 1);
data_size = AV_RL24(buf + 4);
buf += 6;
}
if (s337m_get_offset_and_codec(NULL, state, data_type, data_size, &offset, NULL))
continue;
i = IS_16LE_MARKER(state) ? 0 : IS_20LE_MARKER(state) ? 1 : 2;
markers[i]++;
buf += offset;
state = 0;
}
sum = max = 0;
for (i = 0; i < FF_ARRAY_ELEMS(markers); i++) {
sum += markers[i];
if (markers[max] < markers[i])
max = i;
}
if (markers[max] > 3 && markers[max] * 4 > sum * 3)
return AVPROBE_SCORE_EXTENSION + 1;
return 0;
}
static int s337m_read_header(AVFormatContext *s)
{
s->ctx_flags |= AVFMTCTX_NOHEADER;
return 0;
}
static void bswap_buf24(uint8_t *data, int size)
{
int i;
for (i = 0; i < size / 3; i++, data += 3)
FFSWAP(uint8_t, data[0], data[2]);
}
static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
{
AVIOContext *pb = s->pb;
uint64_t state = 0;
int ret, data_type, data_size, offset;
enum AVCodecID codec;
int64_t pos;
while (!IS_LE_MARKER(state)) {
state = (state << 8) | avio_r8(pb);
if (avio_feof(pb))
return AVERROR_EOF;
}
if (IS_16LE_MARKER(state)) {
data_type = avio_rl16(pb);
data_size = avio_rl16(pb);
} else {
data_type = avio_rl24(pb);
data_size = avio_rl24(pb);
}
pos = avio_tell(pb);
if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0)
return ret;
if ((ret = av_new_packet(pkt, offset)) < 0)
return ret;
pkt->pos = pos;
if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
av_packet_unref(pkt);
return AVERROR_EOF;
}
if (IS_16LE_MARKER(state))
ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
else
bswap_buf24(pkt->data, pkt->size);
if (!s->nb_streams) {
AVStream *st = avformat_new_stream(s, NULL);
if (!st) {
av_packet_unref(pkt);
return AVERROR(ENOMEM);
}
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = codec;
}
return 0;
}
AVInputFormat ff_s337m_demuxer = {
.name = "s337m",
.long_name = NULL_IF_CONFIG_SMALL("SMPTE 337M"),
.read_probe = s337m_probe,
.read_header = s337m_read_header,
.read_packet = s337m_read_packet,
.flags = AVFMT_GENERIC_INDEX,
};

View File

@ -32,7 +32,7 @@
// Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
// Also please add any ticket numbers that you believe might be affected here
#define LIBAVFORMAT_VERSION_MAJOR 57
#define LIBAVFORMAT_VERSION_MINOR 75
#define LIBAVFORMAT_VERSION_MINOR 76
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \