annotate nutenc.c @ 2324:919efc8760a0 libavformat

trying to finally get the nut muxer back uptodate this one only writes the framecode table and mainheader though they should be compliant to the current spec
author michael
date Thu, 09 Aug 2007 01:12:48 +0000
parents
children 2829faf72879
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2324
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
1 /*
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
2 * nut muxer
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
3 * Copyright (c) 2004-2007 Michael Niedermayer
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
4 *
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
5 * This file is part of FFmpeg.
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
6 *
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
11 *
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
15 * Lesser General Public License for more details.
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
16 *
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
20 */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
21
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
22 #include "nut.h"
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
23
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
24 #define TRACE
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
25
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
26 static void build_frame_code(AVFormatContext *s){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
27 NUTContext *nut = s->priv_data;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
28 int key_frame, index, pred, stream_id;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
29 int start=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
30 int end= 254;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
31 int keyframe_0_esc= s->nb_streams > 2;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
32 int pred_table[10];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
33
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
34 if(keyframe_0_esc){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
35 /* keyframe = 0 escape */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
36 FrameCode *ft= &nut->frame_code[start];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
37 ft->flags= FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
38 ft->size_mul=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
39 start++;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
40 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
41
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
42 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
43 int start2= start + (end-start)*stream_id / s->nb_streams;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
44 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
45 AVCodecContext *codec = s->streams[stream_id]->codec;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
46 int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
47 int intra_only= /*codec->intra_only || */is_audio;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
48 int pred_count;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
49
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
50 for(key_frame=0; key_frame<2; key_frame++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
51 if(intra_only && keyframe_0_esc && key_frame==0)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
52 continue;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
53
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
54 {
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
55 FrameCode *ft= &nut->frame_code[start2];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
56 ft->flags= FLAG_KEY*key_frame;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
57 ft->flags|= FLAG_SIZE_MSB | FLAG_CODED_PTS;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
58 ft->stream_id= stream_id;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
59 ft->size_mul=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
60 start2++;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
61 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
62 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
63
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
64 key_frame= intra_only;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
65 #if 1
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
66 if(is_audio){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
67 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
68 int pts;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
69 for(pts=0; pts<2; pts++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
70 for(pred=0; pred<2; pred++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
71 FrameCode *ft= &nut->frame_code[start2];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
72 ft->flags= FLAG_KEY*key_frame;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
73 ft->stream_id= stream_id;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
74 ft->size_mul=frame_bytes + 2;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
75 ft->size_lsb=frame_bytes + pred;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
76 ft->pts_delta=pts;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
77 start2++;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
78 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
79 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
80 }else{
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
81 FrameCode *ft= &nut->frame_code[start2];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
82 ft->flags= FLAG_KEY | FLAG_SIZE_MSB;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
83 ft->stream_id= stream_id;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
84 ft->size_mul=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
85 ft->pts_delta=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
86 start2++;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
87 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
88 #endif
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
89
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
90 if(codec->has_b_frames){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
91 pred_count=5;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
92 pred_table[0]=-2;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
93 pred_table[1]=-1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
94 pred_table[2]=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
95 pred_table[3]=3;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
96 pred_table[4]=4;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
97 }else if(codec->codec_id == CODEC_ID_VORBIS){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
98 pred_count=3;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
99 pred_table[0]=2;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
100 pred_table[1]=9;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
101 pred_table[2]=16;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
102 }else{
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
103 pred_count=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
104 pred_table[0]=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
105 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
106
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
107 for(pred=0; pred<pred_count; pred++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
108 int start3= start2 + (end2-start2)*pred / pred_count;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
109 int end3 = start2 + (end2-start2)*(pred+1) / pred_count;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
110
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
111 for(index=start3; index<end3; index++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
112 FrameCode *ft= &nut->frame_code[index];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
113 ft->flags= FLAG_KEY*key_frame;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
114 ft->flags|= FLAG_SIZE_MSB;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
115 ft->stream_id= stream_id;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
116 //FIXME use single byte size and pred from last
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
117 ft->size_mul= end3-start3;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
118 ft->size_lsb= index - start3;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
119 ft->pts_delta= pred_table[pred];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
120 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
121 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
122 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
123 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
124 nut->frame_code[ 0].flags=
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
125 nut->frame_code[255].flags=
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
126 nut->frame_code['N'].flags= FLAG_INVALID;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
127 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
128
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
129 /**
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
130 * Gets the length in bytes which is needed to store val as v.
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
131 */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
132 static int get_length(uint64_t val){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
133 int i=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
134
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
135 while(val>>=7)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
136 i++;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
137
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
138 return i;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
139 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
140
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
141 static void put_v(ByteIOContext *bc, uint64_t val){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
142 int i= get_length(val);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
143
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
144 while(--i>0)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
145 put_byte(bc, 128 | (val>>(7*i)));
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
146
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
147 put_byte(bc, val&127);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
148 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
149
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
150 /**
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
151 * stores a string as vb.
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
152 */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
153 static void put_str(ByteIOContext *bc, const char *string){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
154 int len= strlen(string);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
155
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
156 put_v(bc, len);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
157 put_buffer(bc, string, len);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
158 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
159
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
160 static void put_s(ByteIOContext *bc, int64_t val){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
161 put_v(bc, 2*FFABS(val) - (val>0));
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
162 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
163
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
164 #ifdef TRACE
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
165 static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
166 printf("get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
167
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
168 put_v(bc, v);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
169 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
170
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
171 static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
172 printf("get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
173
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
174 put_s(bc, v);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
175 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
176 #define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
177 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
178 #endif
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
179
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
180 static int put_packetheader(NUTContext *nut, ByteIOContext *bc, int max_size, int calculate_checksum){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
181 put_flush_packet(bc);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
182 nut->packet_start= url_ftell(bc) - 8;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
183 nut->written_packet_size = max_size;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
184
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
185 /* packet header */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
186 put_v(bc, nut->written_packet_size); /* forward ptr */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
187
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
188 if(calculate_checksum)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
189 init_checksum(bc, av_crc04C11DB7_update, 0);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
190
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
191 return 0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
192 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
193
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
194 /**
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
195 *
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
196 * must not be called more then once per packet
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
197 */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
198 static int update_packetheader(NUTContext *nut, ByteIOContext *bc, int additional_size, int calculate_checksum){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
199 int64_t start= nut->packet_start;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
200 int64_t cur= url_ftell(bc);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
201 int size= cur - start - get_length(nut->written_packet_size) - 8;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
202
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
203 if(calculate_checksum)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
204 size += 4;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
205
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
206 if(size != nut->written_packet_size){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
207 int i;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
208
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
209 assert( size <= nut->written_packet_size );
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
210
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
211 url_fseek(bc, start + 8, SEEK_SET);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
212 for(i=get_length(size); i < get_length(nut->written_packet_size); i++)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
213 put_byte(bc, 128);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
214 put_v(bc, size);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
215
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
216 url_fseek(bc, cur, SEEK_SET);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
217 nut->written_packet_size= size; //FIXME may fail if multiple updates with differing sizes, as get_length may differ
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
218
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
219 if(calculate_checksum)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
220 put_le32(bc, get_checksum(bc));
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
221 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
222
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
223 return 0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
224 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
225
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
226 static int write_header(AVFormatContext *s){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
227 NUTContext *nut = s->priv_data;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
228 ByteIOContext *bc = &s->pb;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
229 AVCodecContext *codec;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
230 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
231
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
232 nut->avf= s;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
233
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
234 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
235 nut->time_base= av_mallocz(sizeof(AVRational )*s->nb_streams);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
236
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
237 for(i=0; i<s->nb_streams; i++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
238 AVStream *st= s->streams[i];
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
239 int num, denom, ssize;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
240 ff_parse_specific_params(st->codec, &num, &ssize, &denom);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
241
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
242 nut->stream[i].time_base= (AVRational){denom, num};
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
243
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
244 av_set_pts_info(st, 64, denom, num);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
245
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
246 for(j=0; j<nut->time_base_count; j++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
247 if(!memcmp(&nut->stream[i].time_base, &nut->time_base[j], sizeof(AVRational))){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
248 break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
249 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
250 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
251 nut->time_base[j]= nut->stream[i].time_base;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
252 if(j==nut->time_base_count)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
253 nut->time_base_count++;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
254 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
255 //FIXME make nut->stream[i].time_base pointers into nut->time_base
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
256
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
257 put_buffer(bc, ID_STRING, strlen(ID_STRING));
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
258 put_byte(bc, 0);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
259
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
260 /* main header */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
261 put_be64(bc, MAIN_STARTCODE);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
262 put_packetheader(nut, bc, 120+5*256/*FIXME check*/, 1);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
263
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
264 put_v(bc, 2); /* version */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
265 put_v(bc, s->nb_streams);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
266 put_v(bc, MAX_DISTANCE);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
267 put_v(bc, nut->time_base_count);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
268
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
269 for(i=0; i<nut->time_base_count; i++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
270 put_v(bc, nut->time_base[i].num);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
271 put_v(bc, nut->time_base[i].den);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
272 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
273
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
274 build_frame_code(s);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
275 assert(nut->frame_code['N'].flags == FLAG_INVALID);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
276
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
277 tmp_pts=0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
278 tmp_mul=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
279 tmp_stream=0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
280 for(i=0; i<256;){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
281 tmp_fields=0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
282 tmp_size=0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
283 // tmp_res=0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
284 if(tmp_pts != nut->frame_code[i].pts_delta) tmp_fields=1;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
285 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
286 if(tmp_stream != nut->frame_code[i].stream_id) tmp_fields=3;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
287 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
288 // if(tmp_res != nut->frame_code[i].res ) tmp_fields=5;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
289
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
290 tmp_pts = nut->frame_code[i].pts_delta;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
291 tmp_flags = nut->frame_code[i].flags;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
292 tmp_stream= nut->frame_code[i].stream_id;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
293 tmp_mul = nut->frame_code[i].size_mul;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
294 tmp_size = nut->frame_code[i].size_lsb;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
295 // tmp_res = nut->frame_code[i].res;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
296
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
297 for(j=0; i<256; j++,i++){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
298 if(i == 'N'){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
299 j--;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
300 continue;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
301 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
302 if(nut->frame_code[i].pts_delta != tmp_pts ) break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
303 if(nut->frame_code[i].flags != tmp_flags ) break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
304 if(nut->frame_code[i].stream_id != tmp_stream) break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
305 if(nut->frame_code[i].size_mul != tmp_mul ) break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
306 if(nut->frame_code[i].size_lsb != tmp_size+j) break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
307 // if(nut->frame_code[i].res != tmp_res ) break;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
308 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
309 if(j != tmp_mul - tmp_size) tmp_fields=6;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
310
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
311 put_v(bc, tmp_flags);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
312 put_v(bc, tmp_fields);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
313 if(tmp_fields>0) put_s(bc, tmp_pts);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
314 if(tmp_fields>1) put_v(bc, tmp_mul);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
315 if(tmp_fields>2) put_v(bc, tmp_stream);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
316 if(tmp_fields>3) put_v(bc, tmp_size);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
317 if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
318 if(tmp_fields>5) put_v(bc, j);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
319 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
320
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
321 update_packetheader(nut, bc, 0, 1);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
322
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
323 put_flush_packet(bc);
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
324
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
325 //FIXME stream header, ...
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
326
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
327 return 0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
328 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
329
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
330 static int write_packet(AVFormatContext *s, AVPacket *pkt){
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
331 //FIXME
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
332 return 0;
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
333 }
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
334
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
335 AVOutputFormat nut_muxer = {
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
336 "nut",
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
337 "nut format",
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
338 "video/x-nut",
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
339 "nut",
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
340 sizeof(NUTContext),
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
341 #ifdef CONFIG_LIBVORBIS
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
342 CODEC_ID_VORBIS,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
343 #elif defined(CONFIG_LIBMP3LAME)
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
344 CODEC_ID_MP3,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
345 #else
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
346 CODEC_ID_MP2, /* AC3 needs liba52 decoder */
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
347 #endif
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
348 CODEC_ID_MPEG4,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
349 write_header,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
350 write_packet,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
351 // write_trailer,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
352 .flags = AVFMT_GLOBALHEADER,
919efc8760a0 trying to finally get the nut muxer back uptodate
michael
parents:
diff changeset
353 };