1 /*****************************************************************************
2 * vc1.c
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5 * $Id: e639a766ed4297b9b6439916eae1c2d583651817 $
6 *
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
24
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_block.h>
37
38 #include <vlc_bits.h>
39 #include <vlc_block_helper.h>
40 #include "../codec/cc.h"
41 #include "packetizer_helper.h"
42 #include "hxxx_nal.h"
43 #include "startcode_helper.h"
44
45 /*****************************************************************************
46 * Module descriptor
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
50
51 vlc_module_begin ()
52 set_category( CAT_SOUT )
53 set_subcategory( SUBCAT_SOUT_PACKETIZER )
54 set_description( N_("VC-1 packetizer") )
55 set_capability( "packetizer", 50 )
56 set_callbacks( Open, Close )
57 vlc_module_end ()
58
59 /*****************************************************************************
60 * Local prototypes
61 *****************************************************************************/
62 struct decoder_sys_t
63 {
64 /*
65 * Input properties
66 */
67 packetizer_t packetizer;
68
69 /* Current sequence header */
70 bool b_sequence_header;
71 struct
72 {
73 block_t *p_sh;
74 bool b_advanced_profile;
75 bool b_interlaced;
76 bool b_frame_interpolation;
77 bool b_range_reduction;
78 bool b_has_bframe;
79 } sh;
80 bool b_entry_point;
81 struct
82 {
83 block_t *p_ep;
84 } ep;
85
86 /* */
87 bool b_frame;
88
89 /* Current frame being built */
90 mtime_t i_frame_dts;
91 mtime_t i_frame_pts;
92 block_t *p_frame;
93 block_t **pp_last;
94
95
96 mtime_t i_interpolated_dts;
97 bool b_check_startcode;
98
99 /* */
100 uint32_t i_cc_flags;
101 mtime_t i_cc_pts;
102 mtime_t i_cc_dts;
103 cc_data_t cc;
104
105 cc_data_t cc_next;
106 };
107
108 typedef enum
109 {
110 IDU_TYPE_SEQUENCE_HEADER = 0x0f,
111 IDU_TYPE_ENTRY_POINT = 0x0e,
112 IDU_TYPE_FRAME = 0x0D,
113 IDU_TYPE_FIELD = 0x0C,
114 IDU_TYPE_SLICE = 0x0B,
115 IDU_TYPE_END_OF_SEQUENCE = 0x0A,
116
117 IDU_TYPE_SEQUENCE_LEVEL_USER_DATA = 0x1F,
118 IDU_TYPE_ENTRY_POINT_USER_DATA = 0x1E,
119 IDU_TYPE_FRAME_USER_DATA = 0x1D,
120 IDU_TYPE_FIELD_USER_DATA = 0x1C,
121 IDU_TYPE_SLICE_USER_DATA = 0x1B,
122 } idu_type_t;
123
124 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block );
125 static void Flush( decoder_t * );
126
127 static void PacketizeReset( void *p_private, bool b_broken );
128 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
129 static int PacketizeValidate( void *p_private, block_t * );
130
131 static block_t *ParseIDU( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag );
132 static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t * );
133
134 static const uint8_t p_vc1_startcode[3] = { 0x00, 0x00, 0x01 };
135 /*****************************************************************************
136 * Open: probe the packetizer and return score
137 *****************************************************************************
138 * Tries to launch a decoder and return score so that the interface is able
139 * to choose.
140 *****************************************************************************/
Open(vlc_object_t * p_this)141 static int Open( vlc_object_t *p_this )
142 {
143 decoder_t *p_dec = (decoder_t*)p_this;
144 decoder_sys_t *p_sys;
145
146 if( p_dec->fmt_in.i_codec != VLC_CODEC_VC1 )
147 return VLC_EGENERIC;
148
149 p_dec->pf_packetize = Packetize;
150 p_dec->pf_flush = Flush;
151 p_dec->pf_get_cc = GetCc;
152
153 /* Create the output format */
154 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
155 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
156 if( unlikely( !p_sys ) )
157 return VLC_ENOMEM;
158
159 packetizer_Init( &p_sys->packetizer,
160 p_vc1_startcode, sizeof(p_vc1_startcode), startcode_FindAnnexB,
161 NULL, 0, 4,
162 PacketizeReset, PacketizeParse, PacketizeValidate, NULL,
163 p_dec );
164
165 p_sys->b_sequence_header = false;
166 p_sys->sh.p_sh = NULL;
167 p_sys->b_entry_point = false;
168 p_sys->ep.p_ep = NULL;
169
170 p_sys->i_frame_dts = VLC_TS_INVALID;
171 p_sys->i_frame_pts = VLC_TS_INVALID;
172
173 p_sys->b_frame = false;
174 p_sys->p_frame = NULL;
175 p_sys->pp_last = &p_sys->p_frame;
176
177 p_sys->i_interpolated_dts = VLC_TS_INVALID;
178 p_sys->b_check_startcode = p_dec->fmt_in.b_packetized;
179
180 if( p_dec->fmt_out.i_extra > 0 )
181 {
182 uint8_t *p_extra = p_dec->fmt_out.p_extra;
183
184 /* With (some) ASF the first byte has to be stripped */
185 if( p_extra[0] != 0x00 )
186 {
187 memmove( &p_extra[0], &p_extra[1], p_dec->fmt_out.i_extra - 1 );
188 p_dec->fmt_out.i_extra--;
189 }
190
191 /* */
192 if( p_dec->fmt_out.i_extra > 0 )
193 packetizer_Header( &p_sys->packetizer,
194 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
195 }
196
197 /* */
198 p_sys->i_cc_pts = VLC_TS_INVALID;
199 p_sys->i_cc_dts = VLC_TS_INVALID;
200 p_sys->i_cc_flags = 0;
201 cc_Init( &p_sys->cc );
202 cc_Init( &p_sys->cc_next );
203
204 return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208 * Close:
209 *****************************************************************************/
Close(vlc_object_t * p_this)210 static void Close( vlc_object_t *p_this )
211 {
212 decoder_t *p_dec = (decoder_t*)p_this;
213 decoder_sys_t *p_sys = p_dec->p_sys;
214
215 packetizer_Clean( &p_sys->packetizer );
216 if( p_sys->p_frame )
217 block_Release( p_sys->p_frame );
218 if( p_sys->sh.p_sh )
219 block_Release( p_sys->sh.p_sh );
220 if( p_sys->ep.p_ep )
221 block_Release( p_sys->ep.p_ep );
222
223 cc_Exit( &p_sys->cc_next );
224 cc_Exit( &p_sys->cc );
225
226 free( p_sys );
227 }
228
229 /*****************************************************************************
230 * Packetize: packetize an access unit
231 *****************************************************************************/
Packetize(decoder_t * p_dec,block_t ** pp_block)232 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
233 {
234 decoder_sys_t *p_sys = p_dec->p_sys;
235
236 if( p_sys->b_check_startcode && pp_block && *pp_block )
237 {
238 /* Fix syntax for (some?) VC1 from asf */
239 const unsigned i_startcode = sizeof(p_vc1_startcode);
240
241 block_t *p_block = *pp_block;
242 if( p_block->i_buffer > 0 &&
243 ( p_block->i_buffer < i_startcode ||
244 memcmp( p_block->p_buffer, p_vc1_startcode, i_startcode ) ) )
245 {
246 *pp_block = p_block = block_Realloc( p_block, i_startcode+1, p_block->i_buffer );
247 if( p_block )
248 {
249 memcpy( p_block->p_buffer, p_vc1_startcode, i_startcode );
250
251 if( p_sys->b_sequence_header && p_sys->sh.b_interlaced &&
252 p_block->i_buffer > i_startcode+1 &&
253 (p_block->p_buffer[i_startcode+1] & 0xc0) == 0xc0 )
254 p_block->p_buffer[i_startcode] = IDU_TYPE_FIELD;
255 else
256 p_block->p_buffer[i_startcode] = IDU_TYPE_FRAME;
257 }
258 }
259 p_sys->b_check_startcode = false;
260 }
261
262 block_t *p_au = packetizer_Packetize( &p_sys->packetizer, pp_block );
263 if( !p_au )
264 p_sys->b_check_startcode = p_dec->fmt_in.b_packetized;
265
266 return p_au;
267 }
268
Flush(decoder_t * p_dec)269 static void Flush( decoder_t *p_dec )
270 {
271 decoder_sys_t *p_sys = p_dec->p_sys;
272
273 packetizer_Flush( &p_sys->packetizer );
274 }
275
PacketizeReset(void * p_private,bool b_broken)276 static void PacketizeReset( void *p_private, bool b_broken )
277 {
278 decoder_t *p_dec = p_private;
279 decoder_sys_t *p_sys = p_dec->p_sys;
280
281 if( b_broken )
282 {
283 if( p_sys->p_frame )
284 block_ChainRelease( p_sys->p_frame );
285 p_sys->p_frame = NULL;
286 p_sys->pp_last = &p_sys->p_frame;
287 p_sys->b_frame = false;
288 }
289
290 p_sys->i_frame_dts = VLC_TS_INVALID;
291 p_sys->i_frame_pts = VLC_TS_INVALID;
292 p_sys->i_interpolated_dts = VLC_TS_INVALID;
293 }
PacketizeParse(void * p_private,bool * pb_ts_used,block_t * p_block)294 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
295 {
296 decoder_t *p_dec = p_private;
297
298 return ParseIDU( p_dec, pb_ts_used, p_block );
299 }
300
PacketizeValidate(void * p_private,block_t * p_au)301 static int PacketizeValidate( void *p_private, block_t *p_au )
302 {
303 decoder_t *p_dec = p_private;
304 decoder_sys_t *p_sys = p_dec->p_sys;
305
306 if( p_sys->i_interpolated_dts <= VLC_TS_INVALID )
307 {
308 msg_Dbg( p_dec, "need a starting pts/dts" );
309 return VLC_EGENERIC;
310 }
311 VLC_UNUSED(p_au);
312 return VLC_SUCCESS;
313 }
314
315 /* BuildExtraData: gather sequence header and entry point */
BuildExtraData(decoder_t * p_dec)316 static void BuildExtraData( decoder_t *p_dec )
317 {
318 decoder_sys_t *p_sys = p_dec->p_sys;
319 es_format_t *p_es = &p_dec->fmt_out;
320 int i_extra;
321 if( !p_sys->b_sequence_header || !p_sys->b_entry_point )
322 return;
323
324 i_extra = p_sys->sh.p_sh->i_buffer + p_sys->ep.p_ep->i_buffer;
325 if( p_es->i_extra != i_extra )
326 {
327 p_es->i_extra = i_extra;
328 p_es->p_extra = xrealloc( p_es->p_extra, p_es->i_extra );
329 }
330 memcpy( p_es->p_extra,
331 p_sys->sh.p_sh->p_buffer, p_sys->sh.p_sh->i_buffer );
332 memcpy( (uint8_t*)p_es->p_extra + p_sys->sh.p_sh->i_buffer,
333 p_sys->ep.p_ep->p_buffer, p_sys->ep.p_ep->i_buffer );
334 }
335 /* ParseIDU: parse an Independent Decoding Unit */
ParseIDU(decoder_t * p_dec,bool * pb_ts_used,block_t * p_frag)336 static block_t *ParseIDU( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag )
337 {
338 decoder_sys_t *p_sys = p_dec->p_sys;
339 block_t *p_pic;
340 const idu_type_t idu = p_frag->p_buffer[3];
341
342 *pb_ts_used = false;
343 if( !p_sys->b_sequence_header && idu != IDU_TYPE_SEQUENCE_HEADER )
344 {
345 msg_Warn( p_dec, "waiting for sequence header" );
346 block_Release( p_frag );
347 return NULL;
348 }
349 if( p_sys->b_sequence_header && !p_sys->b_entry_point && idu != IDU_TYPE_ENTRY_POINT )
350 {
351 msg_Warn( p_dec, "waiting for entry point" );
352 block_Release( p_frag );
353 return NULL;
354 }
355 /* TODO we do not gather ENTRY_POINT and SEQUENCE_DATA user data
356 * But It should not be a problem for decoder */
357
358 /* Do we have completed a frame */
359 p_pic = NULL;
360 if( p_sys->b_frame &&
361 idu != IDU_TYPE_FRAME_USER_DATA &&
362 idu != IDU_TYPE_FIELD && idu != IDU_TYPE_FIELD_USER_DATA &&
363 idu != IDU_TYPE_SLICE && idu != IDU_TYPE_SLICE_USER_DATA &&
364 idu != IDU_TYPE_END_OF_SEQUENCE )
365 {
366 /* Prepend SH and EP on I */
367 if( p_sys->p_frame->i_flags & BLOCK_FLAG_TYPE_I )
368 {
369 block_t *p_list = block_Duplicate( p_sys->sh.p_sh );
370 block_ChainAppend( &p_list, block_Duplicate( p_sys->ep.p_ep ) );
371 block_ChainAppend( &p_list, p_sys->p_frame );
372
373 p_list->i_flags = p_sys->p_frame->i_flags;
374
375 p_sys->p_frame = p_list;
376 }
377
378 /* */
379 p_pic = block_ChainGather( p_sys->p_frame );
380 p_pic->i_dts = p_sys->i_frame_dts;
381 p_pic->i_pts = p_sys->i_frame_pts;
382
383 /* */
384 if( p_pic->i_dts > VLC_TS_INVALID )
385 p_sys->i_interpolated_dts = p_pic->i_dts;
386
387 /* We can interpolate dts/pts only if we have a frame rate */
388 if( p_dec->fmt_out.video.i_frame_rate != 0 && p_dec->fmt_out.video.i_frame_rate_base != 0 )
389 {
390 if( p_sys->i_interpolated_dts > VLC_TS_INVALID )
391 p_sys->i_interpolated_dts += INT64_C(1000000) *
392 p_dec->fmt_out.video.i_frame_rate_base /
393 p_dec->fmt_out.video.i_frame_rate;
394
395 //msg_Dbg( p_dec, "-------------- XXX0 dts=%"PRId64" pts=%"PRId64" interpolated=%"PRId64,
396 // p_pic->i_dts, p_pic->i_pts, p_sys->i_interpolated_dts );
397 if( p_pic->i_dts <= VLC_TS_INVALID )
398 p_pic->i_dts = p_sys->i_interpolated_dts;
399
400 if( p_pic->i_pts <= VLC_TS_INVALID )
401 {
402 if( !p_sys->sh.b_has_bframe || (p_pic->i_flags & BLOCK_FLAG_TYPE_B ) )
403 p_pic->i_pts = p_pic->i_dts;
404 /* TODO compute pts for other case */
405 }
406 }
407
408 //msg_Dbg( p_dec, "-------------- dts=%"PRId64" pts=%"PRId64, p_pic->i_dts, p_pic->i_pts );
409
410 /* CC */
411 p_sys->i_cc_pts = p_pic->i_pts;
412 p_sys->i_cc_dts = p_pic->i_dts;
413 p_sys->i_cc_flags = p_pic->i_flags;
414
415 p_sys->cc = p_sys->cc_next;
416 cc_Flush( &p_sys->cc_next );
417
418 /* Reset context */
419 p_sys->b_frame = false;
420 p_sys->i_frame_dts = VLC_TS_INVALID;
421 p_sys->i_frame_pts = VLC_TS_INVALID;
422 p_sys->p_frame = NULL;
423 p_sys->pp_last = &p_sys->p_frame;
424 }
425
426 /* */
427 if( p_sys->i_frame_dts <= VLC_TS_INVALID && p_sys->i_frame_pts <= VLC_TS_INVALID )
428 {
429 p_sys->i_frame_dts = p_frag->i_dts;
430 p_sys->i_frame_pts = p_frag->i_pts;
431 *pb_ts_used = true;
432 }
433
434 /* We will add back SH and EP on I frames */
435 block_t *p_release = NULL;
436 if( idu != IDU_TYPE_SEQUENCE_HEADER && idu != IDU_TYPE_ENTRY_POINT )
437 block_ChainLastAppend( &p_sys->pp_last, p_frag );
438 else
439 p_release = p_frag;
440
441 /* Parse IDU */
442 if( idu == IDU_TYPE_SEQUENCE_HEADER )
443 {
444 es_format_t *p_es = &p_dec->fmt_out;
445 bs_t s;
446 unsigned i_bitflow = 0;
447 int i_profile;
448
449 /* */
450 if( p_sys->sh.p_sh )
451 block_Release( p_sys->sh.p_sh );
452 p_sys->sh.p_sh = block_Duplicate( p_frag );
453
454 /* Auto detect VC-1_SPMP_PESpacket_PayloadFormatHeader (SMPTE RP 227) for simple/main profile
455 * TODO find a test case and valid it */
456 if( p_frag->i_buffer > 8 && (p_frag->p_buffer[4]&0x80) == 0 ) /* for advanced profile, the first bit is 1 */
457 {
458 video_format_t *p_v = &p_dec->fmt_in.video;
459 const size_t i_potential_width = GetWBE( &p_frag->p_buffer[4] );
460 const size_t i_potential_height = GetWBE( &p_frag->p_buffer[6] );
461
462 if( i_potential_width >= 2 && i_potential_width <= 8192 &&
463 i_potential_height >= 2 && i_potential_height <= 8192 )
464 {
465 if( ( p_v->i_width <= 0 && p_v->i_height <= 0 ) ||
466 ( p_v->i_width == i_potential_width && p_v->i_height == i_potential_height ) )
467 {
468 static const uint8_t startcode[4] = { 0x00, 0x00, 0x01, IDU_TYPE_SEQUENCE_HEADER };
469 p_es->video.i_width = i_potential_width;
470 p_es->video.i_height = i_potential_height;
471
472 /* Remove it */
473 p_frag->p_buffer += 4;
474 p_frag->i_buffer -= 4;
475 memcpy( p_frag->p_buffer, startcode, sizeof(startcode) );
476 }
477 }
478 }
479
480 /* Parse it */
481 bs_init( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
482 s.p_fwpriv = &i_bitflow;
483 s.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
484
485 i_profile = bs_read( &s, 2 );
486 if( i_profile == 3 )
487 {
488 const int i_level = bs_read( &s, 3 );
489
490 /* Advanced profile */
491 p_sys->sh.b_advanced_profile = true;
492 p_sys->sh.b_range_reduction = false;
493 p_sys->sh.b_has_bframe = true;
494
495 bs_skip( &s, 2+3+5+1 ); // chroma format + frame rate Q + bit rate Q + postprocflag
496
497 p_es->video.i_width = 2*bs_read( &s, 12 )+2;
498 p_es->video.i_height = 2*bs_read( &s, 12 )+2;
499
500 if( !p_sys->b_sequence_header )
501 msg_Dbg( p_dec, "found sequence header for advanced profile level L%d resolution %dx%d",
502 i_level, p_es->video.i_width, p_es->video.i_height);
503
504 bs_skip( &s, 1 );// pulldown
505 p_sys->sh.b_interlaced = bs_read( &s, 1 );
506 bs_skip( &s, 1 );// frame counter
507 p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
508 bs_skip( &s, 1 ); // Reserved
509 bs_skip( &s, 1 ); // Psf
510
511 if( bs_read( &s, 1 ) ) /* Display extension */
512 {
513 const int i_display_width = bs_read( &s, 14 )+1;
514 const int i_display_height = bs_read( &s, 14 )+1;
515
516 p_es->video.i_sar_num = i_display_width * p_es->video.i_height;
517 p_es->video.i_sar_den = i_display_height * p_es->video.i_width;
518
519 if( !p_sys->b_sequence_header )
520 msg_Dbg( p_dec, "display size %dx%d", i_display_width, i_display_height );
521
522 if( bs_read( &s, 1 ) ) /* Pixel aspect ratio (PAR/SAR) */
523 {
524 static const unsigned p_ar[16][2] = {
525 { 0, 0}, { 1, 1}, {12,11}, {10,11}, {16,11}, {40,33},
526 {24,11}, {20,11}, {32,11}, {80,33}, {18,11}, {15,11},
527 {64,33}, {160,99},{ 0, 0}, { 0, 0}
528 };
529 int i_ar = bs_read( &s, 4 );
530 unsigned i_ar_w, i_ar_h;
531
532 if( i_ar == 15 )
533 {
534 i_ar_w = bs_read( &s, 8 );
535 i_ar_h = bs_read( &s, 8 );
536 }
537 else
538 {
539 i_ar_w = p_ar[i_ar][0];
540 i_ar_h = p_ar[i_ar][1];
541 }
542 vlc_ureduce( &i_ar_w, &i_ar_h, i_ar_w, i_ar_h, 0 );
543 if( !p_sys->b_sequence_header )
544 msg_Dbg( p_dec, "aspect ratio %d:%d", i_ar_w, i_ar_h );
545 }
546 }
547 if( bs_read( &s, 1 ) ) /* Frame rate */
548 {
549 unsigned i_fps_num = 0;
550 unsigned i_fps_den = 0;
551 if( bs_read( &s, 1 ) )
552 {
553 i_fps_num = bs_read( &s, 16 )+1;
554 i_fps_den = 32;
555 }
556 else
557 {
558 const int i_nr = bs_read( &s, 8 );
559 const int i_dn = bs_read( &s, 4 );
560
561 switch( i_nr )
562 {
563 case 1: i_fps_num = 24000; break;
564 case 2: i_fps_num = 25000; break;
565 case 3: i_fps_num = 30000; break;
566 case 4: i_fps_num = 50000; break;
567 case 5: i_fps_num = 60000; break;
568 case 6: i_fps_num = 48000; break;
569 case 7: i_fps_num = 72000; break;
570 }
571 switch( i_dn )
572 {
573 case 1: i_fps_den = 1000; break;
574 case 2: i_fps_den = 1001; break;
575 }
576 }
577 if( i_fps_num != 0 && i_fps_den != 0 )
578 vlc_ureduce( &p_es->video.i_frame_rate, &p_es->video.i_frame_rate_base, i_fps_num, i_fps_den, 0 );
579
580 if( !p_sys->b_sequence_header )
581 msg_Dbg( p_dec, "frame rate %d/%d", p_es->video.i_frame_rate, p_es->video.i_frame_rate_base );
582 }
583 if( bs_read1( &s ) ) /* Color Format */
584 {
585 switch( bs_read( &s, 8 ) ) /* Color Primaries */
586 {
587 case 1: p_es->video.primaries = COLOR_PRIMARIES_BT709; break;
588 case 4: p_es->video.primaries = COLOR_PRIMARIES_BT470_M; break;
589 case 5: p_es->video.primaries = COLOR_PRIMARIES_BT470_BG; break;
590 case 6: p_es->video.primaries = COLOR_PRIMARIES_SMTPE_RP145; break;
591 default: p_es->video.primaries = COLOR_PRIMARIES_UNDEF; break;
592 }
593
594 switch( bs_read( &s, 8 ) ) /* Transfert Chars */
595 {
596 case 1: p_es->video.transfer = TRANSFER_FUNC_BT709; break;
597 case 4: p_es->video.transfer = TRANSFER_FUNC_BT470_M; break;
598 case 5: p_es->video.transfer = TRANSFER_FUNC_BT470_BG; break;
599 case 6: p_es->video.transfer = TRANSFER_FUNC_SMPTE_170; break;
600 case 7: p_es->video.transfer = TRANSFER_FUNC_SMPTE_240; break;
601 case 8: p_es->video.transfer = TRANSFER_FUNC_LINEAR; break;
602 default: p_es->video.transfer = TRANSFER_FUNC_UNDEF; break;
603 }
604
605 switch( bs_read( &s, 8 ) ) /* Matrix Coef */
606 {
607 case 1: p_es->video.space = COLOR_SPACE_BT709; break;
608 case 6: p_es->video.space = COLOR_SPACE_BT601; break;
609 case 7: p_es->video.space = COLOR_SPACE_SMPTE_240; break;
610 default: p_es->video.space = COLOR_SPACE_UNDEF; break;
611 }
612 }
613 }
614 else
615 {
616 /* Simple and main profile */
617 p_sys->sh.b_advanced_profile = false;
618 p_sys->sh.b_interlaced = false;
619
620 if( !p_sys->b_sequence_header )
621 msg_Dbg( p_dec, "found sequence header for %s profile", i_profile == 0 ? "simple" : "main" );
622
623 bs_skip( &s, 2+3+5+1+1+ // reserved + frame rate Q + bit rate Q + loop filter + reserved
624 1+1+1+1+2+ // multiresolution + reserved + fast uv mc + extended mv + dquant
625 1+1+1+1 ); // variable size transform + reserved + overlap + sync marker
626 p_sys->sh.b_range_reduction = bs_read( &s, 1 );
627 if( bs_read( &s, 3 ) > 0 )
628 p_sys->sh.b_has_bframe = true;
629 else
630 p_sys->sh.b_has_bframe = false;
631 bs_skip( &s, 2 ); // quantizer
632
633 p_sys->sh.b_frame_interpolation = bs_read( &s, 1 );
634 }
635 p_sys->b_sequence_header = true;
636 BuildExtraData( p_dec );
637 }
638 else if( idu == IDU_TYPE_ENTRY_POINT )
639 {
640 if( p_sys->ep.p_ep )
641 block_Release( p_sys->ep.p_ep );
642 p_sys->ep.p_ep = block_Duplicate( p_frag );
643
644 if( !p_sys->b_entry_point )
645 msg_Dbg( p_dec, "found entry point" );
646
647 p_sys->b_entry_point = true;
648 BuildExtraData( p_dec );
649 }
650 else if( idu == IDU_TYPE_FRAME )
651 {
652 bs_t s;
653 unsigned i_bitflow = 0;
654
655 /* Parse it + interpolate pts/dts if possible */
656 bs_init( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
657 s.p_fwpriv = &i_bitflow;
658 s.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
659
660 if( p_sys->sh.b_advanced_profile )
661 {
662 int i_fcm = 0;
663
664 if( p_sys->sh.b_interlaced )
665 {
666 if( bs_read( &s, 1 ) )
667 {
668 if( bs_read( &s, 1 ) )
669 i_fcm = 1; /* interlaced field */
670 else
671 i_fcm = 2; /* interlaced frame */
672 }
673 }
674
675 if( i_fcm == 1 ) /*interlaced field */
676 {
677 /* XXX for mixed I/P we should check reference usage before marking them I (too much work) */
678 switch( bs_read( &s, 3 ) )
679 {
680 case 0: /* II */
681 case 1: /* IP */
682 case 2: /* PI */
683 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
684 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
685 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
686 break;
687 case 3: /* PP */
688 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
689 break;
690 case 4: /* BB */
691 case 5: /* BBi */
692 case 6: /* BiB */
693 case 7: /* BiBi */
694 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
695 break;
696 }
697 }
698 else
699 {
700 if( !bs_read( &s, 1 ) )
701 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
702 else if( !bs_read( &s, 1 ) )
703 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
704 else if( !bs_read( &s, 1 ) )
705 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
706 else if( !bs_read( &s, 1 ) )
707 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B; /* Bi */
708 else
709 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P; /* P Skip */
710 }
711 }
712 else
713 {
714 if( p_sys->sh.b_frame_interpolation )
715 bs_skip( &s, 1 ); // interpolate
716 bs_skip( &s, 2 ); // frame count
717 if( p_sys->sh.b_range_reduction )
718 bs_skip( &s, 1 ); // range reduction
719
720 if( bs_read( &s, 1 ) )
721 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_P;
722 else if( !p_sys->sh.b_has_bframe || bs_read( &s, 1 ) )
723 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
724 else
725 p_sys->p_frame->i_flags |= BLOCK_FLAG_TYPE_B;
726 }
727 p_sys->b_frame = true;
728 }
729 else if( idu == IDU_TYPE_FRAME_USER_DATA )
730 {
731 bs_t s;
732 unsigned i_bitflow = 0;
733 const size_t i_size = p_frag->i_buffer - 4;
734 bs_init( &s, &p_frag->p_buffer[4], i_size );
735 s.p_fwpriv = &i_bitflow;
736 s.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
737
738 unsigned i_data;
739 uint8_t *p_data = malloc( i_size );
740 if( p_data )
741 {
742 /* store converted data */
743 for( i_data = 0; i_data<i_size && bs_remain( &s ) >= 16 /* trailing 0x80 flush byte */; i_data++ )
744 p_data[i_data] = bs_read( &s, 8 );
745
746 /* TS 101 154 Auxiliary Data and VC-1 video */
747 static const uint8_t p_DVB1_user_identifier[] = {
748 0x47, 0x41, 0x39, 0x34 /* user identifier */
749 };
750
751 /* Check if we have DVB1_data() */
752 if( i_data >= sizeof(p_DVB1_user_identifier) &&
753 !memcmp( p_data, p_DVB1_user_identifier, sizeof(p_DVB1_user_identifier) ) )
754 {
755 cc_ProbeAndExtract( &p_sys->cc_next, true, p_data, i_data );
756 }
757
758 free( p_data );
759 }
760 }
761
762 if( p_release )
763 block_Release( p_release );
764 return p_pic;
765 }
766
767 /*****************************************************************************
768 * GetCc:
769 *****************************************************************************/
GetCc(decoder_t * p_dec,decoder_cc_desc_t * p_desc)770 static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
771 {
772 decoder_sys_t *p_sys = p_dec->p_sys;
773 block_t *p_cc;
774
775 p_cc = block_Alloc( p_sys->cc.i_data);
776 if( p_cc )
777 {
778 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
779 p_cc->i_dts =
780 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
781 p_cc->i_flags = p_sys->i_cc_flags & BLOCK_FLAG_TYPE_MASK;
782
783 p_desc->i_608_channels = p_sys->cc.i_608channels;
784 p_desc->i_708_channels = p_sys->cc.i_708channels;
785 p_desc->i_reorder_depth = p_sys->cc.b_reorder ? 4 : -1;
786 }
787 cc_Flush( &p_sys->cc );
788 return p_cc;
789 }
790