avformat/nut: add support for per frame side & meta data with version 4

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
pull/53/head
Michael Niedermayer 11 years ago
parent d8e65e9224
commit 8113e838a8
  1. 1
      libavformat/nut.h
  2. 125
      libavformat/nutdec.c
  3. 148
      libavformat/nutenc.c

@ -46,6 +46,7 @@ typedef enum{
FLAG_SIZE_MSB = 32, ///<if set, data_size_msb is at frame header, otherwise data_size_msb is 0
FLAG_CHECKSUM = 64, ///<if set, the frame header contains a checksum
FLAG_RESERVED = 128, ///<if set, reserved_count is coded in the frame header
FLAG_SM_DATA = 256, ///<if set, side / meta data is stored in the frame header.
FLAG_HEADER_IDX =1024, ///<If set, header_idx is coded in the frame header.
FLAG_MATCH_TIME =2048, ///<If set, match_time_delta is coded in the frame header
FLAG_CODED =4096, ///<if set, coded_flags are stored in the frame header

@ -24,8 +24,10 @@
#include "libavutil/avassert.h"
#include "libavutil/bswap.h"
#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"
#include "libavutil/tree.h"
#include "libavcodec/bytestream.h"
#include "avio_internal.h"
#include "nut.h"
#include "riff.h"
@ -227,7 +229,7 @@ static int decode_main_header(NUTContext *nut)
end += avio_tell(bc);
tmp = ffio_read_varlen(bc);
if (tmp < 2 && tmp > NUT_VERSION) {
if (tmp < 2 && tmp > 4) {
av_log(s, AV_LOG_ERROR, "Version %"PRId64" not supported.\n",
tmp);
return AVERROR(ENOSYS);
@ -776,6 +778,116 @@ static int nut_read_header(AVFormatContext *s)
return 0;
}
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
{
int count = ffio_read_varlen(bc);
int skip_start = 0;
int skip_end = 0;
int channels = 0;
int64_t channel_layout = 0;
int sample_rate = 0;
int width = 0;
int height = 0;
int i;
for (i=0; i<count; i++) {
uint8_t name[256], str_value[256], type_str[256];
int value, type;
if (avio_tell(bc) >= maxpos)
return AVERROR_INVALIDDATA;
get_str(bc, name, sizeof(name));
type = value = get_s(bc);
if (value == -1) {
get_str(bc, str_value, sizeof(str_value));
av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
} else if (value == -2) {
uint8_t *dst = NULL;
int64_t v64, value_len;
get_str(bc, type_str, sizeof(type_str));
value_len = ffio_read_varlen(bc);
if (avio_tell(bc) + value_len >= maxpos)
return AVERROR_INVALIDDATA;
if (!strcmp(name, "Palette")) {
dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, value_len);
} else if (!strcmp(name, "Extradata")) {
dst = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, value_len);
} else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
dst = av_packet_new_side_data(pkt, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, value_len + 8);
if(!dst)
return AVERROR(ENOMEM);
AV_WB64(dst, v64);
dst += 8;
} else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
channel_layout = avio_rl64(bc);
continue;
} else {
av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
avio_skip(bc, value_len);
continue;
}
if(!dst)
return AVERROR(ENOMEM);
avio_read(bc, dst, value_len);
} else if (value == -3) {
value = get_s(bc);
} else if (value == -4) {
value = ffio_read_varlen(bc);
} else if (value < -4) {
get_s(bc);
} else {
if (!strcmp(name, "SkipStart")) {
skip_start = value;
} else if (!strcmp(name, "SkipEnd")) {
skip_end = value;
} else if (!strcmp(name, "Channels")) {
channels = value;
} else if (!strcmp(name, "SampleRate")) {
sample_rate = value;
} else if (!strcmp(name, "Width")) {
width = value;
} else if (!strcmp(name, "Height")) {
height = value;
} else {
av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
}
}
}
if (channels || channel_layout || sample_rate || width || height) {
uint8_t *dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PARAM_CHANGE, 28);
if (!dst)
return AVERROR(ENOMEM);
bytestream_put_le32(&dst,
AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT*(!!channels) +
AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT*(!!channel_layout) +
AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE*(!!sample_rate) +
AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS*(!!(width|height))
);
if (channels)
bytestream_put_le32(&dst, channels);
if (channel_layout)
bytestream_put_le64(&dst, channel_layout);
if (sample_rate)
bytestream_put_le32(&dst, sample_rate);
if (width || height){
bytestream_put_le32(&dst, width);
bytestream_put_le32(&dst, height);
}
}
if (skip_start || skip_end) {
uint8_t *dst = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
if (!dst)
return AVERROR(ENOMEM);
AV_WL32(dst, skip_start);
AV_WL32(dst+4, skip_end);
}
return 0;
}
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
uint8_t *header_idx, int frame_code)
{
@ -883,6 +995,17 @@ static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
return AVERROR(ENOMEM);
memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
pkt->pos = avio_tell(bc); // FIXME
if (stc->last_flags & FLAG_SM_DATA) {
int sm_size;
if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0)
return AVERROR_INVALIDDATA;
if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0)
return AVERROR_INVALIDDATA;
sm_size = avio_tell(bc) - pkt->pos;
size -= sm_size;
pkt->size -= sm_size;
}
avio_read(bc, pkt->data + nut->header_len[header_idx], size);
pkt->stream_index = stream_id;

