⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nutdec.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * "NUT" Container Format demuxer * Copyright (c) 2004-2006 Michael Niedermayer * Copyright (c) 2003 Alex Beregszaszi * * 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 "tree.h"#include "nut.h"#include "avstring.h"#undef NDEBUG#include <assert.h>static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){    unsigned int len= ff_get_v(bc);    if(len && maxlen)        get_buffer(bc, string, FFMIN(len, maxlen));    while(len > maxlen){        get_byte(bc);        len--;    }    if(maxlen)        string[FFMIN(len, maxlen-1)]= 0;    if(maxlen == len)        return -1;    else        return 0;}static int64_t get_s(ByteIOContext *bc){    int64_t v = ff_get_v(bc) + 1;    if (v&1) return -(v>>1);    else     return  (v>>1);}static uint64_t get_fourcc(ByteIOContext *bc){    unsigned int len= ff_get_v(bc);    if     (len==2) return get_le16(bc);    else if(len==4) return get_le32(bc);    else            return -1;}#ifdef TRACEstatic inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){    uint64_t v= ff_get_v(bc);    av_log(NULL, AV_LOG_DEBUG, "get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);    return v;}static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){    int64_t v= get_s(bc);    av_log(NULL, AV_LOG_DEBUG, "get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);    return v;}static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){    uint64_t v= get_vb(bc);    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 get_s(bc)  get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)#define get_vb(bc)  get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)#endifstatic int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum, uint64_t startcode){    int64_t size;//    start= url_ftell(bc) - 8;    startcode= be2me_64(startcode);    startcode= ff_crc04C11DB7_update(0, &startcode, 8);    init_checksum(bc, ff_crc04C11DB7_update, startcode);    size= ff_get_v(bc);    if(size > 4096)        get_be32(bc);    if(get_checksum(bc) && size > 4096)        return -1;    init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);    return size;}static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){    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 are currently    while(!url_feof(bc)){        state= (state<<8) | get_byte(bc);        if((state>>56) != 'N')            continue;        switch(state){        case MAIN_STARTCODE:        case STREAM_STARTCODE:        case SYNCPOINT_STARTCODE:        case INFO_STARTCODE:        case INDEX_STARTCODE:            return state;        }    }    return 0;}/** * Find the given startcode. * @param code the startcode * @param pos the start position of the search, or -1 if the current position * @returns the position of the startcode or -1 if not found */static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){    for(;;){        uint64_t startcode= find_any_startcode(bc, pos);        if(startcode == code)            return url_ftell(bc) - 8;        else if(startcode == 0)            return -1;        pos=-1;    }}static int nut_probe(AVProbeData *p){    int i;    uint64_t code= 0;    for (i = 0; i < p->buf_size; i++) {        code = (code << 8) | p->buf[i];        if (code == MAIN_STARTCODE)            return AVPROBE_SCORE_MAX;    }    return 0;}#define GET_V(dst, check) \    tmp= ff_get_v(bc);\    if(!(check)){\        av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp);\        return -1;\    }\    dst= tmp;static int skip_reserved(ByteIOContext *bc, int64_t pos){    pos -= url_ftell(bc);    if(pos<0){        url_fseek(bc, pos, SEEK_CUR);        return -1;    }else{        while(pos--)            get_byte(bc);        return 0;    }}static int decode_main_header(NUTContext *nut){    AVFormatContext *s= nut->avf;    ByteIOContext *bc = s->pb;    uint64_t tmp, end;    unsigned int stream_count;    int i, j, tmp_stream, tmp_mul, tmp_pts, tmp_size, count, tmp_res;    end= get_packetheader(nut, bc, 1, MAIN_STARTCODE);    end += url_ftell(bc);    GET_V(tmp              , tmp >=2 && tmp <= 3)    GET_V(stream_count     , tmp > 0 && tmp <=MAX_STREAMS)    nut->max_distance = ff_get_v(bc);    if(nut->max_distance > 65536){        av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);        nut->max_distance= 65536;    }    GET_V(nut->time_base_count, tmp>0 && tmp<INT_MAX / sizeof(AVRational))    nut->time_base= av_malloc(nut->time_base_count * sizeof(AVRational));    for(i=0; i<nut->time_base_count; i++){        GET_V(nut->time_base[i].num, tmp>0 && tmp<(1ULL<<31))        GET_V(nut->time_base[i].den, tmp>0 && tmp<(1ULL<<31))        if(ff_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1){            av_log(s, AV_LOG_ERROR, "time base invalid\n");            return -1;        }    }    tmp_pts=0;    tmp_mul=1;    tmp_stream=0;    for(i=0; i<256;){        int tmp_flags = ff_get_v(bc);        int tmp_fields= ff_get_v(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);        else             tmp_size  = 0;        if(tmp_fields>4) tmp_res   = ff_get_v(bc);        else             tmp_res   = 0;        if(tmp_fields>5) count     = ff_get_v(bc);        else             count     = tmp_mul - tmp_size;        while(tmp_fields-- > 6)           ff_get_v(bc);        if(count == 0 || i+count > 256){            av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);            return -1;        }        if(tmp_stream >= stream_count){            av_log(s, AV_LOG_ERROR, "illegal stream number\n");            return -1;        }        for(j=0; j<count; j++,i++){            if (i == 'N') {                nut->frame_code[i].flags= FLAG_INVALID;                j--;                continue;            }            nut->frame_code[i].flags           = tmp_flags ;            nut->frame_code[i].pts_delta       = tmp_pts   ;            nut->frame_code[i].stream_id       = tmp_stream;            nut->frame_code[i].size_mul        = tmp_mul   ;            nut->frame_code[i].size_lsb        = tmp_size+j;            nut->frame_code[i].reserved_count  = tmp_res   ;        }    }    assert(nut->frame_code['N'].flags == FLAG_INVALID);    if(skip_reserved(bc, end) || get_checksum(bc)){        av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");        return -1;    }    nut->stream = av_mallocz(sizeof(StreamContext)*stream_count);    for(i=0; i<stream_count; i++){        av_new_stream(s, i);    }    return 0;}static int decode_stream_header(NUTContext *nut){    AVFormatContext *s= nut->avf;    ByteIOContext *bc = s->pb;    StreamContext *stc;    int class, stream_id;    uint64_t tmp, end;    AVStream *st;    end= get_packetheader(nut, bc, 1, STREAM_STARTCODE);    end += url_ftell(bc);    GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);    stc= &nut->stream[stream_id];    st = s->streams[stream_id];    if (!st)        return AVERROR(ENOMEM);    class = ff_get_v(bc);    tmp = get_fourcc(bc);    st->codec->codec_tag= tmp;    switch(class)    {        case 0:            st->codec->codec_type = CODEC_TYPE_VIDEO;            st->codec->codec_id = codec_get_id(codec_bmp_tags, tmp);            if (st->codec->codec_id == CODEC_ID_NONE)                av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");            break;        case 1:            st->codec->codec_type = CODEC_TYPE_AUDIO;            st->codec->codec_id = codec_get_id(codec_wav_tags, tmp);            if (st->codec->codec_id == CODEC_ID_NONE)                av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");            break;        case 2://            st->codec->codec_type = CODEC_TYPE_TEXT;//            break;        case 3:            st->codec->codec_type = CODEC_TYPE_DATA;            break;        default:            av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);            return -1;    }    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);    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    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);    }    if (st->codec->codec_type == CODEC_TYPE_VIDEO){        GET_V(st->codec->width , tmp > 0)        GET_V(st->codec->height, tmp > 0)        st->codec->sample_aspect_ratio.num= ff_get_v(bc);        st->codec->sample_aspect_ratio.den= ff_get_v(bc);        if((!st->codec->sample_aspect_ratio.num) != (!st->codec->sample_aspect_ratio.den)){            av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n", st->codec->sample_aspect_ratio.num, st->codec->sample_aspect_ratio.den);            return -1;        }        ff_get_v(bc); /* csp type */    }else if (st->codec->codec_type == CODEC_TYPE_AUDIO){        GET_V(st->codec->sample_rate , tmp > 0)        ff_get_v(bc); // samplerate_den        GET_V(st->codec->channels, tmp > 0)    }    if(skip_reserved(bc, end) || get_checksum(bc)){        av_log(s, AV_LOG_ERROR, "stream header %d checksum mismatch\n", stream_id);        return -1;    }    stc->time_base= &nut->time_base[stc->time_base_id];    av_set_pts_info(s->streams[stream_id], 63, stc->time_base->num, stc->time_base->den);    return 0;}static int decode_info_header(NUTContext *nut){    AVFormatContext *s= nut->avf;    ByteIOContext *bc = s->pb;    uint64_t tmp;    unsigned int stream_id_plus1, chapter_start, chapter_len, count;    int chapter_id, i;    int64_t value, end;    char name[256], str_value[1024], type_str[256];    const char *type;    end= get_packetheader(nut, bc, 1, INFO_STARTCODE);    end += url_ftell(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);    for(i=0; i<count; i++){        get_str(bc, name, sizeof(name));        value= get_s(bc);        if(value == -1){            type= "UTF-8";            get_str(bc, str_value, sizeof(str_value));        }else if(value == -2){            get_str(bc, type_str, sizeof(type_str));            type= type_str;            get_str(bc, str_value, sizeof(str_value));        }else if(value == -3){            type= "s";            value= get_s(bc);        }else if(value == -4){            type= "t";            value= ff_get_v(bc);        }else if(value < -4){            type= "r";            get_s(bc);        }else{            type= "v";        }        if(chapter_id==0 && !strcmp(type, "UTF-8")){            if     (!strcmp(name, "Author"))                av_strlcpy(s->author   , str_value, sizeof(s->author));            else if(!strcmp(name, "Title"))                av_strlcpy(s->title    , str_value, sizeof(s->title));            else if(!strcmp(name, "Copyright"))                av_strlcpy(s->copyright, str_value, sizeof(s->copyright));            else if(!strcmp(name, "Description"))                av_strlcpy(s->comment  , str_value, sizeof(s->comment));        }    }    if(skip_reserved(bc, end) || get_checksum(bc)){        av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");        return -1;    }    return 0;}static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){    AVFormatContext *s= nut->avf;    ByteIOContext *bc = s->pb;    int64_t end, tmp;    nut->last_syncpoint_pos= url_ftell(bc)-8;    end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);    end += url_ftell(bc);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -