#include "libavutil/avstring.h"
#include "libavutil/bswap.h"
#include "libavutil/tree.h"
+#include "avio_internal.h"
#include "nut.h"
#undef NDEBUG
#include <assert.h>
-#if FF_API_MAX_STREAMS
-#define NUT_MAX_STREAMS MAX_STREAMS
-#else
#define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */
-#endif
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen){
- unsigned int len= ff_get_v(bc);
+ unsigned int len= ffio_read_varlen(bc);
if(len && maxlen)
- get_buffer(bc, string, FFMIN(len, maxlen));
+ avio_read(bc, string, FFMIN(len, maxlen));
while(len > maxlen){
- get_byte(bc);
+ avio_r8(bc);
len--;
}
}
static int64_t get_s(AVIOContext *bc){
- int64_t v = ff_get_v(bc) + 1;
+ int64_t v = ffio_read_varlen(bc) + 1;
if (v&1) return -(v>>1);
else return (v>>1);
}
static uint64_t get_fourcc(AVIOContext *bc){
- unsigned int len= ff_get_v(bc);
+ unsigned int len= ffio_read_varlen(bc);
- if (len==2) return get_le16(bc);
- else if(len==4) return get_le32(bc);
+ if (len==2) return avio_rl16(bc);
+ else if(len==4) return avio_rl32(bc);
else return -1;
}
#ifdef TRACE
static inline uint64_t get_v_trace(AVIOContext *bc, char *file, char *func, int line){
- uint64_t v= ff_get_v(bc);
+ uint64_t v= ffio_read_varlen(bc);
av_log(NULL, AV_LOG_DEBUG, "get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
return v;
av_log(NULL, AV_LOG_DEBUG, "get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
return v;
}
-#define ff_get_v(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
+#define ffio_read_varlen(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
#define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
#define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
#endif
static int get_packetheader(NUTContext *nut, AVIOContext *bc, int calculate_checksum, uint64_t startcode)
{
int64_t size;
-// start= url_ftell(bc) - 8;
+// start= avio_tell(bc) - 8;
startcode= av_be2ne64(startcode);
startcode= ff_crc04C11DB7_update(0, (uint8_t*)&startcode, 8);
- init_checksum(bc, ff_crc04C11DB7_update, startcode);
- size= ff_get_v(bc);
+ ffio_init_checksum(bc, ff_crc04C11DB7_update, startcode);
+ size= ffio_read_varlen(bc);
if(size > 4096)
- get_be32(bc);
- if(get_checksum(bc) && size > 4096)
+ avio_rb32(bc);
+ if(ffio_get_checksum(bc) && size > 4096)
return -1;
- init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
+ ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
return size;
}
uint64_t state=0;
if(pos >= 0)
- url_fseek(bc, pos, SEEK_SET); //note, this may fail if the stream is not seekable, but that should not matter, as in this case we simply start where we currently are
+ avio_seek(bc, pos, SEEK_SET); //note, this may fail if the stream is not seekable, but that should not matter, as in this case we simply start where we currently are
while(!url_feof(bc)){
- state= (state<<8) | get_byte(bc);
+ state= (state<<8) | avio_r8(bc);
if((state>>56) != 'N')
continue;
switch(state){
for(;;){
uint64_t startcode= find_any_startcode(bc, pos);
if(startcode == code)
- return url_ftell(bc) - 8;
+ return avio_tell(bc) - 8;
else if(startcode == 0)
return -1;
pos=-1;
}
#define GET_V(dst, check) \
- tmp= ff_get_v(bc);\
+ tmp= ffio_read_varlen(bc);\
if(!(check)){\
av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\
return -1;\
dst= tmp;
static int skip_reserved(AVIOContext *bc, int64_t pos){
- pos -= url_ftell(bc);
+ pos -= avio_tell(bc);
if(pos<0){
- url_fseek(bc, pos, SEEK_CUR);
+ avio_seek(bc, pos, SEEK_CUR);
return -1;
}else{
while(pos--)
- get_byte(bc);
+ avio_r8(bc);
return 0;
}
}
int64_t tmp_match;
end= get_packetheader(nut, bc, 1, MAIN_STARTCODE);
- end += url_ftell(bc);
+ end += avio_tell(bc);
GET_V(tmp , tmp >=2 && tmp <= 3)
GET_V(stream_count , tmp > 0 && tmp <= NUT_MAX_STREAMS)
- nut->max_distance = ff_get_v(bc);
+ nut->max_distance = ffio_read_varlen(bc);
if(nut->max_distance > 65536){
av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
nut->max_distance= 65536;
tmp_match= 1-(1LL<<62);
tmp_head_idx= 0;
for(i=0; i<256;){
- int tmp_flags = ff_get_v(bc);
- int tmp_fields= ff_get_v(bc);
+ int tmp_flags = ffio_read_varlen(bc);
+ int tmp_fields= ffio_read_varlen(bc);
if(tmp_fields>0) tmp_pts = get_s(bc);
- if(tmp_fields>1) tmp_mul = ff_get_v(bc);
- if(tmp_fields>2) tmp_stream= ff_get_v(bc);
- if(tmp_fields>3) tmp_size = ff_get_v(bc);
+ if(tmp_fields>1) tmp_mul = ffio_read_varlen(bc);
+ if(tmp_fields>2) tmp_stream= ffio_read_varlen(bc);
+ if(tmp_fields>3) tmp_size = ffio_read_varlen(bc);
else tmp_size = 0;
- if(tmp_fields>4) tmp_res = ff_get_v(bc);
+ if(tmp_fields>4) tmp_res = ffio_read_varlen(bc);
else tmp_res = 0;
- if(tmp_fields>5) count = ff_get_v(bc);
+ if(tmp_fields>5) count = ffio_read_varlen(bc);
else count = tmp_mul - tmp_size;
if(tmp_fields>6) tmp_match = get_s(bc);
- if(tmp_fields>7) tmp_head_idx= ff_get_v(bc);
+ if(tmp_fields>7) tmp_head_idx= ffio_read_varlen(bc);
while(tmp_fields-- > 8)
- ff_get_v(bc);
+ ffio_read_varlen(bc);
if(count == 0 || i+count > 256){
av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
}
assert(nut->frame_code['N'].flags == FLAG_INVALID);
- if(end > url_ftell(bc) + 4){
+ if(end > avio_tell(bc) + 4){
int rem= 1024;
GET_V(nut->header_count, tmp<128U)
nut->header_count++;
return AVERROR_INVALIDDATA;
}
nut->header[i]= av_malloc(nut->header_len[i]);
- get_buffer(bc, nut->header[i], nut->header_len[i]);
+ avio_read(bc, nut->header[i], nut->header_len[i]);
}
assert(nut->header_len[0]==0);
}
- if(skip_reserved(bc, end) || get_checksum(bc)){
+ if(skip_reserved(bc, end) || ffio_get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
return AVERROR_INVALIDDATA;
}
AVStream *st;
end= get_packetheader(nut, bc, 1, STREAM_STARTCODE);
- end += url_ftell(bc);
+ end += avio_tell(bc);
GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
stc= &nut->stream[stream_id];
if (!st)
return AVERROR(ENOMEM);
- class = ff_get_v(bc);
+ class = ffio_read_varlen(bc);
tmp = get_fourcc(bc);
st->codec->codec_tag= tmp;
switch(class)
GET_V(stc->time_base_id , tmp < nut->time_base_count);
GET_V(stc->msb_pts_shift , tmp < 16);
- stc->max_pts_distance= ff_get_v(bc);
+ stc->max_pts_distance= ffio_read_varlen(bc);
GET_V(stc->decode_delay , tmp < 1000); //sanity limit, raise this if Moore's law is true
st->codec->has_b_frames= stc->decode_delay;
- ff_get_v(bc); //stream flags
+ ffio_read_varlen(bc); //stream flags
GET_V(st->codec->extradata_size, tmp < (1<<30));
if(st->codec->extradata_size){
st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
- get_buffer(bc, st->codec->extradata, st->codec->extradata_size);
+ avio_read(bc, st->codec->extradata, st->codec->extradata_size);
}
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
GET_V(st->codec->width , tmp > 0)
GET_V(st->codec->height, tmp > 0)
- st->sample_aspect_ratio.num= ff_get_v(bc);
- st->sample_aspect_ratio.den= ff_get_v(bc);
+ st->sample_aspect_ratio.num= ffio_read_varlen(bc);
+ st->sample_aspect_ratio.den= ffio_read_varlen(bc);
if((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)){
av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n", st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
return -1;
}
- ff_get_v(bc); /* csp type */
+ ffio_read_varlen(bc); /* csp type */
}else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
GET_V(st->codec->sample_rate , tmp > 0)
- ff_get_v(bc); // samplerate_den
+ ffio_read_varlen(bc); // samplerate_den
GET_V(st->codec->channels, tmp > 0)
}
- if(skip_reserved(bc, end) || get_checksum(bc)){
+ if(skip_reserved(bc, end) || ffio_get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "stream header %d checksum mismatch\n", stream_id);
return -1;
}
AVMetadata **metadata = NULL;
end= get_packetheader(nut, bc, 1, INFO_STARTCODE);
- end += url_ftell(bc);
+ end += avio_tell(bc);
GET_V(stream_id_plus1, tmp <= s->nb_streams)
chapter_id = get_s(bc);
- chapter_start= ff_get_v(bc);
- chapter_len = ff_get_v(bc);
- count = ff_get_v(bc);
+ chapter_start= ffio_read_varlen(bc);
+ chapter_len = ffio_read_varlen(bc);
+ count = ffio_read_varlen(bc);
if(chapter_id && !stream_id_plus1){
int64_t start= chapter_start / nut->time_base_count;
value= get_s(bc);
}else if(value == -4){
type= "t";
- value= ff_get_v(bc);
+ value= ffio_read_varlen(bc);
}else if(value < -4){
type= "r";
get_s(bc);
}
}
- if(skip_reserved(bc, end) || get_checksum(bc)){
+ if(skip_reserved(bc, end) || ffio_get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
return -1;
}
AVIOContext *bc = s->pb;
int64_t end, tmp;
- nut->last_syncpoint_pos= url_ftell(bc)-8;
+ nut->last_syncpoint_pos= avio_tell(bc)-8;
end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
- end += url_ftell(bc);
+ end += avio_tell(bc);
- tmp= ff_get_v(bc);
- *back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);
+ tmp= ffio_read_varlen(bc);
+ *back_ptr= nut->last_syncpoint_pos - 16*ffio_read_varlen(bc);
if(*back_ptr < 0)
return -1;
ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp / nut->time_base_count);
- if(skip_reserved(bc, end) || get_checksum(bc)){
+ if(skip_reserved(bc, end) || ffio_get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
return -1;
}
AVIOContext *bc = s->pb;
uint64_t tmp, end;
int i, j, syncpoint_count;
- int64_t filesize= url_fsize(bc);
+ int64_t filesize= avio_size(bc);
int64_t *syncpoints;
int8_t *has_keyframe;
int ret= -1;
- url_fseek(bc, filesize-12, SEEK_SET);
- url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
- if(get_be64(bc) != INDEX_STARTCODE){
+ avio_seek(bc, filesize-12, SEEK_SET);
+ avio_seek(bc, filesize-avio_rb64(bc), SEEK_SET);
+ if(avio_rb64(bc) != INDEX_STARTCODE){
av_log(s, AV_LOG_ERROR, "no index at the end\n");
return -1;
}
end= get_packetheader(nut, bc, 1, INDEX_STARTCODE);
- end += url_ftell(bc);
+ end += avio_tell(bc);
- ff_get_v(bc); //max_pts
+ ffio_read_varlen(bc); //max_pts
GET_V(syncpoint_count, tmp < INT_MAX/8 && tmp > 0)
syncpoints= av_malloc(sizeof(int64_t)*syncpoint_count);
has_keyframe= av_malloc(sizeof(int8_t)*(syncpoint_count+1));
for(i=0; i<syncpoint_count; i++){
- syncpoints[i] = ff_get_v(bc);
+ syncpoints[i] = ffio_read_varlen(bc);
if(syncpoints[i] <= 0)
goto fail;
if(i)
for(i=0; i<s->nb_streams; i++){
int64_t last_pts= -1;
for(j=0; j<syncpoint_count;){
- uint64_t x= ff_get_v(bc);
+ uint64_t x= ffio_read_varlen(bc);
int type= x&1;
int n= j;
x>>=1;
assert(n<=syncpoint_count+1);
for(; j<n && j<syncpoint_count; j++){
if(has_keyframe[j]){
- uint64_t B, A= ff_get_v(bc);
+ uint64_t B, A= ffio_read_varlen(bc);
if(!A){
- A= ff_get_v(bc);
- B= ff_get_v(bc);
+ A= ffio_read_varlen(bc);
+ B= ffio_read_varlen(bc);
//eor_pts[j][i] = last_pts + A + B
}else
B= 0;
}
}
- if(skip_reserved(bc, end) || get_checksum(bc)){
+ if(skip_reserved(bc, end) || ffio_get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
goto fail;
}
pos=0;
for(;;){
uint64_t startcode= find_any_startcode(bc, pos);
- pos= url_ftell(bc);
+ pos= avio_tell(bc);
if(startcode==0){
av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
s->data_offset= pos-8;
- if(!url_is_streamed(bc)){
- int64_t orig_pos= url_ftell(bc);
+ if(bc->seekable){
+ int64_t orig_pos= avio_tell(bc);
find_and_decode_index(nut);
- url_fseek(bc, orig_pos, SEEK_SET);
+ avio_seek(bc, orig_pos, SEEK_SET);
}
assert(nut->next_startcode == SYNCPOINT_STARTCODE);
int size, flags, size_mul, pts_delta, i, reserved_count;
uint64_t tmp;
- if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
- av_log(s, AV_LOG_ERROR, "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n", url_ftell(bc), nut->last_syncpoint_pos, nut->max_distance);
+ if(avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance){
+ av_log(s, AV_LOG_ERROR, "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n", avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
return AVERROR_INVALIDDATA;
}
if(flags & FLAG_INVALID)
return AVERROR_INVALIDDATA;
if(flags & FLAG_CODED)
- flags ^= ff_get_v(bc);
+ flags ^= ffio_read_varlen(bc);
if(flags & FLAG_STREAM_ID){
GET_V(*stream_id, tmp < s->nb_streams)
}
stc= &nut->stream[*stream_id];
if(flags&FLAG_CODED_PTS){
- int coded_pts= ff_get_v(bc);
+ int coded_pts= ffio_read_varlen(bc);
//FIXME check last_pts validity?
if(coded_pts < (1<<stc->msb_pts_shift)){
*pts=ff_lsb2full(stc, coded_pts);
}else
*pts= stc->last_pts + pts_delta;
if(flags&FLAG_SIZE_MSB){
- size += size_mul*ff_get_v(bc);
+ size += size_mul*ffio_read_varlen(bc);
}
if(flags&FLAG_MATCH_TIME)
get_s(bc);
if(flags&FLAG_HEADER_IDX)
- *header_idx= ff_get_v(bc);
+ *header_idx= ffio_read_varlen(bc);
if(flags&FLAG_RESERVED)
- reserved_count= ff_get_v(bc);
+ reserved_count= ffio_read_varlen(bc);
for(i=0; i<reserved_count; i++)
- ff_get_v(bc);
+ ffio_read_varlen(bc);
if(*header_idx >= (unsigned)nut->header_count){
av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
size -= nut->header_len[*header_idx];
if(flags&FLAG_CHECKSUM){
- get_be32(bc); //FIXME check this
+ avio_rb32(bc); //FIXME check this
}else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
return AVERROR_INVALIDDATA;
||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
|| discard >= AVDISCARD_ALL
|| stc->skip_until_key_frame){
- url_fskip(bc, size);
+ avio_skip(bc, size);
return 1;
}
av_new_packet(pkt, size + nut->header_len[header_idx]);
memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
- pkt->pos= url_ftell(bc); //FIXME
- get_buffer(bc, pkt->data + nut->header_len[header_idx], size);
+ pkt->pos= avio_tell(bc); //FIXME
+ avio_read(bc, pkt->data + nut->header_len[header_idx], size);
pkt->stream_index = stream_id;
if (stc->last_flags & FLAG_KEY)
int64_t ts, back_ptr;
for(;;){
- int64_t pos= url_ftell(bc);
+ int64_t pos= avio_tell(bc);
uint64_t tmp= nut->next_startcode;
nut->next_startcode=0;
if(tmp){
pos-=8;
}else{
- frame_code = get_byte(bc);
+ frame_code = avio_r8(bc);
if(url_feof(bc))
return -1;
if(frame_code == 'N'){
tmp= frame_code;
for(i=1; i<8; i++)
- tmp = (tmp<<8) + get_byte(bc);
+ tmp = (tmp<<8) + avio_r8(bc);
}
}
switch(tmp){
case STREAM_STARTCODE:
case INDEX_STARTCODE:
skip= get_packetheader(nut, bc, 0, tmp);
- url_fseek(bc, skip, SEEK_CUR);
+ avio_skip(bc, skip);
break;
case INFO_STARTCODE:
if(decode_info_header(nut)<0)
case SYNCPOINT_STARTCODE:
if(decode_syncpoint(nut, &ts, &back_ptr)<0)
goto resync;
- frame_code = get_byte(bc);
+ frame_code = avio_r8(bc);
case 0:
ret= decode_frame(nut, pkt, frame_code);
if(ret==0)
}
av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
pos= find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
- url_fseek(s->pb, pos, SEEK_SET);
+ avio_seek(s->pb, pos, SEEK_SET);
av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
if(pos2 > pos || pos2 + 15 < pos){
av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");