Commit 7b454ebb authored by James Almer's avatar James Almer

Add Length delimited bitstream (Annex B) demuxer

parent 0b6ee30e
Pipeline #3398 passed with stages
in 5 minutes and 10 seconds
......@@ -96,7 +96,7 @@ static void usage(const char *const app, const char *const reason, ...) {
fprintf(stderr, "Supported options:\n"
" --input/-i $file: input file\n"
" --output/-o $file: output file\n"
" --demuxer $name: force demuxer type (must be 'ivf'; default: detect from extension)\n"
" --demuxer $name: force demuxer type ('ivf' or 'annexb'; default: detect from extension)\n"
" --muxer $name: force muxer type ('md5', 'yuv', 'yuv4mpeg2' or 'null'; default: detect from extension)\n"
" --quiet/-q: disable status messages\n"
" --limit/-l $num: stop decoding after $num frames\n"
......
/*
* Copyright © 2018, VideoLAN and dav1d authors
* Copyright © 2018, Two Orioles, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "input/demuxer.h"
typedef struct DemuxerPriv {
FILE *f;
size_t temporal_unit_size;
size_t frame_unit_size;
} AnnexbInputContext;
static int leb128(AnnexbInputContext *const c, size_t *const len) {
unsigned more, i = 0;
int64_t sz = 0;
uint8_t byte;
do {
if (fread(&byte, 1, 1, c->f) < 1)
return -1;
more = byte & 0x80;
unsigned bits = byte & 0x7f;
if (i <= 3 || (i == 4 && bits < (1 << 4)))
sz |= (int64_t)bits << (i * 7);
else if (bits) return -1;
if (++i == 8 && more) return -1;
} while (more);
if (sz > 0xFFFFFFFFU)
return -1;
*len = sz;
return i;
}
static int annexb_open(AnnexbInputContext *const c, const char *const file,
unsigned fps[2], unsigned *const num_frames)
{
int res;
size_t len;
if (!(c->f = fopen(file, "rb"))) {
fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
return -1;
}
// TODO: Parse sequence header and read timing info if any.
fps[0] = 25;
fps[1] = 1;
for (*num_frames = 0;; (*num_frames)++) {
res = leb128(c, &len);
if (res < 0)
break;
fseek(c->f, len, SEEK_CUR);
}
fseek(c->f, 0, SEEK_SET);
return 0;
}
static int annexb_read(AnnexbInputContext *const c, Dav1dData *const data) {
size_t len;
int res;
if (!c->temporal_unit_size) {
res = leb128(c, &c->temporal_unit_size);
if (res < 0) return -1;
}
if (!c->frame_unit_size) {
res = leb128(c, &c->frame_unit_size);
if (res < 0) return -1;
c->temporal_unit_size -= res;
}
res = leb128(c, &len);
if (res < 0) return -1;
uint8_t *ptr = dav1d_data_create(data, len);
if (!ptr) return -1;
c->temporal_unit_size -= len + res;
c->frame_unit_size -= len + res;
if ((res = fread(ptr, len, 1, c->f)) != 1) {
fprintf(stderr, "Failed to read frame data: %s\n", strerror(errno));
dav1d_data_unref(data);
return -1;
}
return 0;
}
static void annexb_close(AnnexbInputContext *const c) {
fclose(c->f);
}
const Demuxer annexb_demuxer = {
.priv_data_size = sizeof(AnnexbInputContext),
.name = "annexb",
.extension = "obu",
.open = annexb_open,
.read = annexb_read,
.close = annexb_close,
};
......@@ -41,7 +41,7 @@ struct DemuxerContext {
const Demuxer *impl;
};
#define MAX_NUM_DEMUXERS 1
#define MAX_NUM_DEMUXERS 2
static const Demuxer *demuxers[MAX_NUM_DEMUXERS];
static int num_demuxers = 0;
......@@ -53,6 +53,7 @@ static int num_demuxers = 0;
void init_demuxers(void) {
register_demuxer(ivf_demuxer);
register_demuxer(annexb_demuxer);
}
static const char *find_extension(const char *const f) {
......
......@@ -36,6 +36,7 @@ dav1d_sources = files(
'dav1d.c',
'dav1d_cli_parse.c',
'input/input.c',
'input/annexb.c',
'input/ivf.c',
'output/md5.c',
'output/null.c',
......
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