22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mathematics.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/avassert.h"
57 int lsf, mpeg25, sample_rate_index, bitrate_index,
frame_size;
59 unsigned int header = 0xFFF00000;
61 lsf = sample_rate < (24000 + 32000) / 2;
62 mpeg25 = sample_rate < (12000 + 16000) / 2;
63 sample_rate <<= lsf + mpeg25;
64 if (sample_rate < (32000 + 44100) / 2) sample_rate_index = 2;
65 else if (sample_rate < (44100 + 48000) / 2) sample_rate_index = 0;
66 else sample_rate_index = 1;
70 for (bitrate_index = 2; bitrate_index < 30; bitrate_index++) {
73 frame_size = (frame_size * 144000) / (sample_rate << lsf) +
76 if (frame_size == size)
80 header |= (!lsf) << 19;
81 header |= (4 - layer) << 17;
86 if (bitrate_index == 30)
89 header |= (bitrate_index >> 1) << 12;
90 header |= sample_rate_index << 10;
91 header |= (bitrate_index & 1) << 9;
122 static const uint8_t headers[][5] = {
123 { 3, 0x00, 0x00, 0x01 },
124 { 4, 0x00, 0x00, 0x01, 0xB6},
134 nut->
header[i] = &headers[i - 1][1];
154 if (keyframe_0_esc) {
162 for (stream_id = 0; stream_id < s->
nb_streams; stream_id++) {
164 int end2 = start + (end - start) * (stream_id + 1) / s->
nb_streams;
177 if (f.
den == 1 && f.
num>0)
183 for (key_frame = 0; key_frame < 2; key_frame++) {
184 if (!
intra_only || !keyframe_0_esc || key_frame != 0) {
199 int frame_bytes = codec->frame_size * (int64_t)codec->bit_rate /
200 (8 * codec->sample_rate);
202 for (pts = 0; pts < 2; pts++) {
203 for (pred = 0; pred < 2; pred++) {
224 if (codec->has_b_frames) {
241 for (pred = 0; pred < pred_count; pred++) {
242 int start3 = start2 + (end2 - start2) * pred / pred_count;
243 int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
247 for (index = start3; index < end3; index++) {
278 int len = strlen(
string);
290 static inline void ff_put_v_trace(
AVIOContext *bc, uint64_t
v,
const char *file,
293 av_log(NULL,
AV_LOG_DEBUG,
"ff_put_v %5"PRId64
" / %"PRIX64
" in %s %s:%d\n", v, v, file, func, line);
298 static inline void put_s_trace(
AVIOContext *bc, int64_t
v,
const char *file,
const char *
func,
int line)
300 av_log(NULL,
AV_LOG_DEBUG,
"put_s %5"PRId64
" / %"PRIX64
" in %s %s:%d\n", v, v, file, func, line);
304 #define ff_put_v(bc, v) ff_put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
305 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
310 int calculate_checksum, uint64_t startcode)
314 int forw_ptr = dyn_size + 4 * calculate_checksum;
323 if (calculate_checksum)
326 if (calculate_checksum)
334 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
351 tmp_match = 1 - (1LL << 62);
353 for (i = 0; i < 256; ) {
372 for (j = 0; i < 256; j++, i++) {
386 if (j != tmp_mul - tmp_size)
391 if (tmp_fields > 0)
put_s(bc, tmp_pts);
392 if (tmp_fields > 1)
ff_put_v(bc, tmp_mul);
393 if (tmp_fields > 2)
ff_put_v(bc, tmp_stream);
394 if (tmp_fields > 3)
ff_put_v(bc, tmp_size);
395 if (tmp_fields > 4)
ff_put_v(bc, 0 );
396 if (tmp_fields > 5)
ff_put_v(bc, j);
397 if (tmp_fields > 6)
ff_put_v(bc, tmp_match);
398 if (tmp_fields > 7)
ff_put_v(bc, tmp_head_idx);
477 int count = 0, dyn_size;
504 int count=0, dyn_size, i;
518 count +=
add_info(dyn_bc,
"r_frame_rate", buf);
570 int64_t payload_size;
578 ff_put_v(bc, (next_node[1]->pos >> 4) - (dummy.
pos>>4));
579 dummy.
pos = next_node[1]->
pos;
593 for (k= j - n; k<=j && k<nut->
sp_count; k++) {
603 payload_size =
avio_tell(bc) - startpos + 8 + 4;
605 avio_wb64(bc, 8 + payload_size +
av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
717 if (INT64_C(1000) * time_base.
num >= time_base.
den)
789 if (pkt->
size > 4096)
809 int best_length, frame_code,
flags, needed_flags, i, header_idx;
817 "Negative pts not supported stream %d, pts %"PRId64
"\n",
848 if (dummy.
pos == INT64_MAX)
883 best_length = INT_MAX;
885 for (i = 0; i < 256; i++) {
888 int flags = fc->
flags;
896 flags = needed_flags;
899 if ((flags & needed_flags) != needed_flags)
902 if ((flags ^ needed_flags) &
FLAG_KEY)
919 if ( (flags & FLAG_CODED)
925 length += 1 - nut->
header_len[best_header_idx];
934 if (length < best_length) {
948 ff_put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
949 flags = needed_flags;
1013 .mime_type =
"video/x-nut",
1014 .extensions =
"nut",
unsigned int nb_chapters
Number of chapters in AVChapter array.
const char const char void * val
#define CONFIG_LIBMP3LAME
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
int64_t last_syncpoint_pos
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding) av_const
Rescale a 64-bit integer with specified rounding.
void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
AVCodecContext * codec
Codec context associated with this stream.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
mpeg audio layer common tables.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
const uint8_t * header[128]
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(void *key, const void *b), void *next[2])
static void build_elision_headers(AVFormatContext *s)
static void build_frame_code(AVFormatContext *s)
static av_cold int end(AVCodecContext *avctx)
if set, coded_pts is in the frame header
const uint16_t avpriv_mpa_freq_tab[3]
static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
const AVMetadataConv ff_nut_metadata_conv[]
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
int ff_nut_sp_pos_cmp(const Syncpoint *a, const Syncpoint *b)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static const uint8_t frame_size[4]
AVOutputFormat ff_nut_muxer
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
void ff_nut_free_sp(NUTContext *nut)
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
void * priv_data
Format private data.
static int nut_write_header(AVFormatContext *s)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVRational av_div_q(AVRational b, AVRational c) av_const
Divide one rational by another.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
preferred ID for decoding MPEG audio layer 1, 2 or 3
static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
if set, frame is keyframe
int flags
A combination of AV_PKT_FLAG values.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
static void write_mainheader(NUTContext *nut, AVIOContext *bc)
unsigned int nb_streams
A list of all streams in the file.
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64])
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
int width
picture width / height.
static int write_trailer(AVFormatContext *s1)
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
if set, frame_code is invalid
struct AVTreeNode * syncpoints
int avoid_negative_ts
Avoid negative timestamps during muxing.
if set, data_size_msb is at frame header, otherwise data_size_msb is 0
if set, the frame header contains a checksum
preferred ID for MPEG-1/2 video decoding
static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
static const float pred[4]
int64_t end
chapter start/end time in time_base units
enum AVMediaType codec_type
int sample_rate
samples per second
main external API structure.
static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id)
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
static int write_index(NUTContext *nut, AVIOContext *bc)
if set, coded_flags are stored in the frame header
AVIOContext * pb
I/O context.
rational number numerator/denominator
static int nut_write_trailer(AVFormatContext *s)
unsigned long ffio_get_checksum(AVIOContext *s)
int(* func)(AVBPrint *dst, const char *in, const char *arg)
static void put_s(AVIOContext *bc, int64_t val)
static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc, AVStream *st, int i)
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type)
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
const Dispositions ff_nut_dispositions[]
static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
FrameCode frame_code[256]
int disposition
AV_DISPOSITION_* bit field.
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
AVRational time_base
time base in which the start/end timestamps are specified
#define SYNCPOINT_STARTCODE
static void put_str(AVIOContext *bc, const char *string)
Store a string as vb.
static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc, int calculate_checksum, uint64_t startcode)
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
If set, header_idx is coded in the frame header.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int channels
number of audio channels
static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt)
static void write_header(FFV1Context *f)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
if set, stream_id is coded in the frame header
static int add_info(AVIOContext *bc, const char *type, const char *value)
#define AV_DICT_IGNORE_SUFFIX
#define av_assert0(cond)
assert() equivalent, that is always enabled.
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
const AVCodecTag *const ff_nut_codec_tags[]
This structure stores compressed data.
unsigned int time_base_count
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
unsigned int max_distance