1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* $Id: muxogm.c,v 1.4 2005/02/20 00:41:56 titer Exp $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.m0k.org/>.
It may be used under the terms of the GNU General Public License. */
#include "hb.h"
#include <ogg/ogg.h>
struct hb_mux_object_s
{
HB_MUX_COMMON;
hb_job_t * job;
FILE * file;
};
struct hb_mux_data_s
{
int codec;
ogg_stream_state os;
int i_packet_no;
};
typedef struct __attribute__((__packed__))
{
uint8_t i_packet_type;
char stream_type[8];
char sub_type[4];
int32_t i_size;
int64_t i_time_unit;
int64_t i_samples_per_unit;
int32_t i_default_len;
int32_t i_buffer_size;
int16_t i_bits_per_sample;
int16_t i_padding_0; // hum hum
union
{
struct
{
int32_t i_width;
int32_t i_height;
} video;
struct
{
int16_t i_channels;
int16_t i_block_align;
int32_t i_avgbytespersec;
} audio;
} header;
} ogg_stream_header_t;
#define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
static void _SetWLE( uint8_t *p, uint16_t i_dw )
{
p[1] = ( i_dw >> 8 )&0xff;
p[0] = ( i_dw )&0xff;
}
#define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
static void _SetDWLE( uint8_t *p, uint32_t i_dw )
{
p[3] = ( i_dw >> 24 )&0xff;
p[2] = ( i_dw >> 16 )&0xff;
p[1] = ( i_dw >> 8 )&0xff;
p[0] = ( i_dw )&0xff;
}
#define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
static void _SetQWLE( uint8_t *p, uint64_t i_qw )
{
SetDWLE( p, i_qw&0xffffffff );
SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
}
static int OGMFlush( hb_mux_object_t * m, hb_mux_data_t * mux_data )
{
for( ;; )
{
ogg_page og;
if( ogg_stream_flush( &mux_data->os, &og ) == 0 )
{
break;
}
if( fwrite( og.header, og.header_len, 1, m->file ) <= 0 ||
fwrite( og.body, og.body_len, 1, m->file ) <= 0 )
{
return -1;
}
}
return 0;
}
/**********************************************************************
* OGMInit
**********************************************************************
* Allocates hb_mux_data_t structures, create file and write headers
*********************************************************************/
static int OGMInit( hb_mux_object_t * m )
{
hb_job_t * job = m->job;
hb_title_t * title = job->title;
hb_audio_t * audio;
hb_mux_data_t * mux_data;
int i;
ogg_packet op;
ogg_stream_header_t h;
/* Open output file */
if( ( m->file = fopen( job->file, "wb" ) ) == NULL )
{
hb_log( "muxogm: failed to open `%s'", job->file );
return -1;
}
hb_log( "muxogm: `%s' opened", job->file );
/* Video track */
mux_data = malloc( sizeof( hb_mux_data_t ) );
mux_data->codec = job->vcodec;
mux_data->i_packet_no = 0;
job->mux_data = mux_data;
ogg_stream_init( &mux_data->os, 0 );
/* Audio */
for( i = 0; i < hb_list_count( title->list_audio ); i++ )
{
audio = hb_list_item( title->list_audio, i );
mux_data = malloc( sizeof( hb_mux_data_t ) );
mux_data->codec = job->acodec;
mux_data->i_packet_no = 0;
audio->mux_data = mux_data;
ogg_stream_init( &mux_data->os, i + 1 );
}
/* First pass: all b_o_s packets */
/* Video */
mux_data = job->mux_data;
memset( &h, 0, sizeof( ogg_stream_header_t ) );
h.i_packet_type = 0x01;
memcpy( h.stream_type, "video ", 8 );
if( mux_data->codec == HB_VCODEC_X264 )
{
memcpy( h.sub_type, "H264", 4 );
}
else if( mux_data->codec == HB_VCODEC_XVID )
{
memcpy( h.sub_type, "XVID", 4 );
}
else
{
memcpy( h.sub_type, "DX50", 4 );
}
SetDWLE( &h.i_size, sizeof( ogg_stream_header_t ) - 1);
SetQWLE( &h.i_time_unit, (int64_t) 10 * 1000 * 1000 *
(int64_t) job->vrate_base / (int64_t) job->vrate );
SetQWLE( &h.i_samples_per_unit, 1 );
SetDWLE( &h.i_default_len, 0 );
SetDWLE( &h.i_buffer_size, 1024*1024 );
SetWLE ( &h.i_bits_per_sample, 0 );
SetDWLE( &h.header.video.i_width, job->width );
SetDWLE( &h.header.video.i_height, job->height );
op.packet = (unsigned char*)&h;
op.bytes = sizeof( ogg_stream_header_t );
op.b_o_s = 1;
op.e_o_s = 0;
op.granulepos = 0;
op.packetno = mux_data->i_packet_no++;
ogg_stream_packetin( &mux_data->os, &op );
OGMFlush( m, mux_data );
/* Audio */
for( i = 0; i < hb_list_count( title->list_audio ); i++ )
{
audio = hb_list_item( title->list_audio, i );
mux_data = audio->mux_data;
memset( &h, 0, sizeof( ogg_stream_header_t ) );
switch( job->acodec )
{
case HB_ACODEC_LAME:
{
h.i_packet_type = 0x01;
memcpy( h.stream_type, "audio ", 8 );
memcpy( h.sub_type, "55 ", 4 );
SetDWLE( &h.i_size, sizeof( ogg_stream_header_t ) - 1);
SetQWLE( &h.i_time_unit, 0 );
SetQWLE( &h.i_samples_per_unit, job->arate );
SetDWLE( &h.i_default_len, 1 );
SetDWLE( &h.i_buffer_size, 30 * 1024 );
SetWLE ( &h.i_bits_per_sample, 0 );
SetDWLE( &h.header.audio.i_channels, 2 );
SetDWLE( &h.header.audio.i_block_align, 0 );
SetDWLE( &h.header.audio.i_avgbytespersec,
job->abitrate / 8 );
op.packet = (char*) &h;
op.bytes = sizeof( ogg_stream_header_t );
op.b_o_s = 1;
op.e_o_s = 0;
op.granulepos = 0;
op.packetno = mux_data->i_packet_no++;
ogg_stream_packetin( &mux_data->os, &op );
break;
}
case HB_ACODEC_VORBIS:
{
memcpy( &op, audio->config.vorbis.headers[0],
sizeof( ogg_packet ) );
op.packet = audio->config.vorbis.headers[0] +
sizeof( ogg_packet );
ogg_stream_packetin( &mux_data->os, &op );
break;
}
default:
hb_log( "muxogm: unhandled codec" );
break;
}
OGMFlush( m, mux_data );
}
/* second pass: all non b_o_s packets */
for( i = 0; i < hb_list_count( title->list_audio ); i++ )
{
audio = hb_list_item( title->list_audio, i );
if( job->acodec == HB_ACODEC_VORBIS )
{
int j;
mux_data = audio->mux_data;
for( j = 1; j < 3; j++ )
{
memcpy( &op, audio->config.vorbis.headers[j],
sizeof( ogg_packet ) );
op.packet = audio->config.vorbis.headers[j] +
sizeof( ogg_packet );
ogg_stream_packetin( &mux_data->os, &op );
OGMFlush( m, mux_data );
}
}
}
hb_log( "muxogm: headers written" );
return 0;
}
static int OGMMux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
hb_buffer_t * buf )
{
ogg_packet op;
switch( mux_data->codec )
{
case HB_VCODEC_FFMPEG:
case HB_VCODEC_XVID:
case HB_VCODEC_X264:
op.bytes = buf->size + 1;
op.packet = malloc( op.bytes );
op.packet[0] = buf->key ? 0x08 : 0x00;
memcpy( &op.packet[1], buf->data, buf->size );
op.b_o_s = 0;
op.e_o_s = 0;
op.granulepos = mux_data->i_packet_no;
op.packetno = mux_data->i_packet_no++;
break;
case HB_ACODEC_LAME:
op.bytes = buf->size + 1;
op.packet = malloc( op.bytes );
op.packet[0] = 0x08;
memcpy( &op.packet[1], buf->data, buf->size );
op.b_o_s = 0;
op.e_o_s = 0;
op.granulepos = mux_data->i_packet_no * 1152;
op.packetno = mux_data->i_packet_no++;
break;
case HB_ACODEC_VORBIS:
memcpy( &op, buf->data, sizeof( ogg_packet ) );
op.packet = malloc( op.bytes );
memcpy( op.packet, buf->data + sizeof( ogg_packet ), op.bytes );
break;
default:
hb_log( "muxogm: unhandled codec" );
op.bytes = 0;
op.packet = NULL;
break;
}
if( op.packet )
{
ogg_stream_packetin( &mux_data->os, &op );
for( ;; )
{
ogg_page og;
if( ogg_stream_pageout( &mux_data->os, &og ) == 0 )
{
break;
}
if( fwrite( og.header, og.header_len, 1, m->file ) <= 0 ||
fwrite( og.body, og.body_len, 1, m->file ) <= 0 )
{
hb_log( "muxogm: write failed" );
break;
}
}
free( op.packet );
}
return 0;
}
static int OGMEnd( hb_mux_object_t * m )
{
hb_job_t * job = m->job;
hb_title_t * title = job->title;
hb_audio_t * audio;
hb_mux_data_t * mux_data;
int i;
mux_data = job->mux_data;
if( OGMFlush( m, mux_data ) < 0 )
{
return -1;
}
for( i = 0; i < hb_list_count( title->list_audio ); i++ )
{
audio = hb_list_item( title->list_audio, i );
mux_data = audio->mux_data;
if( OGMFlush( m, mux_data ) < 0 )
{
return -1;
}
}
fclose( m->file );
hb_log( "muxogm: `%s' closed", job->file );
return 0;
}
hb_mux_object_t * hb_mux_ogm_init( hb_job_t * job )
{
hb_mux_object_t * m = calloc( sizeof( hb_mux_object_t ), 1 );
m->init = OGMInit;
m->mux = OGMMux;
m->end = OGMEnd;
m->job = job;
return m;
}
|