annotate nut.c @ 1298:29fba975df47 libavformat

Allow to get the the track number in the IPRT (part) tag in AVI. Patch by Panagiotis Issaris % takis P issaris A uhasselt P be % Original thread: Date: Sep 6, 2006 6:00 PM Subject: [Ffmpeg-devel] [PATCH] Enable track tag in AVI
author gpoirier
date Thu, 07 Sep 2006 16:35:22 +0000
parents eb0a8abb507c
children 0899bfe4105c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1 /*
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
2 * "NUT" Container Format muxer and demuxer (DRAFT-200403??)
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
3 * Copyright (c) 2003 Alex Beregszaszi
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
4 * Copyright (c) 2004 Michael Niedermayer
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
5 *
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
10 *
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
14 * Lesser General Public License for more details.
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
15 *
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
16 * You should have received a copy of the GNU General Public
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 887
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
19 *
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
20 *
787
386fff926e83 some nut.hu hype
alex
parents: 782
diff changeset
21 * Visit the official site at http://www.nut.hu/
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
22 *
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
23 */
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
24
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
25 /*
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
26 * TODO:
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
27 * - index writing
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
28 * - index packet reading support
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
29 */
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
30
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
31 //#define DEBUG 1
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
32
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
33 #include <limits.h>
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
34 #include "avformat.h"
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
35 #include "mpegaudio.h"
1172
6a5e58d2114b move common stuff from avienc.c and wav.c to new file riff.c
mru
parents: 1169
diff changeset
36 #include "riff.h"
1176
bfea7dcd2698 move adler32 to libavutil
mru
parents: 1172
diff changeset
37 #include "adler32.h"
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
38
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
39 #undef NDEBUG
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
40 #include <assert.h>
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
41
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
42 //#define TRACE
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
43
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
44 //from /dev/random
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
45
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
46 #define MAIN_STARTCODE (0x7A561F5F04ADULL + (((uint64_t)('N'<<8) + 'M')<<48))
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
47 #define STREAM_STARTCODE (0x11405BF2F9DBULL + (((uint64_t)('N'<<8) + 'S')<<48))
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
48 #define KEYFRAME_STARTCODE (0xE4ADEECA4569ULL + (((uint64_t)('N'<<8) + 'K')<<48))
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
49 #define INDEX_STARTCODE (0xDD672F23E64EULL + (((uint64_t)('N'<<8) + 'X')<<48))
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
50 #define INFO_STARTCODE (0xAB68B596BA78ULL + (((uint64_t)('N'<<8) + 'I')<<48))
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
51
478
ac85aafd0971 file id string
michael
parents: 468
diff changeset
52 #define ID_STRING "nut/multimedia container\0"
ac85aafd0971 file id string
michael
parents: 468
diff changeset
53
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
54 #define MAX_DISTANCE (1024*16-1)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
55 #define MAX_SHORT_DISTANCE (1024*4-1)
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
56
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
57 #define FLAG_DATA_SIZE 1
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
58 #define FLAG_KEY_FRAME 2
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
59 #define FLAG_INVALID 4
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
60
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
61 typedef struct {
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
62 uint8_t flags;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
63 uint8_t stream_id_plus1;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
64 uint16_t size_mul;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
65 uint16_t size_lsb;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
66 int16_t timestamp_delta;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
67 uint8_t reserved_count;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
68 } FrameCode;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
69
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
70 typedef struct {
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
71 int last_key_frame;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
72 int msb_timestamp_shift;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
73 int rate_num;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
74 int rate_den;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
75 int64_t last_pts;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
76 int64_t last_sync_pos; ///<pos of last 1/2 type frame
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
77 int decode_delay;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
78 } StreamContext;
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
79
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
80 typedef struct {
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
81 AVFormatContext *avf;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
82 int written_packet_size;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
83 int64_t packet_start[3]; //0-> startcode less, 1-> short startcode 2-> long startcodes
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
84 FrameCode frame_code[256];
639
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
85 unsigned int stream_count;
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
86 uint64_t next_startcode; ///< stores the next startcode if it has alraedy been parsed but the stream isnt seekable
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
87 StreamContext *stream;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
88 int max_distance;
478
ac85aafd0971 file id string
michael
parents: 468
diff changeset
89 int max_short_distance;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
90 int rate_num;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
91 int rate_den;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
92 int short_startcode;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
93 } NUTContext;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
94
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
95 static char *info_table[][2]={
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
96 {NULL , NULL }, // end
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
97 {NULL , NULL },
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
98 {NULL , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
99 {NULL , "v"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
100 {NULL , "s"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
101 {"StreamId" , "v"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
102 {"SegmentId" , "v"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
103 {"StartTimestamp" , "v"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
104 {"EndTimestamp" , "v"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
105 {"Author" , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
106 {"Title" , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
107 {"Description" , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
108 {"Copyright" , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
109 {"Encoder" , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
110 {"Keyword" , "UTF8"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
111 {"Cover" , "JPEG"},
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
112 {"Cover" , "PNG"},
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
113 };
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
114
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
115 static void update(NUTContext *nut, int stream_index, int64_t frame_start, int frame_type, int frame_code, int key_frame, int size, int64_t pts){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
116 StreamContext *stream= &nut->stream[stream_index];
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
117
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
118 stream->last_key_frame= key_frame;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
119 nut->packet_start[ frame_type ]= frame_start;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
120 stream->last_pts= pts;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
121 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
122
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
123 static void reset(AVFormatContext *s, int64_t global_ts){
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
124 NUTContext *nut = s->priv_data;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
125 int i;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
126
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
127 for(i=0; i<s->nb_streams; i++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
128 StreamContext *stream= &nut->stream[i];
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
129
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
130 stream->last_key_frame= 1;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
131
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
132 stream->last_pts= av_rescale(global_ts, stream->rate_num*(int64_t)nut->rate_den, stream->rate_den*(int64_t)nut->rate_num);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
133 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
134 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
135
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
136 static void build_frame_code(AVFormatContext *s){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
137 NUTContext *nut = s->priv_data;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
138 int key_frame, index, pred, stream_id;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
139 int start=0;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
140 int end= 255;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
141 int keyframe_0_esc= s->nb_streams > 2;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
142 int pred_table[10];
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
143
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
144 if(keyframe_0_esc){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
145 /* keyframe = 0 escape */
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
146 FrameCode *ft= &nut->frame_code[start];
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
147 ft->flags= FLAG_DATA_SIZE;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
148 ft->stream_id_plus1= 0;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
149 ft->size_mul=1;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
150 ft->timestamp_delta=0;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
151 start++;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
152 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
153
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
154 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
155 int start2= start + (end-start)*stream_id / s->nb_streams;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
156 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
157 AVCodecContext *codec = s->streams[stream_id]->codec;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
158 int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
159 int intra_only= /*codec->intra_only || */is_audio;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
160 int pred_count;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
161
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
162 for(key_frame=0; key_frame<2; key_frame++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
163 if(intra_only && keyframe_0_esc && key_frame==0)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
164 continue;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
165
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
166 {
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
167 FrameCode *ft= &nut->frame_code[start2];
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
168 ft->flags= FLAG_KEY_FRAME*key_frame;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
169 ft->flags|= FLAG_DATA_SIZE;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
170 ft->stream_id_plus1= stream_id + 1;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
171 ft->size_mul=1;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
172 ft->timestamp_delta=0;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
173 start2++;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
174 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
175 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
176
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
177 key_frame= intra_only;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
178 #if 1
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
179 if(is_audio){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
180 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
181 int pts;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
182 for(pts=0; pts<2; pts++){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
183 for(pred=0; pred<2; pred++){
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
184 FrameCode *ft= &nut->frame_code[start2];
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
185 ft->flags= FLAG_KEY_FRAME*key_frame;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
186 ft->stream_id_plus1= stream_id + 1;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
187 ft->size_mul=frame_bytes + 2;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
188 ft->size_lsb=frame_bytes + pred;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
189 ft->timestamp_delta=pts;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
190 start2++;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
191 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
192 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
193 }else{
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
194 FrameCode *ft= &nut->frame_code[start2];
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
195 ft->flags= FLAG_KEY_FRAME | FLAG_DATA_SIZE;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
196 ft->stream_id_plus1= stream_id + 1;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
197 ft->size_mul=1;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
198 ft->timestamp_delta=1;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
199 start2++;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
200 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
201 #endif
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
202
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
203 if(codec->has_b_frames){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
204 pred_count=5;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
205 pred_table[0]=-2;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
206 pred_table[1]=-1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
207 pred_table[2]=1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
208 pred_table[3]=3;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
209 pred_table[4]=4;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
210 }else if(codec->codec_id == CODEC_ID_VORBIS){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
211 pred_count=3;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
212 pred_table[0]=2;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
213 pred_table[1]=9;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
214 pred_table[2]=16;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
215 }else{
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
216 pred_count=1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
217 pred_table[0]=1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
218 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
219
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
220 for(pred=0; pred<pred_count; pred++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
221 int start3= start2 + (end2-start2)*pred / pred_count;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
222 int end3 = start2 + (end2-start2)*(pred+1) / pred_count;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
223
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
224 for(index=start3; index<end3; index++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
225 FrameCode *ft= &nut->frame_code[index];
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
226 ft->flags= FLAG_KEY_FRAME*key_frame;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
227 ft->flags|= FLAG_DATA_SIZE;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
228 ft->stream_id_plus1= stream_id + 1;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
229 //FIXME use single byte size and pred from last
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
230 ft->size_mul= end3-start3;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
231 ft->size_lsb= index - start3;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
232 ft->timestamp_delta= pred_table[pred];
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
233 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
234 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
235 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
236 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
237 nut->frame_code['N'].flags= FLAG_INVALID;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
238 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
239
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
240 static uint64_t get_v(ByteIOContext *bc)
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
241 {
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
242 uint64_t val = 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
243
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
244 for(;;)
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
245 {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
246 int tmp = get_byte(bc);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
247
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
248 if (tmp&0x80)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
249 val= (val<<7) + tmp - 0x80;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
250 else{
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
251 //av_log(NULL, AV_LOG_DEBUG, "get_v()= %lld\n", (val<<7) + tmp);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
252 return (val<<7) + tmp;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
253 }
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
254 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
255 return -1;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
256 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
257
639
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
258 static int get_str(ByteIOContext *bc, char *string, unsigned int maxlen){
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
259 unsigned int len= get_v(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
260
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
261 if(len && maxlen)
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
262 get_buffer(bc, string, FFMIN(len, maxlen));
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
263 while(len > maxlen){
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
264 get_byte(bc);
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
265 len--;
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
266 }
240
e6f99d238179 get_bi(), be/le fix
michaelni
parents: 232
diff changeset
267
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
268 if(maxlen)
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
269 string[FFMIN(len, maxlen-1)]= 0;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
270
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
271 if(maxlen == len)
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
272 return -1;
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
273 else
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
274 return 0;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
275 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
276
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
277 static int64_t get_s(ByteIOContext *bc){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
278 int64_t v = get_v(bc) + 1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
279
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
280 if (v&1) return -(v>>1);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
281 else return (v>>1);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
282 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
283
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
284 static uint64_t get_vb(ByteIOContext *bc){
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
285 uint64_t val=0;
639
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
286 unsigned int i= get_v(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
287
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
288 if(i>8)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
289 return UINT64_MAX;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
290
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
291 while(i--)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
292 val = (val<<8) + get_byte(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
293
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
294 //av_log(NULL, AV_LOG_DEBUG, "get_vb()= %lld\n", val);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
295 return val;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
296 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
297
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
298 #ifdef TRACE
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
299 static inline uint64_t get_v_trace(ByteIOContext *bc, char *file, char *func, int line){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
300 uint64_t v= get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
301
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
302 printf("get_v %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
303 return v;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
304 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
305
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
306 static inline int64_t get_s_trace(ByteIOContext *bc, char *file, char *func, int line){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
307 int64_t v= get_s(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
308
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
309 printf("get_s %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
310 return v;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
311 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
312
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
313 static inline uint64_t get_vb_trace(ByteIOContext *bc, char *file, char *func, int line){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
314 uint64_t v= get_vb(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
315
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
316 printf("get_vb %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
317 return v;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
318 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
319 #define get_v(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
320 #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
321 #define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
322 #endif
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
323
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
324
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
325 static int get_packetheader(NUTContext *nut, ByteIOContext *bc, int calculate_checksum)
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
326 {
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
327 int64_t start, size;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
328 start= url_ftell(bc) - 8;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
329
854
c9d9796a3d00 checksum doesnt cover the forward_ptr anymore
michael
parents: 853
diff changeset
330 size= get_v(bc);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
331
1184
5a743f6f836a fix regression failure after adler simplification
michael
parents: 1176
diff changeset
332 init_checksum(bc, calculate_checksum ? av_adler32_update : NULL, 1);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
333
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
334 nut->packet_start[2] = start;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
335 nut->written_packet_size= size;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
336
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
337 return size;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
338 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
339
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
340 static int check_checksum(ByteIOContext *bc){
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
341 unsigned long checksum= get_checksum(bc);
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
342 return checksum != get_be32(bc);
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
343 }
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
344
221
fa80efcf8e39 padding fix
michaelni
parents: 220
diff changeset
345 /**
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
346 *
221
fa80efcf8e39 padding fix
michaelni
parents: 220
diff changeset
347 */
fa80efcf8e39 padding fix
michaelni
parents: 220
diff changeset
348 static int get_length(uint64_t val){
fa80efcf8e39 padding fix
michaelni
parents: 220
diff changeset
349 int i;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
350
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
351 for (i=7; val>>i; i+=7);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
352
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
353 return i;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
354 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
355
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
356 static uint64_t find_any_startcode(ByteIOContext *bc, int64_t pos){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
357 uint64_t state=0;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
358
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
359 if(pos >= 0)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
360 url_fseek(bc, pos, SEEK_SET); //note, this may fail if the stream isnt seekable, but that shouldnt matter, as in this case we simply start where we are currently
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
361
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
362 while(!url_feof(bc)){
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
363 state= (state<<8) | get_byte(bc);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
364 if((state>>56) != 'N')
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
365 continue;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
366 switch(state){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
367 case MAIN_STARTCODE:
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
368 case STREAM_STARTCODE:
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
369 case KEYFRAME_STARTCODE:
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
370 case INFO_STARTCODE:
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
371 case INDEX_STARTCODE:
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
372 return state;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
373 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
374 }
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
375
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
376 return 0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
377 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
378
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
379 /**
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
380 * find the given startcode.
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
381 * @param code the startcode
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
382 * @param pos the start position of the search, or -1 if the current position
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
383 * @returns the position of the startcode or -1 if not found
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
384 */
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
385 static int64_t find_startcode(ByteIOContext *bc, uint64_t code, int64_t pos){
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
386 for(;;){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
387 uint64_t startcode= find_any_startcode(bc, pos);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
388 if(startcode == code)
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
389 return url_ftell(bc) - 8;
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
390 else if(startcode == 0)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
391 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
392 pos=-1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
393 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
394 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
395
902
5d1bcf01ce28 Properly #ifdef muxer-specific parts.
diego
parents: 896
diff changeset
396 static int64_t lsb2full(StreamContext *stream, int64_t lsb){
5d1bcf01ce28 Properly #ifdef muxer-specific parts.
diego
parents: 896
diff changeset
397 int64_t mask = (1<<stream->msb_timestamp_shift)-1;
5d1bcf01ce28 Properly #ifdef muxer-specific parts.
diego
parents: 896
diff changeset
398 int64_t delta= stream->last_pts - mask/2;
5d1bcf01ce28 Properly #ifdef muxer-specific parts.
diego
parents: 896
diff changeset
399 return ((lsb - delta)&mask) + delta;
5d1bcf01ce28 Properly #ifdef muxer-specific parts.
diego
parents: 896
diff changeset
400 }
5d1bcf01ce28 Properly #ifdef muxer-specific parts.
diego
parents: 896
diff changeset
401
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 854
diff changeset
402 #ifdef CONFIG_MUXERS
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
403
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
404 static void put_v(ByteIOContext *bc, uint64_t val)
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
405 {
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
406 int i;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
407
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
408 //av_log(NULL, AV_LOG_DEBUG, "put_v()= %lld\n", val);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
409 val &= 0x7FFFFFFFFFFFFFFFULL; // FIXME can only encode upto 63 bits currently
221
fa80efcf8e39 padding fix
michaelni
parents: 220
diff changeset
410 i= get_length(val);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
411
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
412 for (i-=7; i>0; i-=7){
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
413 put_byte(bc, 0x80 | (val>>i));
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
414 }
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
415
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
416 put_byte(bc, val&0x7f);
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
417 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
418
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
419 /**
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
420 * stores a string as vb.
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
421 */
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
422 static void put_str(ByteIOContext *bc, const char *string){
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
423 int len= strlen(string);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
424
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
425 put_v(bc, len);
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
426 put_buffer(bc, string, len);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
427 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
428
699
2f5f4578a076 "put_s should store signed values. Spotted on #mplayerdev by someone I
michael
parents: 683
diff changeset
429 static void put_s(ByteIOContext *bc, int64_t val){
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
430 if (val<=0) put_v(bc, -2*val );
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
431 else put_v(bc, 2*val-1);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
432 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
433
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
434 static void put_vb(ByteIOContext *bc, uint64_t val){
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
435 int i;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
436
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
437 for (i=8; val>>i; i+=8);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
438
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
439 put_v(bc, i>>3);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
440 for(i-=8; i>=0; i-=8)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
441 put_byte(bc, (val>>i)&0xFF);
228
542e6d639bb9 fixed endianness dependancies (untested)
bellard
parents: 223
diff changeset
442 }
542e6d639bb9 fixed endianness dependancies (untested)
bellard
parents: 223
diff changeset
443
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
444 #ifdef TRACE
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
445 static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
446 printf("get_v %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
447
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
448 put_v(bc, v);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
449 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
450
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
451 static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
452 printf("get_s %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
453
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
454 put_s(bc, v);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
455 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
456
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
457 static inline void put_vb_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
458 printf("get_vb %5lld / %llX in %s %s:%d\n", v, v, file, func, line);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
459
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
460 put_vb(bc, v);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
461 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
462 #define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
463 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
464 #define put_vb(bc, v) put_vb_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
465 #endif
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
466
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
467 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum)
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
468 {
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
469 put_flush_packet(bc);
853
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
470 nut->packet_start[2]= url_ftell(bc) - 8;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
471 nut->written_packet_size = max_size;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
472
854
c9d9796a3d00 checksum doesnt cover the forward_ptr anymore
michael
parents: 853
diff changeset
473 /* packet header */
c9d9796a3d00 checksum doesnt cover the forward_ptr anymore
michael
parents: 853
diff changeset
474 put_v(bc, nut->written_packet_size); /* forward ptr */
c9d9796a3d00 checksum doesnt cover the forward_ptr anymore
michael
parents: 853
diff changeset
475
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
476 if(calculate_checksum)
1184
5a743f6f836a fix regression failure after adler simplification
michael
parents: 1176
diff changeset
477 init_checksum(bc, av_adler32_update, 1);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
478
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
479 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
480 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
481
853
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
482 /**
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
483 *
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
484 * must not be called more then once per packet
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
485 */
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
486 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
487 int64_t start= nut->packet_start[2];
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
488 int64_t cur= url_ftell(bc);
853
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
489 int size= cur - start - get_length(nut->written_packet_size)/7 - 8;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
490
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
491 if(calculate_checksum)
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
492 size += 4;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
493
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
494 if(size != nut->written_packet_size){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
495 int i;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
496
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
497 assert( size <= nut->written_packet_size );
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
498
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
499 url_fseek(bc, start + 8, SEEK_SET);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
500 for(i=get_length(size); i < get_length(nut->written_packet_size); i+=7)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
501 put_byte(bc, 0x80);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
502 put_v(bc, size);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
503
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
504 url_fseek(bc, cur, SEEK_SET);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
505 nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
506
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
507 if(calculate_checksum)
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
508 put_be32(bc, get_checksum(bc));
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
509 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
510
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
511 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
512 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
513
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
514 static int nut_write_header(AVFormatContext *s)
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
515 {
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
516 NUTContext *nut = s->priv_data;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
517 ByteIOContext *bc = &s->pb;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
518 AVCodecContext *codec;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
519 int i, j, tmp_time, tmp_flags,tmp_stream, tmp_mul, tmp_size, tmp_fields;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
520
1066
716c75917a10 Disable lavf NUT until it is synced to spec
ods15
parents: 979
diff changeset
521 if (strcmp(s->filename, "./data/b-libav.nut")) {
716c75917a10 Disable lavf NUT until it is synced to spec
ods15
parents: 979
diff changeset
522 av_log(s, AV_LOG_ERROR, " libavformat NUT is non-compliant and disabled\n");
716c75917a10 Disable lavf NUT until it is synced to spec
ods15
parents: 979
diff changeset
523 return -1;
716c75917a10 Disable lavf NUT until it is synced to spec
ods15
parents: 979
diff changeset
524 }
716c75917a10 Disable lavf NUT until it is synced to spec
ods15
parents: 979
diff changeset
525
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
526 nut->avf= s;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
527
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
528 nut->stream =
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
529 av_mallocz(sizeof(StreamContext)*s->nb_streams);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
530
478
ac85aafd0971 file id string
michael
parents: 468
diff changeset
531
ac85aafd0971 file id string
michael
parents: 468
diff changeset
532 put_buffer(bc, ID_STRING, strlen(ID_STRING));
ac85aafd0971 file id string
michael
parents: 468
diff changeset
533 put_byte(bc, 0);
ac85aafd0971 file id string
michael
parents: 468
diff changeset
534 nut->packet_start[2]= url_ftell(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
535
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
536 /* main header */
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
537 put_be64(bc, MAIN_STARTCODE);
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
538 put_packetheader(nut, bc, 120+5*256, 1);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
539 put_v(bc, 2); /* version */
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
540 put_v(bc, s->nb_streams);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
541 put_v(bc, MAX_DISTANCE);
478
ac85aafd0971 file id string
michael
parents: 468
diff changeset
542 put_v(bc, MAX_SHORT_DISTANCE);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
543
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
544 put_v(bc, nut->rate_num=1);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
545 put_v(bc, nut->rate_den=2);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
546 put_v(bc, nut->short_startcode=0x4EFE79);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
547
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
548 build_frame_code(s);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
549 assert(nut->frame_code['N'].flags == FLAG_INVALID);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
550
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
551 tmp_time= tmp_flags= tmp_stream= tmp_mul= tmp_size= /*tmp_res=*/ INT_MAX;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
552 for(i=0; i<256;){
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
553 tmp_fields=0;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
554 tmp_size= 0;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
555 if(tmp_time != nut->frame_code[i].timestamp_delta) tmp_fields=1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
556 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
557 if(tmp_stream != nut->frame_code[i].stream_id_plus1) tmp_fields=3;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
558 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
559 // if(tmp_res != nut->frame_code[i].res ) tmp_fields=5;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
560
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
561 tmp_time = nut->frame_code[i].timestamp_delta;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
562 tmp_flags = nut->frame_code[i].flags;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
563 tmp_stream= nut->frame_code[i].stream_id_plus1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
564 tmp_mul = nut->frame_code[i].size_mul;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
565 tmp_size = nut->frame_code[i].size_lsb;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
566 // tmp_res = nut->frame_code[i].res;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
567
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
568 for(j=0; i<256; j++,i++){
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
569 if(nut->frame_code[i].timestamp_delta != tmp_time ) break;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
570 if(nut->frame_code[i].flags != tmp_flags ) break;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
571 if(nut->frame_code[i].stream_id_plus1 != tmp_stream) break;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
572 if(nut->frame_code[i].size_mul != tmp_mul ) break;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
573 if(nut->frame_code[i].size_lsb != tmp_size+j) break;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
574 // if(nut->frame_code[i].res != tmp_res ) break;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
575 }
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
576 if(j != tmp_mul - tmp_size) tmp_fields=6;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
577
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
578 put_v(bc, tmp_flags);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
579 put_v(bc, tmp_fields);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
580 if(tmp_fields>0) put_s(bc, tmp_time);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
581 if(tmp_fields>1) put_v(bc, tmp_mul);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
582 if(tmp_fields>2) put_v(bc, tmp_stream);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
583 if(tmp_fields>3) put_v(bc, tmp_size);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
584 if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
585 if(tmp_fields>5) put_v(bc, j);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
586 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
587
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
588 update_packetheader(nut, bc, 0, 1);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
589
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
590 /* stream headers */
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
591 for (i = 0; i < s->nb_streams; i++)
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
592 {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
593 int nom, denom, ssize;
271
e35faf19f79f fixed time base and sample rate handling
al3x
parents: 270
diff changeset
594
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
595 codec = s->streams[i]->codec;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
596
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
597 put_be64(bc, STREAM_STARTCODE);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
598 put_packetheader(nut, bc, 120 + codec->extradata_size, 1);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
599 put_v(bc, i /*s->streams[i]->index*/);
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
600 switch(codec->codec_type){
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
601 case CODEC_TYPE_VIDEO: put_v(bc, 0); break;
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
602 case CODEC_TYPE_AUDIO: put_v(bc, 1); break;
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
603 // case CODEC_TYPE_TEXT : put_v(bc, 2); break;
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
604 case CODEC_TYPE_DATA : put_v(bc, 3); break;
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
605 default: return -1;
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
606 }
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
607 if (codec->codec_tag)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
608 put_vb(bc, codec->codec_tag);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
609 else if (codec->codec_type == CODEC_TYPE_VIDEO)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
610 {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
611 put_vb(bc, codec_get_bmp_tag(codec->codec_id));
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
612 }
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
613 else if (codec->codec_type == CODEC_TYPE_AUDIO)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
614 {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
615 put_vb(bc, codec_get_wav_tag(codec->codec_id));
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
616 }
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
617 else
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
618 put_vb(bc, 0);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
619
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
620 ff_parse_specific_params(codec, &nom, &ssize, &denom);
282
alex
parents: 277
diff changeset
621
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
622 nut->stream[i].rate_num= nom;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
623 nut->stream[i].rate_den= denom;
463
696f41bc8784 store index for seeking in the native timebase of each stream
michael
parents: 462
diff changeset
624 av_set_pts_info(s->streams[i], 60, denom, nom);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
625
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
626 put_v(bc, codec->bit_rate);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
627 put_vb(bc, 0); /* no language code */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
628 put_v(bc, nom);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
629 put_v(bc, denom);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
630 if(nom / denom < 1000)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
631 nut->stream[i].msb_timestamp_shift = 7;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
632 else
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
633 nut->stream[i].msb_timestamp_shift = 14;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
634 put_v(bc, nut->stream[i].msb_timestamp_shift);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
635 put_v(bc, codec->has_b_frames);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
636 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
637
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
638 if(codec->extradata_size){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
639 put_v(bc, 1);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
640 put_v(bc, codec->extradata_size);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
641 put_buffer(bc, codec->extradata, codec->extradata_size);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
642 }
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
643 put_v(bc, 0); /* end of codec specific headers */
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
644
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
645 switch(codec->codec_type)
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
646 {
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
647 case CODEC_TYPE_AUDIO:
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
648 put_v(bc, codec->sample_rate);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
649 put_v(bc, 1);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
650 put_v(bc, codec->channels);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
651 break;
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
652 case CODEC_TYPE_VIDEO:
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
653 put_v(bc, codec->width);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
654 put_v(bc, codec->height);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
655 put_v(bc, codec->sample_aspect_ratio.num);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
656 put_v(bc, codec->sample_aspect_ratio.den);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
657 put_v(bc, 0); /* csp type -- unknown */
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
658 break;
228
542e6d639bb9 fixed endianness dependancies (untested)
bellard
parents: 223
diff changeset
659 default:
542e6d639bb9 fixed endianness dependancies (untested)
bellard
parents: 223
diff changeset
660 break;
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
661 }
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
662 update_packetheader(nut, bc, 0, 1);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
663 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
664
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
665 /* info header */
223
848b612d0595 move packetheader after startcodes
al3x
parents: 222
diff changeset
666 put_be64(bc, INFO_STARTCODE);
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
667 put_packetheader(nut, bc, 30+strlen(s->author)+strlen(s->title)+
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
668 strlen(s->comment)+strlen(s->copyright)+strlen(LIBAVFORMAT_IDENT), 1);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
669 if (s->author[0])
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
670 {
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
671 put_v(bc, 9); /* type */
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
672 put_str(bc, s->author);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
673 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
674 if (s->title[0])
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
675 {
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
676 put_v(bc, 10); /* type */
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
677 put_str(bc, s->title);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
678 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
679 if (s->comment[0])
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
680 {
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
681 put_v(bc, 11); /* type */
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
682 put_str(bc, s->comment);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
683 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
684 if (s->copyright[0])
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
685 {
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
686 put_v(bc, 12); /* type */
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
687 put_str(bc, s->copyright);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
688 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
689 /* encoder */
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
690 if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)){
441
6635c1e75087 bit-exact
michael
parents: 437
diff changeset
691 put_v(bc, 13); /* type */
6635c1e75087 bit-exact
michael
parents: 437
diff changeset
692 put_str(bc, LIBAVFORMAT_IDENT);
6635c1e75087 bit-exact
michael
parents: 437
diff changeset
693 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
694
222
1da43d2bdcae some fixes
al3x
parents: 221
diff changeset
695 put_v(bc, 0); /* eof info */
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
696 update_packetheader(nut, bc, 0, 1);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
697
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
698 put_flush_packet(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
699
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
700 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
701 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
702
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
703 static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
704 {
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
705 NUTContext *nut = s->priv_data;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
706 StreamContext *stream= &nut->stream[pkt->stream_index];
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
707 ByteIOContext *bc = &s->pb;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
708 int key_frame = 0, full_pts=0;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
709 AVCodecContext *enc;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
710 int64_t coded_pts;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
711 int frame_type, best_length, frame_code, flags, i, size_mul, size_lsb, time_delta;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
712 const int64_t frame_start= url_ftell(bc);
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
713 int64_t pts= pkt->pts;
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
714 int size= pkt->size;
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
715 int stream_index= pkt->stream_index;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
716
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
717 enc = s->streams[stream_index]->codec;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
718 key_frame = !!(pkt->flags & PKT_FLAG_KEY);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
719
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
720 frame_type=0;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
721 if(frame_start + size + 20 - FFMAX(nut->packet_start[1], nut->packet_start[2]) > MAX_DISTANCE)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
722 frame_type=2;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
723 if(key_frame && !stream->last_key_frame)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
724 frame_type=2;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
725
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
726 if(frame_type>1){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
727 int64_t global_ts= av_rescale(pts, stream->rate_den*(int64_t)nut->rate_num, stream->rate_num*(int64_t)nut->rate_den);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
728 reset(s, global_ts);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
729 put_be64(bc, KEYFRAME_STARTCODE);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
730 put_v(bc, global_ts);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
731 }
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
732 assert(stream->last_pts != AV_NOPTS_VALUE);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
733 coded_pts = pts & ((1<<stream->msb_timestamp_shift)-1);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
734 if(lsb2full(stream, coded_pts) != pts)
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
735 full_pts=1;
328
137b4ce31b0f msb pts support
alex
parents: 282
diff changeset
736
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
737 if(full_pts)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
738 coded_pts= pts + (1<<stream->msb_timestamp_shift);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
739
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
740 best_length=INT_MAX;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
741 frame_code= -1;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
742 for(i=0; i<256; i++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
743 int stream_id_plus1= nut->frame_code[i].stream_id_plus1;
429
983639863758 removing keyframe prediction and checksum threshold
michael
parents: 427
diff changeset
744 int fc_key_frame;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
745 int length=0;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
746 size_mul= nut->frame_code[i].size_mul;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
747 size_lsb= nut->frame_code[i].size_lsb;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
748 time_delta= nut->frame_code[i].timestamp_delta;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
749 flags= nut->frame_code[i].flags;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
750
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
751 assert(size_mul > size_lsb);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
752
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
753 if(stream_id_plus1 == 0) length+= get_length(stream_index);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
754 else if(stream_id_plus1 - 1 != stream_index)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
755 continue;
429
983639863758 removing keyframe prediction and checksum threshold
michael
parents: 427
diff changeset
756 fc_key_frame= !!(flags & FLAG_KEY_FRAME);
983639863758 removing keyframe prediction and checksum threshold
michael
parents: 427
diff changeset
757
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
758 assert(key_frame==0 || key_frame==1);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
759 if(fc_key_frame != key_frame)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
760 continue;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
761
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
762 if(flags & FLAG_DATA_SIZE){
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
763 if(size % size_mul != size_lsb)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
764 continue;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
765 length += get_length(size / size_mul);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
766 }else if(size != size_lsb)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
767 continue;
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
768
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
769 if(full_pts && time_delta)
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
770 continue;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
771
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
772 if(!time_delta){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
773 length += get_length(coded_pts);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
774 }else{
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
775 if(time_delta != pts - stream->last_pts)
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
776 continue;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
777 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
778
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
779 if(length < best_length){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
780 best_length= length;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
781 frame_code=i;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
782 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
783 // av_log(s, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d %d %d\n", key_frame, frame_type, full_pts, size, stream_index, flags, size_mul, size_lsb, stream_id_plus1, length);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
784 }
223
848b612d0595 move packetheader after startcodes
al3x
parents: 222
diff changeset
785
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
786 assert(frame_code != -1);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
787 flags= nut->frame_code[frame_code].flags;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
788 size_mul= nut->frame_code[frame_code].size_mul;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
789 size_lsb= nut->frame_code[frame_code].size_lsb;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
790 time_delta= nut->frame_code[frame_code].timestamp_delta;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
791 #ifdef TRACE
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
792 best_length /= 7;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
793 best_length ++; //frame_code
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
794 if(frame_type==2){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
795 best_length += 8; // startcode
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
796 }
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
797 av_log(s, AV_LOG_DEBUG, "kf:%d ft:%d pt:%d fc:%2X len:%2d size:%d stream:%d flag:%d mul:%d lsb:%d s+1:%d pts_delta:%d pts:%lld fs:%lld\n", key_frame, frame_type, full_pts ? 1 : 0, frame_code, best_length, size, stream_index, flags, size_mul, size_lsb, nut->frame_code[frame_code].stream_id_plus1,(int)(pts - stream->last_pts), pts, frame_start);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
798 // av_log(s, AV_LOG_DEBUG, "%d %d %d\n", stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
799 #endif
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
800
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
801 assert(frame_type != 1); //short startcode not implemented yet
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
802 put_byte(bc, frame_code);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
803
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
804 if(nut->frame_code[frame_code].stream_id_plus1 == 0)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
805 put_v(bc, stream_index);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
806 if (!time_delta){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
807 put_v(bc, coded_pts);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
808 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
809 if(flags & FLAG_DATA_SIZE)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
810 put_v(bc, size / size_mul);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
811 else
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
812 assert(size == size_lsb);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
813 if(size > MAX_DISTANCE){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
814 assert(frame_type > 1);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
815 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
816
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 463
diff changeset
817 put_buffer(bc, pkt->data, size);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
818
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
819 update(nut, stream_index, frame_start, frame_type, frame_code, key_frame, size, pts);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
820
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
821 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
822 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
823
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
824 static int nut_write_trailer(AVFormatContext *s)
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
825 {
328
137b4ce31b0f msb pts support
alex
parents: 282
diff changeset
826 NUTContext *nut = s->priv_data;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
827 ByteIOContext *bc = &s->pb;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
828
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
829 #if 0
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
830 int i;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
831
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
832 /* WRITE INDEX */
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
833
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
834 for (i = 0; s->nb_streams; i++)
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
835 {
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
836 put_be64(bc, INDEX_STARTCODE);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
837 put_packetheader(nut, bc, 64, 1);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
838 put_v(bc, s->streams[i]->id);
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
839 put_v(bc, ...);
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
840 update_packetheader(nut, bc, 0, 1);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
841 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
842 #endif
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
843
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
844 put_flush_packet(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
845
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
846 av_freep(&nut->stream);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
847
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
848 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
849 }
858
66cc656ea404 Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents: 854
diff changeset
850 #endif //CONFIG_MUXERS
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
851
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
852 static int nut_probe(AVProbeData *p)
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
853 {
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
854 int i;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
855 uint64_t code= 0xff;
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
856
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
857 for (i = 0; i < p->buf_size; i++) {
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
858 code = (code << 8) | p->buf[i];
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
859 if (code == MAIN_STARTCODE)
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
860 return AVPROBE_SCORE_MAX;
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
861 }
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
862 return 0;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
863 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
864
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
865 static int decode_main_header(NUTContext *nut){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
866 AVFormatContext *s= nut->avf;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
867 ByteIOContext *bc = &s->pb;
220
1dbacadcd222 big/little endian fix
michaelni
parents: 219
diff changeset
868 uint64_t tmp;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
869 int i, j, tmp_stream, tmp_mul, tmp_time, tmp_size, count, tmp_res;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
870
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
871 get_packetheader(nut, bc, 1);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
872
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
873 tmp = get_v(bc);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
874 if (tmp != 2){
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
875 av_log(s, AV_LOG_ERROR, "bad version (%"PRId64")\n", tmp);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
876 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
877 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
878
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
879 nut->stream_count = get_v(bc);
639
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
880 if(nut->stream_count > MAX_STREAMS){
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
881 av_log(s, AV_LOG_ERROR, "too many streams\n");
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
882 return -1;
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
883 }
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
884 nut->max_distance = get_v(bc);
478
ac85aafd0971 file id string
michael
parents: 468
diff changeset
885 nut->max_short_distance = get_v(bc);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
886 nut->rate_num= get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
887 nut->rate_den= get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
888 nut->short_startcode= get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
889 if(nut->short_startcode>>16 != 'N'){
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
890 av_log(s, AV_LOG_ERROR, "invalid short startcode %X\n", nut->short_startcode);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
891 return -1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
892 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
893
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
894 for(i=0; i<256;){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
895 int tmp_flags = get_v(bc);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
896 int tmp_fields= get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
897 if(tmp_fields>0) tmp_time = get_s(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
898 if(tmp_fields>1) tmp_mul = get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
899 if(tmp_fields>2) tmp_stream= get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
900 if(tmp_fields>3) tmp_size = get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
901 else tmp_size = 0;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
902 if(tmp_fields>4) tmp_res = get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
903 else tmp_res = 0;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
904 if(tmp_fields>5) count = get_v(bc);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
905 else count = tmp_mul - tmp_size;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
906
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
907 while(tmp_fields-- > 6)
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
908 get_v(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
909
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
910 if(count == 0 || i+count > 256){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
911 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
912 return -1;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
913 }
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
914 if(tmp_stream > nut->stream_count + 1){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
915 av_log(s, AV_LOG_ERROR, "illegal stream number\n");
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
916 return -1;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
917 }
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
918
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
919 for(j=0; j<count; j++,i++){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
920 nut->frame_code[i].flags = tmp_flags ;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
921 nut->frame_code[i].timestamp_delta = tmp_time ;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
922 nut->frame_code[i].stream_id_plus1 = tmp_stream;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
923 nut->frame_code[i].size_mul = tmp_mul ;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
924 nut->frame_code[i].size_lsb = tmp_size+j;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
925 nut->frame_code[i].reserved_count = tmp_res ;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
926 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
927 }
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
928 if(nut->frame_code['N'].flags != FLAG_INVALID){
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
929 av_log(s, AV_LOG_ERROR, "illegal frame_code table\n");
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
930 return -1;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
931 }
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
932
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
933 if(check_checksum(bc)){
735
72f8690c3f37 Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents: 709
diff changeset
934 av_log(s, AV_LOG_ERROR, "Main header checksum mismatch\n");
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
935 return -1;
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
936 }
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
937
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
938 return 0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
939 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
940
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
941 static int decode_stream_header(NUTContext *nut){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
942 AVFormatContext *s= nut->avf;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
943 ByteIOContext *bc = &s->pb;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
944 int class, nom, denom, stream_id;
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
945 uint64_t tmp;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
946 AVStream *st;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
947
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
948 get_packetheader(nut, bc, 1);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
949 stream_id= get_v(bc);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
950 if(stream_id >= nut->stream_count || s->streams[stream_id])
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
951 return -1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
952
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
953 st = av_new_stream(s, stream_id);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
954 if (!st)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
955 return AVERROR_NOMEM;
462
b69898ffc92a move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents: 461
diff changeset
956
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
957 class = get_v(bc);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
958 tmp = get_vb(bc);
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
959 st->codec->codec_tag= tmp;
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
960 switch(class)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
961 {
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
962 case 0:
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
963 st->codec->codec_type = CODEC_TYPE_VIDEO;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
964 st->codec->codec_id = codec_get_bmp_id(tmp);
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
965 if (st->codec->codec_id == CODEC_ID_NONE)
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
966 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
967 break;
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
968 case 1:
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
969 case 32: //compatibility
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
970 st->codec->codec_type = CODEC_TYPE_AUDIO;
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
971 st->codec->codec_id = codec_get_wav_id(tmp);
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
972 if (st->codec->codec_id == CODEC_ID_NONE)
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
973 av_log(s, AV_LOG_ERROR, "Unknown codec?!\n");
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
974 break;
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
975 case 2:
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
976 // st->codec->codec_type = CODEC_TYPE_TEXT;
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
977 // break;
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
978 case 3:
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
979 st->codec->codec_type = CODEC_TYPE_DATA;
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
980 break;
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
981 default:
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
982 av_log(s, AV_LOG_ERROR, "Unknown stream class (%d)\n", class);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
983 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
984 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
985 s->bit_rate += get_v(bc);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
986 get_vb(bc); /* language code */
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
987 nom = get_v(bc);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
988 denom = get_v(bc);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
989 nut->stream[stream_id].msb_timestamp_shift = get_v(bc);
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
990 st->codec->has_b_frames=
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
991 nut->stream[stream_id].decode_delay= get_v(bc);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
992 get_byte(bc); /* flags */
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
993
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
994 /* codec specific data headers */
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
995 while(get_v(bc) != 0){
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
996 st->codec->extradata_size= get_v(bc);
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
997 if((unsigned)st->codec->extradata_size > (1<<30))
639
0b52743104ac integer overflows, heap corruption
michael
parents: 587
diff changeset
998 return -1;
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
999 st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1000 get_buffer(bc, st->codec->extradata, st->codec->extradata_size);
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
1001 // url_fskip(bc, get_v(bc));
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1002 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1003
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1004 if (st->codec->codec_type == CODEC_TYPE_VIDEO) /* VIDEO */
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1005 {
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1006 st->codec->width = get_v(bc);
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1007 st->codec->height = get_v(bc);
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1008 st->codec->sample_aspect_ratio.num= get_v(bc);
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1009 st->codec->sample_aspect_ratio.den= get_v(bc);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1010 get_v(bc); /* csp type */
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1011 }
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1012 if (st->codec->codec_type == CODEC_TYPE_AUDIO) /* AUDIO */
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1013 {
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1014 st->codec->sample_rate = get_v(bc);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1015 get_v(bc); // samplerate_den
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 798
diff changeset
1016 st->codec->channels = get_v(bc);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1017 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1018 if(check_checksum(bc)){
735
72f8690c3f37 Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents: 709
diff changeset
1019 av_log(s, AV_LOG_ERROR, "Stream header %d checksum mismatch\n", stream_id);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1020 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1021 }
463
696f41bc8784 store index for seeking in the native timebase of each stream
michael
parents: 462
diff changeset
1022 av_set_pts_info(s->streams[stream_id], 60, denom, nom);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1023 nut->stream[stream_id].rate_num= nom;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1024 nut->stream[stream_id].rate_den= denom;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1025 return 0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1026 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1027
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1028 static int decode_info_header(NUTContext *nut){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1029 AVFormatContext *s= nut->avf;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1030 ByteIOContext *bc = &s->pb;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1031
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1032 get_packetheader(nut, bc, 1);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1033
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1034 for(;;){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1035 int id= get_v(bc);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1036 char *name, *type, custom_name[256], custom_type[256];
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1037
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1038 if(!id)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1039 break;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1040 else if(id >= sizeof(info_table)/sizeof(info_table[0])){
881
91dcb9da9be6 use PRIxN, %zd, %td formats where needed
mru
parents: 858
diff changeset
1041 av_log(s, AV_LOG_ERROR, "info id is too large %d %zd\n", id, sizeof(info_table)/sizeof(info_table[0]));
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1042 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1043 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1044
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1045 type= info_table[id][1];
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1046 name= info_table[id][0];
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1047 //av_log(s, AV_LOG_DEBUG, "%d %s %s\n", id, type, name);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1048
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1049 if(!type){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1050 get_str(bc, custom_type, sizeof(custom_type));
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1051 type= custom_type;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1052 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1053 if(!name){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1054 get_str(bc, custom_name, sizeof(custom_name));
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1055 name= custom_name;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1056 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1057
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1058 if(!strcmp(type, "v")){
683
095009fc2f35 kill warnings patch by (M«©ns Rullg«©rd <mru inprovide com>)
michael
parents: 682
diff changeset
1059 get_v(bc);
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1060 }else{
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1061 if(!strcmp(name, "Author"))
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1062 get_str(bc, s->author, sizeof(s->author));
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1063 else if(!strcmp(name, "Title"))
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1064 get_str(bc, s->title, sizeof(s->title));
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1065 else if(!strcmp(name, "Copyright"))
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1066 get_str(bc, s->copyright, sizeof(s->copyright));
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1067 else if(!strcmp(name, "Description"))
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1068 get_str(bc, s->comment, sizeof(s->comment));
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1069 else
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1070 get_str(bc, NULL, 0);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1071 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1072 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1073 if(check_checksum(bc)){
735
72f8690c3f37 Ministry of English Composition, reporting for duty (and the word is "skipped", not "skiped"; "skiped" would rhyme with "hyped")
melanson
parents: 709
diff changeset
1074 av_log(s, AV_LOG_ERROR, "Info header checksum mismatch\n");
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1075 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1076 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1077 return 0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1078 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1079
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1080 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1081 {
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1082 NUTContext *nut = s->priv_data;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1083 ByteIOContext *bc = &s->pb;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1084 int64_t pos;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1085 int inited_stream_count;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1086
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1087 nut->avf= s;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1088
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1089 /* main header */
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1090 pos=0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1091 for(;;){
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1092 pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1093 if (pos<0){
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1094 av_log(s, AV_LOG_ERROR, "no main startcode found\n");
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1095 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1096 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1097 if(decode_main_header(nut) >= 0)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1098 break;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1099 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1100
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1101
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1102 s->bit_rate = 0;
328
137b4ce31b0f msb pts support
alex
parents: 282
diff changeset
1103
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1104 nut->stream = av_malloc(sizeof(StreamContext)*nut->stream_count);
272
13cbfe47f085 fixed codec specific data headers
al3x
parents: 271
diff changeset
1105
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1106 /* stream headers */
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1107 pos=0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1108 for(inited_stream_count=0; inited_stream_count < nut->stream_count;){
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1109 pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
782
56c96e647ce4 fix stream class
michael
parents: 775
diff changeset
1110 if (pos<0+1){
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1111 av_log(s, AV_LOG_ERROR, "not all stream headers found\n");
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
1112 return -1;
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 416
diff changeset
1113 }
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1114 if(decode_stream_header(nut) >= 0)
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1115 inited_stream_count++;
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
1116 }
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
1117
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1118 /* info headers */
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1119 pos=0;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1120 for(;;){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1121 uint64_t startcode= find_any_startcode(bc, pos);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1122 pos= url_ftell(bc);
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
1123
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1124 if(startcode==0){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1125 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1126 return -1;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1127 }else if(startcode == KEYFRAME_STARTCODE){
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1128 nut->next_startcode= startcode;
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1129 break;
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1130 }else if(startcode != INFO_STARTCODE){
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1131 continue;
415
badb7a946a9c info packet read/write & simplify
michael
parents: 414
diff changeset
1132 }
419
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1133
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1134 decode_info_header(nut);
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1135 }
51c25922a543 search for undamaged headers
michael
parents: 418
diff changeset
1136
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1137 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1138 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1139
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1140 static int decode_frame_header(NUTContext *nut, int *key_frame_ret, int64_t *pts_ret, int *stream_id_ret, int frame_code, int frame_type, int64_t frame_start){
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1141 AVFormatContext *s= nut->avf;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1142 StreamContext *stream;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1143 ByteIOContext *bc = &s->pb;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1144 int size, flags, size_mul, size_lsb, stream_id, time_delta;
328
137b4ce31b0f msb pts support
alex
parents: 282
diff changeset
1145 int64_t pts = 0;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1146
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1147 if(frame_type < 2 && frame_start - nut->packet_start[2] > nut->max_distance){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1148 av_log(s, AV_LOG_ERROR, "last frame must have been damaged\n");
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1149 return -1;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1150 }
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1151
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1152 if(frame_type)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1153 nut->packet_start[ frame_type ]= frame_start; //otherwise 1 goto 1 may happen
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1154
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1155 flags= nut->frame_code[frame_code].flags;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1156 size_mul= nut->frame_code[frame_code].size_mul;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1157 size_lsb= nut->frame_code[frame_code].size_lsb;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1158 stream_id= nut->frame_code[frame_code].stream_id_plus1 - 1;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1159 time_delta= nut->frame_code[frame_code].timestamp_delta;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1160
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1161 if(stream_id==-1)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1162 stream_id= get_v(bc);
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1163 if(stream_id >= s->nb_streams){
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1164 av_log(s, AV_LOG_ERROR, "illegal stream_id\n");
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1165 return -1;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1166 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1167 stream= &nut->stream[stream_id];
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1168
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1169 // av_log(s, AV_LOG_DEBUG, "ft:%d ppts:%d %d %d\n", frame_type, stream->lru_pts_delta[0], stream->lru_pts_delta[1], stream->lru_pts_delta[2]);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1170
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1171 *key_frame_ret= !!(flags & FLAG_KEY_FRAME);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1172
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1173 if(!time_delta){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1174 int64_t mask = (1<<stream->msb_timestamp_shift)-1;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1175 pts= get_v(bc);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1176 if(pts > mask){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1177 pts -= mask+1;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1178 }else{
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1179 if(stream->last_pts == AV_NOPTS_VALUE){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1180 av_log(s, AV_LOG_ERROR, "no reference pts available\n");
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1181 return -1;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1182 }
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1183 pts= lsb2full(stream, pts);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1184 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1185 }else{
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1186 if(stream->last_pts == AV_NOPTS_VALUE){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1187 av_log(s, AV_LOG_ERROR, "no reference pts available\n");
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1188 return -1;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1189 }
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1190 pts= stream->last_pts + time_delta;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1191 }
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1192
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1193 if(*key_frame_ret){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1194 // av_log(s, AV_LOG_DEBUG, "stream:%d start:%lld pts:%lld length:%lld\n",stream_id, frame_start, av_pts, frame_start - nut->stream[stream_id].last_sync_pos);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1195 av_add_index_entry(
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1196 s->streams[stream_id],
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1197 frame_start,
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1198 pts,
979
d2e5dfdf4def add size to AVIndex
michael
parents: 902
diff changeset
1199 0,
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1200 frame_start - nut->stream[stream_id].last_sync_pos,
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1201 AVINDEX_KEYFRAME);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1202 nut->stream[stream_id].last_sync_pos= frame_start;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1203 // assert(nut->packet_start == frame_start);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1204 }
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1205
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1206 assert(size_mul > size_lsb);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1207 size= size_lsb;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1208 if(flags & FLAG_DATA_SIZE)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1209 size+= size_mul*get_v(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1210
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1211 #ifdef TRACE
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1212 av_log(s, AV_LOG_DEBUG, "fs:%lld fc:%d ft:%d kf:%d pts:%lld size:%d mul:%d lsb:%d flags:%d delta:%d\n", frame_start, frame_code, frame_type, *key_frame_ret, pts, size, size_mul, size_lsb, flags, time_delta);
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1213 #endif
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1214
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1215 if(frame_type==0 && url_ftell(bc) - nut->packet_start[2] + size > nut->max_distance){
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1216 av_log(s, AV_LOG_ERROR, "frame size too large\n");
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1217 return -1;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1218 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1219
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1220 *stream_id_ret = stream_id;
463
696f41bc8784 store index for seeking in the native timebase of each stream
michael
parents: 462
diff changeset
1221 *pts_ret = pts;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1222
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1223 update(nut, stream_id, frame_start, frame_type, frame_code, *key_frame_ret, size, pts);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1224
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1225 return size;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1226 }
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1227
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1228 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code, int frame_type, int64_t frame_start){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1229 AVFormatContext *s= nut->avf;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1230 ByteIOContext *bc = &s->pb;
708
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1231 int size, stream_id, key_frame, discard;
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1232 int64_t pts, last_IP_pts;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1233
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1234 size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, frame_start);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1235 if(size < 0)
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1236 return -1;
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1237
708
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1238 discard= s->streams[ stream_id ]->discard;
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1239 last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1240 if( (discard >= AVDISCARD_NONKEY && !key_frame)
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1241 ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
d79164865a7c more fine grained discarding of packets
michael
parents: 699
diff changeset
1242 || discard >= AVDISCARD_ALL){
652
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1243 url_fskip(bc, size);
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1244 return 1;
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1245 }
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1246
775
c5077fdab490 AVPacket.pos
michael
parents: 743
diff changeset
1247 av_get_packet(bc, pkt, size);
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1248 pkt->stream_index = stream_id;
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1249 if (key_frame)
887
d70e50f1495f COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 885
diff changeset
1250 pkt->flags |= PKT_FLAG_KEY;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1251 pkt->pts = pts;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1252
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1253 return 0;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1254 }
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1255
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1256 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1257 {
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1258 NUTContext *nut = s->priv_data;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1259 ByteIOContext *bc = &s->pb;
652
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1260 int i, frame_code=0, ret;
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1261
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1262 for(;;){
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1263 int64_t pos= url_ftell(bc);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1264 int frame_type= 0;
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1265 uint64_t tmp= nut->next_startcode;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1266 nut->next_startcode=0;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1267
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1268 if (url_feof(bc))
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1269 return -1;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1270
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1271 if(tmp){
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1272 pos-=8;
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1273 }else{
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1274 frame_code = get_byte(bc);
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1275 if(frame_code == 'N'){
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1276 tmp= frame_code;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1277 for(i=1; i<8; i++)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1278 tmp = (tmp<<8) + get_byte(bc);
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1279 }
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1280 }
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1281 switch(tmp){
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1282 case MAIN_STARTCODE:
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1283 case STREAM_STARTCODE:
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1284 case INDEX_STARTCODE:
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1285 get_packetheader(nut, bc, 0);
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1286 assert(nut->packet_start[2] == pos);
853
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
1287 url_fseek(bc, nut->written_packet_size, SEEK_CUR);
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1288 break;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1289 case INFO_STARTCODE:
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1290 if(decode_info_header(nut)<0)
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1291 goto resync;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1292 break;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1293 case KEYFRAME_STARTCODE:
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1294 frame_type = 2;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1295 reset(s, get_v(bc));
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1296 frame_code = get_byte(bc);
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1297 case 0:
652
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1298 ret= decode_frame(nut, pkt, frame_code, frame_type, pos);
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1299 if(ret==0)
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1300 return 0;
652
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1301 else if(ret==1) //ok but discard packet
b47948262721 support discarding uninterresting packets
michael
parents: 639
diff changeset
1302 break;
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1303 default:
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1304 resync:
881
91dcb9da9be6 use PRIxN, %zd, %td formats where needed
mru
parents: 858
diff changeset
1305 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", nut->packet_start[2]+1);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1306 tmp= find_any_startcode(bc, nut->packet_start[2]+1);
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1307 if(tmp==0)
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1308 return -1;
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1309 av_log(s, AV_LOG_DEBUG, "sync\n");
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1310 nut->next_startcode= tmp;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1311 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1312 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1313 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1314
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1315 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1316 NUTContext *nut = s->priv_data;
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1317 StreamContext *stream;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1318 ByteIOContext *bc = &s->pb;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1319 int64_t pos, pts;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1320 uint64_t code;
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1321 int frame_code,step, stream_id, i,size, key_frame;
881
91dcb9da9be6 use PRIxN, %zd, %td formats where needed
mru
parents: 858
diff changeset
1322 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1323
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1324 if(*pos_arg < 0)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1325 return AV_NOPTS_VALUE;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1326
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1327 pos= *pos_arg;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1328 step= FFMIN(16*1024, pos);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1329 do{
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1330 pos-= step;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1331 code= find_any_startcode(bc, pos);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1332
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1333 if(code && url_ftell(bc) - 8 <= *pos_arg)
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1334 break;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1335 step= FFMIN(2*step, pos);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1336 }while(step);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1337
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1338 if(!code) //nothing found, not even after pos_arg
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1339 return AV_NOPTS_VALUE;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1340
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1341 url_fseek(bc, -8, SEEK_CUR);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1342 for(i=0; i<s->nb_streams; i++)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1343 nut->stream[i].last_sync_pos= url_ftell(bc);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1344
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1345 for(;;){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1346 int frame_type=0;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1347 int64_t pos= url_ftell(bc);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1348 uint64_t tmp=0;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1349
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1350 if(pos > pos_limit || url_feof(bc))
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1351 return AV_NOPTS_VALUE;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1352
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1353 frame_code = get_byte(bc);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1354 if(frame_code == 'N'){
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1355 tmp= frame_code;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1356 for(i=1; i<8; i++)
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1357 tmp = (tmp<<8) + get_byte(bc);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1358 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1359 //av_log(s, AV_LOG_DEBUG, "before switch %llX at=%lld\n", tmp, pos);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1360
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1361 switch(tmp){
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1362 case MAIN_STARTCODE:
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1363 case STREAM_STARTCODE:
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1364 case INDEX_STARTCODE:
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1365 case INFO_STARTCODE:
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1366 get_packetheader(nut, bc, 0);
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1367 assert(nut->packet_start[2]==pos);
853
00db24c46875 forward_ptr doesnt contain its own length anymore
michael
parents: 820
diff changeset
1368 url_fseek(bc, nut->written_packet_size, SEEK_CUR);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1369 break;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1370 case KEYFRAME_STARTCODE:
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1371 frame_type=2;
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1372 reset(s, get_v(bc));
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1373 frame_code = get_byte(bc);
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1374 case 0:
461
63540e5504f7 bytestream tracing support
michael
parents: 456
diff changeset
1375 size= decode_frame_header(nut, &key_frame, &pts, &stream_id, frame_code, frame_type, pos);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1376 if(size < 0)
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1377 goto resync;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1378
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1379 stream= &nut->stream[stream_id];
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1380 if(stream_id != stream_index || !key_frame || pos < *pos_arg){
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1381 url_fseek(bc, size, SEEK_CUR);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1382 break;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1383 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1384
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1385 *pos_arg= pos;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1386 return pts;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1387 default:
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1388 resync:
881
91dcb9da9be6 use PRIxN, %zd, %td formats where needed
mru
parents: 858
diff changeset
1389 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", nut->packet_start[2]+1);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1390 if(!find_any_startcode(bc, nut->packet_start[2]+1))
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1391 return AV_NOPTS_VALUE;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1392
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1393 url_fseek(bc, -8, SEEK_CUR);
421
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1394 }
a35263593560 10l (bytes_left() -> url_feof())
michael
parents: 419
diff changeset
1395 }
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1396 return AV_NOPTS_VALUE;
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1397 }
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1398
555
0a997108f384 use native timebase for seeking
michael
parents: 478
diff changeset
1399 static int nut_read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1400 // NUTContext *nut = s->priv_data;
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1401 int64_t pos;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1402
555
0a997108f384 use native timebase for seeking
michael
parents: 478
diff changeset
1403 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1404 return -1;
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1405
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1406 pos= url_ftell(&s->pb);
456
c13fd248e222 match latest spec
michael
parents: 442
diff changeset
1407 nut_read_timestamp(s, stream_index, &pos, pos-1);
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1408
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1409 return 0;
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1410 }
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1411
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1412 static int nut_read_close(AVFormatContext *s)
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1413 {
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1414 NUTContext *nut = s->priv_data;
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1415
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1416 av_freep(&nut->stream);
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1417
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1418 return 0;
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1419 }
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1420
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
1421 #ifdef CONFIG_NUT_DEMUXER
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
1422 AVInputFormat nut_demuxer = {
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1423 "nut",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1424 "nut format",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1425 sizeof(NUTContext),
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1426 nut_probe,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1427 nut_read_header,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1428 nut_read_packet,
403
967c6bb2b8a6 nut (de)muxer update
michael
parents: 370
diff changeset
1429 nut_read_close,
426
652aaf869c7b fourcc & language code v->vb type
michael
parents: 421
diff changeset
1430 nut_read_seek,
437
50bae308f71e moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents: 432
diff changeset
1431 nut_read_timestamp,
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1432 .extensions = "nut",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1433 };
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
1434 #endif
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
1435 #ifdef CONFIG_NUT_MUXER
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
1436 AVOutputFormat nut_muxer = {
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1437 "nut",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1438 "nut format",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1439 "video/x-nut",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1440 "nut",
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1441 sizeof(NUTContext),
682
decfffe703bc 01-makefile_fix_updated.patch
michael
parents: 652
diff changeset
1442 #ifdef CONFIG_LIBVORBIS
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1443 CODEC_ID_VORBIS,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1444 #elif defined(CONFIG_MP3LAME)
232
eb90c0a5a1ba CODEC_ID_MP3LAME is obsolete
bellard
parents: 228
diff changeset
1445 CODEC_ID_MP3,
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1446 #else
223
848b612d0595 move packetheader after startcodes
al3x
parents: 222
diff changeset
1447 CODEC_ID_MP2, /* AC3 needs liba52 decoder */
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1448 #endif
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1449 CODEC_ID_MPEG4,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1450 nut_write_header,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1451 nut_write_packet,
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1452 nut_write_trailer,
798
b54823a20627 nut prefers extradata style global headers
michael
parents: 787
diff changeset
1453 .flags = AVFMT_GLOBALHEADER,
219
2f16e3066399 initial nut muxer and demuxer (demuxer is not fail safe)
al3x
parents:
diff changeset
1454 };
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
1455 #endif