1 /*****************************************************************************
2 * h264.c: h264/avc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
39 #include <vlc_block.h>
41 #include <vlc_block_helper.h>
43 #include "../codec/cc.h"
44 #include "packetizer_helper.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t
* );
50 static void Close( vlc_object_t
* );
53 set_category( CAT_SOUT
)
54 set_subcategory( SUBCAT_SOUT_PACKETIZER
)
55 set_description( N_("H.264 video packetizer") )
56 set_capability( "packetizer", 50 )
57 set_callbacks( Open
, Close
)
61 /****************************************************************************
63 ****************************************************************************/
70 int i_pic_parameter_set_id
;
74 int i_bottom_field_flag
;
78 int i_pic_order_cnt_lsb
;
79 int i_delta_pic_order_cnt_bottom
;
81 int i_delta_pic_order_cnt0
;
82 int i_delta_pic_order_cnt1
;
90 packetizer_t packetizer
;
99 block_t
*pp_sps
[SPS_MAX
];
100 block_t
*pp_pps
[PPS_MAX
];
103 int i_avcC_length_size
;
105 /* Useful values of the Sequence Parameter Set */
106 int i_log2_max_frame_num
;
107 int b_frame_mbs_only
;
108 int i_pic_order_cnt_type
;
109 int i_delta_pic_order_always_zero_flag
;
110 int i_log2_max_pic_order_cnt_lsb
;
112 /* Value from Picture Parameter Set */
113 int i_pic_order_present_flag
;
115 /* Useful values of the Slice Header */
138 NAL_SLICE_IDR
= 5, /* ref_idc != 0 */
139 NAL_SEI
= 6, /* ref_idc == 0 */
143 /* ref_idc == 0 for 6,9,10,11,12 */
148 NAL_PRIORITY_DISPOSABLE
= 0,
149 NAL_PRIORITY_LOW
= 1,
150 NAL_PRIORITY_HIGH
= 2,
151 NAL_PRIORITY_HIGHEST
= 3,
154 static block_t
*Packetize( decoder_t
*, block_t
** );
155 static block_t
*PacketizeAVC1( decoder_t
*, block_t
** );
156 static block_t
*GetCc( decoder_t
*p_dec
, bool pb_present
[4] );
158 static void PacketizeReset( void *p_private
, bool b_broken
);
159 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
* );
160 static int PacketizeValidate( void *p_private
, block_t
* );
162 static block_t
*ParseNALBlock( decoder_t
*, bool *pb_used_ts
, block_t
* );
163 static block_t
*CreateAnnexbNAL( decoder_t
*, const uint8_t *p
, int );
165 static block_t
*OutputPicture( decoder_t
*p_dec
);
166 static void PutSPS( decoder_t
*p_dec
, block_t
*p_frag
);
167 static void PutPPS( decoder_t
*p_dec
, block_t
*p_frag
);
168 static void ParseSlice( decoder_t
*p_dec
, bool *pb_new_picture
, slice_t
*p_slice
,
169 int i_nal_ref_idc
, int i_nal_type
, const block_t
*p_frag
);
170 static void ParseSei( decoder_t
*, block_t
* );
173 static const uint8_t p_h264_startcode
[3] = { 0x00, 0x00, 0x01 };
175 /*****************************************************************************
176 * Open: probe the packetizer and return score
177 * When opening after demux, the packetizer is only loaded AFTER the decoder
178 * That means that what you set in fmt_out is ignored by the decoder in this special case
179 *****************************************************************************/
180 static int Open( vlc_object_t
*p_this
)
182 decoder_t
*p_dec
= (decoder_t
*)p_this
;
183 decoder_sys_t
*p_sys
;
186 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_H264
)
188 if( p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC( 'a', 'v', 'c', '1') &&
189 p_dec
->fmt_in
.i_extra
< 7 )
192 /* Allocate the memory needed to store the decoder's structure */
193 if( ( p_dec
->p_sys
= p_sys
= malloc( sizeof(decoder_sys_t
) ) ) == NULL
)
198 packetizer_Init( &p_sys
->packetizer
,
199 p_h264_startcode
, sizeof(p_h264_startcode
),
201 PacketizeReset
, PacketizeParse
, PacketizeValidate
, p_dec
);
203 p_sys
->b_slice
= false;
204 p_sys
->p_frame
= NULL
;
205 p_sys
->b_header
= false;
206 p_sys
->b_sps
= false;
207 p_sys
->b_pps
= false;
208 for( i
= 0; i
< SPS_MAX
; i
++ )
209 p_sys
->pp_sps
[i
] = NULL
;
210 for( i
= 0; i
< PPS_MAX
; i
++ )
211 p_sys
->pp_pps
[i
] = NULL
;
213 p_sys
->slice
.i_nal_type
= -1;
214 p_sys
->slice
.i_nal_ref_idc
= -1;
215 p_sys
->slice
.i_idr_pic_id
= -1;
216 p_sys
->slice
.i_frame_num
= -1;
217 p_sys
->slice
.i_frame_type
= 0;
218 p_sys
->slice
.i_pic_parameter_set_id
= -1;
219 p_sys
->slice
.i_field_pic_flag
= 0;
220 p_sys
->slice
.i_bottom_field_flag
= -1;
221 p_sys
->slice
.i_pic_order_cnt_lsb
= -1;
222 p_sys
->slice
.i_delta_pic_order_cnt_bottom
= -1;
224 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
225 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
227 /* Setup properties */
228 es_format_Copy( &p_dec
->fmt_out
, &p_dec
->fmt_in
);
229 p_dec
->fmt_out
.i_codec
= VLC_CODEC_H264
;
231 if( p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC( 'a', 'v', 'c', '1' ) )
233 /* This type of stream is produced by mp4 and matroska
234 * when we want to store it in another streamformat, you need to convert
235 * The fmt_in.p_extra should ALWAYS contain the avcC
236 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
237 uint8_t *p
= &((uint8_t*)p_dec
->fmt_in
.p_extra
)[4];
243 p_sys
->i_avcC_length_size
= 1 + ((*p
++)&0x03);
247 for( i
= 0; i
< i_sps
; i
++ )
249 uint16_t i_length
= GetWBE( p
); p
+= 2;
251 (uint8_t*)p_dec
->fmt_in
.p_extra
+ p_dec
->fmt_in
.i_extra
- p
)
255 block_t
*p_sps
= CreateAnnexbNAL( p_dec
, p
, i_length
);
258 ParseNALBlock( p_dec
, &b_dummy
, p_sps
);
263 for( i
= 0; i
< i_pps
; i
++ )
265 uint16_t i_length
= GetWBE( p
); p
+= 2;
267 (uint8_t*)p_dec
->fmt_in
.p_extra
+ p_dec
->fmt_in
.i_extra
- p
)
271 block_t
*p_pps
= CreateAnnexbNAL( p_dec
, p
, i_length
);
274 ParseNALBlock( p_dec
, &b_dummy
, p_pps
);
277 msg_Dbg( p_dec
, "avcC length size=%d, sps=%d, pps=%d",
278 p_sys
->i_avcC_length_size
, i_sps
, i_pps
);
280 if( !p_sys
->b_sps
|| !p_sys
->b_pps
)
283 /* FIXME: FFMPEG isn't happy at all if you leave this */
284 if( p_dec
->fmt_out
.i_extra
> 0 )
285 free( p_dec
->fmt_out
.p_extra
);
286 p_dec
->fmt_out
.i_extra
= 0;
287 p_dec
->fmt_out
.p_extra
= NULL
;
289 /* Set the new extradata */
290 for( i
= 0; i
< SPS_MAX
; i
++ )
292 if( p_sys
->pp_sps
[i
] )
293 p_dec
->fmt_out
.i_extra
+= p_sys
->pp_sps
[i
]->i_buffer
;
295 for( i
= 0; i
< PPS_MAX
; i
++ )
297 if( p_sys
->pp_pps
[i
] )
298 p_dec
->fmt_out
.i_extra
+= p_sys
->pp_pps
[i
]->i_buffer
;
300 p_dec
->fmt_out
.p_extra
= malloc( p_dec
->fmt_out
.i_extra
);
301 if( p_dec
->fmt_out
.p_extra
)
303 uint8_t *p_dst
= p_dec
->fmt_out
.p_extra
;
305 for( i
= 0; i
< SPS_MAX
; i
++ )
307 if( p_sys
->pp_sps
[i
] )
309 memcpy( p_dst
, p_sys
->pp_sps
[i
]->p_buffer
, p_sys
->pp_sps
[i
]->i_buffer
);
310 p_dst
+= p_sys
->pp_sps
[i
]->i_buffer
;
313 for( i
= 0; i
< PPS_MAX
; i
++ )
315 if( p_sys
->pp_pps
[i
] )
317 memcpy( p_dst
, p_sys
->pp_pps
[i
]->p_buffer
, p_sys
->pp_pps
[i
]->i_buffer
);
318 p_dst
+= p_sys
->pp_pps
[i
]->i_buffer
;
321 p_sys
->b_header
= true;
325 p_dec
->fmt_out
.i_extra
= 0;
329 p_dec
->pf_packetize
= PacketizeAVC1
;
334 /* This type of stream contains data with 3 of 4 byte startcodes
335 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
336 * The fmt_out.p_extra should be the same */
339 p_dec
->pf_packetize
= Packetize
;
340 p_dec
->pf_get_cc
= GetCc
;
343 p_sys
->i_cc_pts
= VLC_TS_INVALID
;
344 p_sys
->i_cc_dts
= VLC_TS_INVALID
;
345 p_sys
->i_cc_flags
= 0;
346 cc_Init( &p_sys
->cc
);
347 cc_Init( &p_sys
->cc_next
);
350 if( p_dec
->fmt_in
.i_extra
> 0 )
351 packetizer_Header( &p_sys
->packetizer
,
352 p_dec
->fmt_in
.p_extra
, p_dec
->fmt_in
.i_extra
);
358 /*****************************************************************************
359 * Close: clean up the packetizer
360 *****************************************************************************/
361 static void Close( vlc_object_t
*p_this
)
363 decoder_t
*p_dec
= (decoder_t
*)p_this
;
364 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
368 block_ChainRelease( p_sys
->p_frame
);
369 for( i
= 0; i
< SPS_MAX
; i
++ )
371 if( p_sys
->pp_sps
[i
] )
372 block_Release( p_sys
->pp_sps
[i
] );
374 for( i
= 0; i
< PPS_MAX
; i
++ )
376 if( p_sys
->pp_pps
[i
] )
377 block_Release( p_sys
->pp_pps
[i
] );
379 packetizer_Clean( &p_sys
->packetizer
);
381 if( p_dec
->pf_get_cc
)
383 cc_Exit( &p_sys
->cc_next
);
384 cc_Exit( &p_sys
->cc
);
390 /****************************************************************************
391 * Packetize: the whole thing
392 * Search for the startcodes 3 or more bytes
393 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
394 ****************************************************************************/
395 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
397 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
399 return packetizer_Packetize( &p_sys
->packetizer
, pp_block
);
402 /****************************************************************************
403 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
404 * Will always use 4 byte 0 0 0 1 startcodes
405 * Will prepend a SPS and PPS before each keyframe
406 ****************************************************************************/
407 static block_t
*PacketizeAVC1( decoder_t
*p_dec
, block_t
**pp_block
)
409 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
411 block_t
*p_ret
= NULL
;
414 if( !pp_block
|| !*pp_block
)
416 if( (*pp_block
)->i_flags
&(BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
418 block_Release( *pp_block
);
425 for( p
= p_block
->p_buffer
; p
< &p_block
->p_buffer
[p_block
->i_buffer
]; )
432 for( i
= 0; i
< p_sys
->i_avcC_length_size
; i
++ )
434 i_size
= (i_size
<< 8) | (*p
++);
438 i_size
> ( p_block
->p_buffer
+ p_block
->i_buffer
- p
) )
440 msg_Err( p_dec
, "Broken frame : size %d is too big", i_size
);
444 block_t
*p_part
= CreateAnnexbNAL( p_dec
, p
, i_size
);
448 p_part
->i_dts
= p_block
->i_dts
;
449 p_part
->i_pts
= p_block
->i_pts
;
452 if( ( p_pic
= ParseNALBlock( p_dec
, &b_dummy
, p_part
) ) )
454 block_ChainAppend( &p_ret
, p_pic
);
458 block_Release( p_block
);
463 /*****************************************************************************
465 *****************************************************************************/
466 static block_t
*GetCc( decoder_t
*p_dec
, bool pb_present
[4] )
468 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
471 for( int i
= 0; i
< 4; i
++ )
472 pb_present
[i
] = p_sys
->cc
.pb_present
[i
];
474 if( p_sys
->cc
.i_data
<= 0 )
477 p_cc
= block_New( p_dec
, p_sys
->cc
.i_data
);
480 memcpy( p_cc
->p_buffer
, p_sys
->cc
.p_data
, p_sys
->cc
.i_data
);
482 p_cc
->i_pts
= p_sys
->cc
.b_reorder
? p_sys
->i_cc_pts
: p_sys
->i_cc_dts
;
483 p_cc
->i_flags
= ( p_sys
->cc
.b_reorder
? p_sys
->i_cc_flags
: BLOCK_FLAG_TYPE_P
) & BLOCK_FLAG_TYPE_MASK
;
485 cc_Flush( &p_sys
->cc
);
489 /****************************************************************************
491 ****************************************************************************/
492 static void PacketizeReset( void *p_private
, bool b_broken
)
494 decoder_t
*p_dec
= p_private
;
495 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
500 block_ChainRelease( p_sys
->p_frame
);
501 p_sys
->p_frame
= NULL
;
502 p_sys
->slice
.i_frame_type
= 0;
503 p_sys
->b_slice
= false;
505 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
506 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
508 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
*p_block
)
510 decoder_t
*p_dec
= p_private
;
512 /* Remove trailing 0 bytes */
513 while( p_block
->i_buffer
&& p_block
->p_buffer
[p_block
->i_buffer
-1] == 0x00 )
516 return ParseNALBlock( p_dec
, pb_ts_used
, p_block
);
518 static int PacketizeValidate( void *p_private
, block_t
*p_au
)
520 VLC_UNUSED(p_private
);
525 static block_t
*CreateAnnexbNAL( decoder_t
*p_dec
, const uint8_t *p
, int i_size
)
529 p_nal
= block_New( p_dec
, 4 + i_size
);
530 if( !p_nal
) return NULL
;
533 p_nal
->p_buffer
[0] = 0x00;
534 p_nal
->p_buffer
[1] = 0x00;
535 p_nal
->p_buffer
[2] = 0x00;
536 p_nal
->p_buffer
[3] = 0x01;
539 memcpy( &p_nal
->p_buffer
[4], p
, i_size
);
545 static void CreateDecodedNAL( uint8_t **pp_ret
, int *pi_ret
,
546 const uint8_t *src
, int i_src
)
548 const uint8_t *end
= &src
[i_src
];
549 uint8_t *dst
= malloc( i_src
);
557 if( src
< end
- 3 && src
[0] == 0x00 && src
[1] == 0x00 &&
569 *pi_ret
= dst
- *pp_ret
;
572 static inline int bs_read_ue( bs_t
*s
)
576 while( bs_read1( s
) == 0 && s
->p
< s
->p_end
&& i
< 32 )
580 return( ( 1 << i
) - 1 + bs_read( s
, i
) );
583 static inline int bs_read_se( bs_t
*s
)
585 int val
= bs_read_ue( s
);
587 return val
&0x01 ? (val
+1)/2 : -(val
/2);
590 /*****************************************************************************
591 * ParseNALBlock: parses annexB type NALs
592 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
593 *****************************************************************************/
594 static block_t
*ParseNALBlock( decoder_t
*p_dec
, bool *pb_used_ts
, block_t
*p_frag
)
596 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
597 block_t
*p_pic
= NULL
;
599 const int i_nal_ref_idc
= (p_frag
->p_buffer
[4] >> 5)&0x03;
600 const int i_nal_type
= p_frag
->p_buffer
[4]&0x1f;
601 const mtime_t i_frag_dts
= p_frag
->i_dts
;
602 const mtime_t i_frag_pts
= p_frag
->i_pts
;
604 if( p_sys
->b_slice
&& ( !p_sys
->b_sps
|| !p_sys
->b_pps
) )
606 block_ChainRelease( p_sys
->p_frame
);
607 msg_Warn( p_dec
, "waiting for SPS/PPS" );
610 p_sys
->slice
.i_frame_type
= 0;
611 p_sys
->p_frame
= NULL
;
612 p_sys
->b_slice
= false;
613 cc_Flush( &p_sys
->cc_next
);
616 if( ( !p_sys
->b_sps
|| !p_sys
->b_pps
) &&
617 i_nal_type
>= NAL_SLICE
&& i_nal_type
<= NAL_SLICE_IDR
)
619 p_sys
->b_slice
= true;
620 /* Fragment will be discarded later on */
622 else if( i_nal_type
>= NAL_SLICE
&& i_nal_type
<= NAL_SLICE_IDR
)
627 ParseSlice( p_dec
, &b_new_picture
, &slice
, i_nal_ref_idc
, i_nal_type
, p_frag
);
630 if( b_new_picture
&& p_sys
->b_slice
)
631 p_pic
= OutputPicture( p_dec
);
634 p_sys
->slice
= slice
;
635 p_sys
->b_slice
= true;
637 else if( i_nal_type
== NAL_SPS
)
640 p_pic
= OutputPicture( p_dec
);
642 PutSPS( p_dec
, p_frag
);
644 /* Do not append the SPS because we will insert it on keyframes */
647 else if( i_nal_type
== NAL_PPS
)
650 p_pic
= OutputPicture( p_dec
);
652 PutPPS( p_dec
, p_frag
);
654 /* Do not append the PPS because we will insert it on keyframes */
657 else if( i_nal_type
== NAL_AU_DELIMITER
||
658 i_nal_type
== NAL_SEI
||
659 ( i_nal_type
>= 13 && i_nal_type
<= 18 ) )
662 p_pic
= OutputPicture( p_dec
);
664 /* Parse SEI for CC support */
665 if( i_nal_type
== NAL_SEI
)
666 ParseSei( p_dec
, p_frag
);
669 /* Append the block */
671 block_ChainAppend( &p_sys
->p_frame
, p_frag
);
674 if( p_sys
->i_frame_dts
<= VLC_TS_INVALID
&&
675 p_sys
->i_frame_pts
<= VLC_TS_INVALID
)
677 p_sys
->i_frame_dts
= i_frag_dts
;
678 p_sys
->i_frame_pts
= i_frag_pts
;
684 static block_t
*OutputPicture( decoder_t
*p_dec
)
686 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
689 if( !p_sys
->b_header
&& p_sys
->slice
.i_frame_type
!= BLOCK_FLAG_TYPE_I
)
692 if( p_sys
->slice
.i_frame_type
== BLOCK_FLAG_TYPE_I
&& p_sys
->b_sps
&& p_sys
->b_pps
)
694 block_t
*p_list
= NULL
;
697 for( i
= 0; i
< SPS_MAX
; i
++ )
699 if( p_sys
->pp_sps
[i
] )
700 block_ChainAppend( &p_list
, block_Duplicate( p_sys
->pp_sps
[i
] ) );
702 for( i
= 0; i
< PPS_MAX
; i
++ )
704 if( p_sys
->pp_pps
[i
] )
705 block_ChainAppend( &p_list
, block_Duplicate( p_sys
->pp_pps
[i
] ) );
708 p_sys
->b_header
= true;
710 block_ChainAppend( &p_list
, p_sys
->p_frame
);
711 p_pic
= block_ChainGather( p_list
);
715 p_pic
= block_ChainGather( p_sys
->p_frame
);
717 p_pic
->i_dts
= p_sys
->i_frame_dts
;
718 p_pic
->i_pts
= p_sys
->i_frame_pts
;
719 p_pic
->i_length
= 0; /* FIXME */
720 p_pic
->i_flags
|= p_sys
->slice
.i_frame_type
;
722 p_sys
->slice
.i_frame_type
= 0;
723 p_sys
->p_frame
= NULL
;
724 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
725 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
726 p_sys
->b_slice
= false;
729 p_sys
->i_cc_pts
= p_pic
->i_pts
;
730 p_sys
->i_cc_dts
= p_pic
->i_dts
;
731 p_sys
->i_cc_flags
= p_pic
->i_flags
;
734 cc_data_t cc_tmp
= p_sys
->cc
;
735 p_sys
->cc
= p_sys
->cc_next
;
736 p_sys
->cc_next
= cc_tmp
;
738 cc_Flush( &p_sys
->cc_next
);
743 static void PutSPS( decoder_t
*p_dec
, block_t
*p_frag
)
745 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
747 uint8_t *pb_dec
= NULL
;
753 CreateDecodedNAL( &pb_dec
, &i_dec
, &p_frag
->p_buffer
[5],
754 p_frag
->i_buffer
- 5 );
756 bs_init( &s
, pb_dec
, i_dec
);
757 int i_profile_idc
= bs_read( &s
, 8 );
758 p_dec
->fmt_out
.i_profile
= i_profile_idc
;
759 /* Skip constraint_set0123, reserved(4) */
760 bs_skip( &s
, 1+1+1+1 + 4 );
761 p_dec
->fmt_out
.i_level
= bs_read( &s
, 8 );
763 i_sps_id
= bs_read_ue( &s
);
764 if( i_sps_id
>= SPS_MAX
)
766 msg_Warn( p_dec
, "invalid SPS (sps_id=%d)", i_sps_id
);
768 block_Release( p_frag
);
772 if( i_profile_idc
== 100 || i_profile_idc
== 110 ||
773 i_profile_idc
== 122 || i_profile_idc
== 244 ||
774 i_profile_idc
== 44 || i_profile_idc
== 83 ||
775 i_profile_idc
== 86 )
777 /* chroma_format_idc */
778 const int i_chroma_format_idc
= bs_read_ue( &s
);
779 if( i_chroma_format_idc
== 3 )
780 bs_skip( &s
, 1 ); /* seperate_colour_plane_flag */
781 /* bit_depth_luma_minus8 */
783 /* bit_depth_chroma_minus8 */
785 /* qpprime_y_zero_transform_bypass_flag */
787 /* seq_scaling_matrix_present_flag */
788 i_tmp
= bs_read( &s
, 1 );
791 for( int i
= 0; i
< ((3 != i_chroma_format_idc
) ? 8 : 12); i
++ )
793 /* seq_scaling_list_present_flag[i] */
794 i_tmp
= bs_read( &s
, 1 );
797 const int i_size_of_scaling_list
= (i
< 6 ) ? 16 : 64;
798 /* scaling_list (...) */
801 for( int j
= 0; j
< i_size_of_scaling_list
; j
++ )
803 if( i_nextscale
!= 0 )
806 i_tmp
= bs_read( &s
, 1 );
807 i_nextscale
= ( i_lastscale
+ i_tmp
+ 256 ) % 256;
808 /* useDefaultScalingMatrixFlag = ... */
811 i_lastscale
= ( i_nextscale
== 0 ) ? i_lastscale
: i_nextscale
;
817 /* Skip i_log2_max_frame_num */
818 p_sys
->i_log2_max_frame_num
= bs_read_ue( &s
);
819 if( p_sys
->i_log2_max_frame_num
> 12)
820 p_sys
->i_log2_max_frame_num
= 12;
822 p_sys
->i_pic_order_cnt_type
= bs_read_ue( &s
);
823 if( p_sys
->i_pic_order_cnt_type
== 0 )
825 /* skip i_log2_max_poc_lsb */
826 p_sys
->i_log2_max_pic_order_cnt_lsb
= bs_read_ue( &s
);
827 if( p_sys
->i_log2_max_pic_order_cnt_lsb
> 12 )
828 p_sys
->i_log2_max_pic_order_cnt_lsb
= 12;
830 else if( p_sys
->i_pic_order_cnt_type
== 1 )
833 /* skip b_delta_pic_order_always_zero */
834 p_sys
->i_delta_pic_order_always_zero_flag
= bs_read( &s
, 1 );
835 /* skip i_offset_for_non_ref_pic */
837 /* skip i_offset_for_top_to_bottom_field */
839 /* read i_num_ref_frames_in_poc_cycle */
840 i_cycle
= bs_read_ue( &s
);
841 if( i_cycle
> 256 ) i_cycle
= 256;
844 /* skip i_offset_for_ref_frame */
849 /* i_num_ref_frames */
851 /* b_gaps_in_frame_num_value_allowed */
855 p_dec
->fmt_out
.video
.i_width
= 16 * ( bs_read_ue( &s
) + 1 );
856 p_dec
->fmt_out
.video
.i_height
= 16 * ( bs_read_ue( &s
) + 1 );
858 /* b_frame_mbs_only */
859 p_sys
->b_frame_mbs_only
= bs_read( &s
, 1 );
860 if( p_sys
->b_frame_mbs_only
== 0 )
864 /* b_direct8x8_inference */
868 i_tmp
= bs_read( &s
, 1 );
882 i_tmp
= bs_read( &s
, 1 );
885 /* read the aspect ratio part if any */
886 i_tmp
= bs_read( &s
, 1 );
889 static const struct { int w
, h
; } sar
[17] =
891 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
892 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
893 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
894 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
897 int i_sar
= bs_read( &s
, 8 );
905 else if( i_sar
== 255 )
907 w
= bs_read( &s
, 16 );
908 h
= bs_read( &s
, 16 );
916 if( w
!= 0 && h
!= 0 )
918 p_dec
->fmt_out
.video
.i_sar_num
= w
;
919 p_dec
->fmt_out
.video
.i_sar_den
= h
;
923 p_dec
->fmt_out
.video
.i_sar_num
= 1;
924 p_dec
->fmt_out
.video
.i_sar_den
= 1;
931 /* We have a new SPS */
933 msg_Dbg( p_dec
, "found NAL_SPS (sps_id=%d)", i_sps_id
);
936 if( p_sys
->pp_sps
[i_sps_id
] )
937 block_Release( p_sys
->pp_sps
[i_sps_id
] );
938 p_sys
->pp_sps
[i_sps_id
] = p_frag
;
941 static void PutPPS( decoder_t
*p_dec
, block_t
*p_frag
)
943 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
948 bs_init( &s
, &p_frag
->p_buffer
[5], p_frag
->i_buffer
- 5 );
949 i_pps_id
= bs_read_ue( &s
); // pps id
950 i_sps_id
= bs_read_ue( &s
); // sps id
951 if( i_pps_id
>= PPS_MAX
|| i_sps_id
>= SPS_MAX
)
953 msg_Warn( p_dec
, "invalid PPS (pps_id=%d sps_id=%d)", i_pps_id
, i_sps_id
);
954 block_Release( p_frag
);
957 bs_skip( &s
, 1 ); // entropy coding mode flag
958 p_sys
->i_pic_order_present_flag
= bs_read( &s
, 1 );
961 /* We have a new PPS */
963 msg_Dbg( p_dec
, "found NAL_PPS (pps_id=%d sps_id=%d)", i_pps_id
, i_sps_id
);
966 if( p_sys
->pp_pps
[i_pps_id
] )
967 block_Release( p_sys
->pp_pps
[i_pps_id
] );
968 p_sys
->pp_pps
[i_pps_id
] = p_frag
;
971 static void ParseSlice( decoder_t
*p_dec
, bool *pb_new_picture
, slice_t
*p_slice
,
972 int i_nal_ref_idc
, int i_nal_type
, const block_t
*p_frag
)
974 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
977 int i_first_mb
, i_slice_type
;
981 /* do not convert the whole frame */
982 CreateDecodedNAL( &pb_dec
, &i_dec
, &p_frag
->p_buffer
[5],
983 __MIN( p_frag
->i_buffer
- 5, 60 ) );
984 bs_init( &s
, pb_dec
, i_dec
);
986 /* first_mb_in_slice */
987 i_first_mb
= bs_read_ue( &s
);
990 switch( (i_slice_type
= bs_read_ue( &s
)) )
993 slice
.i_frame_type
= BLOCK_FLAG_TYPE_P
;
996 slice
.i_frame_type
= BLOCK_FLAG_TYPE_B
;
999 slice
.i_frame_type
= BLOCK_FLAG_TYPE_I
;
1001 case 3: case 8: /* SP */
1002 slice
.i_frame_type
= BLOCK_FLAG_TYPE_P
;
1005 slice
.i_frame_type
= BLOCK_FLAG_TYPE_I
;
1008 slice
.i_frame_type
= 0;
1013 slice
.i_nal_type
= i_nal_type
;
1014 slice
.i_nal_ref_idc
= i_nal_ref_idc
;
1016 slice
.i_pic_parameter_set_id
= bs_read_ue( &s
);
1017 slice
.i_frame_num
= bs_read( &s
, p_sys
->i_log2_max_frame_num
+ 4 );
1019 slice
.i_field_pic_flag
= 0;
1020 slice
.i_bottom_field_flag
= -1;
1021 if( !p_sys
->b_frame_mbs_only
)
1023 /* field_pic_flag */
1024 slice
.i_field_pic_flag
= bs_read( &s
, 1 );
1025 if( slice
.i_field_pic_flag
)
1026 slice
.i_bottom_field_flag
= bs_read( &s
, 1 );
1029 slice
.i_idr_pic_id
= p_sys
->slice
.i_idr_pic_id
;
1030 if( slice
.i_nal_type
== NAL_SLICE_IDR
)
1031 slice
.i_idr_pic_id
= bs_read_ue( &s
);
1033 slice
.i_pic_order_cnt_lsb
= -1;
1034 slice
.i_delta_pic_order_cnt_bottom
= -1;
1035 slice
.i_delta_pic_order_cnt0
= 0;
1036 slice
.i_delta_pic_order_cnt1
= 0;
1037 if( p_sys
->i_pic_order_cnt_type
== 0 )
1039 slice
.i_pic_order_cnt_lsb
= bs_read( &s
, p_sys
->i_log2_max_pic_order_cnt_lsb
+ 4 );
1040 if( p_sys
->i_pic_order_present_flag
&& !slice
.i_field_pic_flag
)
1041 slice
.i_delta_pic_order_cnt_bottom
= bs_read_se( &s
);
1043 else if( (p_sys
->i_pic_order_cnt_type
== 1) &&
1044 (!p_sys
->i_delta_pic_order_always_zero_flag
) )
1046 slice
.i_delta_pic_order_cnt0
= bs_read_se( &s
);
1047 if( p_sys
->i_pic_order_present_flag
&& !slice
.i_field_pic_flag
)
1048 slice
.i_delta_pic_order_cnt1
= bs_read_se( &s
);
1052 /* Detection of the first VCL NAL unit of a primary coded picture
1053 * (cf. 7.4.1.2.4) */
1055 if( slice
.i_frame_num
!= p_sys
->slice
.i_frame_num
||
1056 slice
.i_pic_parameter_set_id
!= p_sys
->slice
.i_pic_parameter_set_id
||
1057 slice
.i_field_pic_flag
!= p_sys
->slice
.i_field_pic_flag
||
1058 slice
.i_nal_ref_idc
!= p_sys
->slice
.i_nal_ref_idc
)
1060 if( (slice
.i_bottom_field_flag
!= -1) &&
1061 (p_sys
->slice
.i_bottom_field_flag
!= -1) &&
1062 (slice
.i_bottom_field_flag
!= p_sys
->slice
.i_bottom_field_flag
) )
1064 if( p_sys
->i_pic_order_cnt_type
== 0 &&
1065 ( slice
.i_pic_order_cnt_lsb
!= p_sys
->slice
.i_pic_order_cnt_lsb
||
1066 slice
.i_delta_pic_order_cnt_bottom
!= p_sys
->slice
.i_delta_pic_order_cnt_bottom
) )
1068 else if( p_sys
->i_pic_order_cnt_type
== 1 &&
1069 ( slice
.i_delta_pic_order_cnt0
!= p_sys
->slice
.i_delta_pic_order_cnt0
||
1070 slice
.i_delta_pic_order_cnt1
!= p_sys
->slice
.i_delta_pic_order_cnt1
) )
1072 if( ( slice
.i_nal_type
== NAL_SLICE_IDR
|| p_sys
->slice
.i_nal_type
== NAL_SLICE_IDR
) &&
1073 ( slice
.i_nal_type
!= p_sys
->slice
.i_nal_type
|| slice
.i_idr_pic_id
!= p_sys
->slice
.i_idr_pic_id
) )
1077 *pb_new_picture
= b_pic
;
1081 static void ParseSei( decoder_t
*p_dec
, block_t
*p_frag
)
1083 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1088 CreateDecodedNAL( &pb_dec
, &i_dec
, &p_frag
->p_buffer
[5], p_frag
->i_buffer
- 5 );
1092 /* The +1 is for rbsp trailing bits */
1093 for( int i_used
= 0; i_used
+1 < i_dec
; )
1097 while( i_used
+1 < i_dec
)
1099 const int i_byte
= pb_dec
[i_used
++];
1101 if( i_byte
!= 0xff )
1106 while( i_used
+1 < i_dec
)
1108 const int i_byte
= pb_dec
[i_used
++];
1110 if( i_byte
!= 0xff )
1114 if( i_used
+ i_size
+ 1 > i_dec
)
1117 /* Look for user_data_registered_itu_t_t35 */
1120 static const uint8_t p_dvb1_data_start_code
[] = {
1123 0x47, 0x41, 0x39, 0x34
1125 const int i_t35
= i_size
;
1126 const uint8_t *p_t35
= &pb_dec
[i_used
];
1128 /* Check for we have DVB1_data() */
1130 !memcmp( p_t35
, p_dvb1_data_start_code
, sizeof(p_dvb1_data_start_code
) ) )
1132 cc_Extract( &p_sys
->cc_next
, &p_t35
[3], i_t35
- 3 );