@ -26,6 +26,7 @@
#include "libavutil/tree.h"
#include "libavutil/dict.h"
#include "libavutil/avassert.h"
#include "libavcodec/bytestream.h"
#include "libavcodec/mpegaudiodata.h"
#include "nut.h"
#include "internal.h"
@ -778,6 +779,8 @@ static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc,
flags |= FLAG_SIZE_MSB;
if (pkt->pts - nus->last_pts != fc->pts_delta)
flags |= FLAG_CODED_PTS;
if (pkt->side_data_elems && nut->version > 3)
flags |= FLAG_SM_DATA;
if (pkt->size > 2 * nut->max_distance)
flags |= FLAG_CHECKSUM;
if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
@ -810,11 +813,128 @@ static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
return best_i;
}
static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
{
AVStream *st = s->streams[pkt->stream_index];
int ret, i, dyn_size;
unsigned flags;
AVIOContext *dyn_bc;
int sm_data_count = 0;
uint8_t tmp[256];
uint8_t *dyn_buf;
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
for (i = 0; i<pkt->side_data_elems; i++) {
const uint8_t *data = pkt->side_data[i].data;
int size = pkt->side_data[i].size;
const uint8_t *data_end = data + size;
if (is_meta) {
if ( pkt->side_data[i].type == AV_PKT_DATA_METADATA_UPDATE
|| pkt->side_data[i].type == AV_PKT_DATA_STRINGS_METADATA) {
if (!size || data[size-1])
return AVERROR(EINVAL);
while (data < data_end) {
const uint8_t *key = data;
const uint8_t *val = data + strlen(key) + 1;
if(val >= data_end)
return AVERROR(EINVAL);
put_str(dyn_bc, key);
put_s(dyn_bc, -1);
put_str(dyn_bc, val);
data = val + strlen(val) + 1;
sm_data_count++;
}
}
} else {
switch (pkt->side_data[i].type) {
case AV_PKT_DATA_PALETTE:
case AV_PKT_DATA_NEW_EXTRADATA:
case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL:
default:
if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
put_str(dyn_bc, "Palette");
} else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) {
put_str(dyn_bc, "Extradata");
} else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data));
put_str(dyn_bc, tmp);
} else {
snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d",
(st->codec->flags & CODEC_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT,
pkt->side_data[i].type);
put_str(dyn_bc, tmp);
}
put_s(dyn_bc, -2);
put_str(dyn_bc, "bin");
ff_put_v(dyn_bc, pkt->side_data[i].size);
avio_write(dyn_bc, data, pkt->side_data[i].size);
sm_data_count++;
break;
case AV_PKT_DATA_PARAM_CHANGE:
flags = bytestream_get_le32(&data);
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
put_str(dyn_bc, "Channels");
put_s(dyn_bc, bytestream_get_le32(&data));
sm_data_count++;
}
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
put_str(dyn_bc, "ChannelLayout");
put_s(dyn_bc, -2);
put_str(dyn_bc, "u64");
ff_put_v(bc, 8);
avio_write(dyn_bc, data, 8); data+=8;
sm_data_count++;
}
if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
put_str(dyn_bc, "SampleRate");
put_s(dyn_bc, bytestream_get_le32(&data));
sm_data_count++;
}
if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
put_str(dyn_bc, "Width");
put_s(dyn_bc, bytestream_get_le32(&data));
put_str(dyn_bc, "Height");
put_s(dyn_bc, bytestream_get_le32(&data));
sm_data_count+=2;
}
case AV_PKT_DATA_SKIP_SAMPLES:
if (AV_RL32(data)) {
put_str(dyn_bc, "SkipStart");
put_s(dyn_bc, (unsigned)AV_RL32(data));
sm_data_count++;
}
if (AV_RL32(data+4)) {
put_str(dyn_bc, "SkipEnd");
put_s(dyn_bc, (unsigned)AV_RL32(data+4));
sm_data_count++;
}
break;
case AV_PKT_DATA_METADATA_UPDATE:
case AV_PKT_DATA_STRINGS_METADATA:
// belongs into meta, not side data
break;
}
}
}
ff_put_v(bc, sm_data_count);
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
avio_write(bc, dyn_buf, dyn_size);
av_freep(&dyn_buf);
return 0;
}
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
{
NUTContext *nut = s->priv_data;
StreamContext *nus = &nut->stream[pkt->stream_index];
AVIOContext *bc = s->pb, *dyn_bc;
AVIOContext *bc = s->pb, *dyn_bc, *sm_bc = NULL;
FrameCode *fc;
int64_t coded_pts;
int best_length, frame_code, flags, needed_flags, i, header_idx;
@ -822,6 +942,9 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
int store_sp = 0;
int ret;
int sm_size = 0;
int data_size = pkt->size;
uint8_t *sm_buf;
if (pkt->pts < 0) {
av_log(s, AV_LOG_ERROR,
@ -830,13 +953,23 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
return AVERROR(EINVAL);
}
if (pkt->side_data_elems && nut->version > 3) {
ret = avio_open_dyn_buf(&sm_bc);
if (ret < 0)
return ret;
write_sm_data(s, sm_bc, pkt, 0);
write_sm_data(s, sm_bc, pkt, 1);
sm_size = avio_close_dyn_buf(sm_bc, &sm_buf);
data_size += sm_size;
}
if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
write_headers(s, bc);
if (key_frame && !(nus->last_flags & FLAG_KEY))
store_sp = 1;
if (pkt->size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
store_sp = 1;
//FIXME: Ensure store_sp is 1 in the first place.
@ -916,10 +1049,10 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
if (flags & FLAG_STREAM_ID)
length += ff_get_v_length(pkt->stream_index);
if (pkt->size % fc->size_mul != fc->size_lsb)
if (data_size % fc->size_mul != fc->size_lsb)
continue;
if (flags & FLAG_SIZE_MSB)
length += ff_get_v_length(pkt->size / fc->size_mul);
length += ff_get_v_length(data_size / fc->size_mul);
if (flags & FLAG_CHECKSUM)
length += 4;
@ -961,13 +1094,18 @@ static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
}
if (flags & FLAG_STREAM_ID) ff_put_v(bc, pkt->stream_index);
if (flags & FLAG_CODED_PTS) ff_put_v(bc, coded_pts);
if (flags & FLAG_SIZE_MSB ) ff_put_v(bc, pkt->size / fc->size_mul);
if (flags & FLAG_SIZE_MSB ) ff_put_v(bc, data_size / fc->size_mul);
if (flags & FLAG_HEADER_IDX) ff_put_v(bc, header_idx = best_header_idx);
if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
else ffio_get_checksum(bc);
if (flags & FLAG_SM_DATA) {
avio_write(bc, sm_buf, sm_size);
av_freep(&sm_buf);
}
avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
nus->last_flags = flags;
nus->last_pts = pkt->pts;

Loading…
Cancel
Save