-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitstream_info.c
1064 lines (937 loc) · 29.8 KB
/
bitstream_info.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* bitstream_info.h
* Created by Graham Booker on 1/6/07.
*
* This file is part of Perian.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bitstream_info.h"
#include <AudioToolbox/AudioToolbox.h>
#include <QuickTime/QuickTime.h>
#import "ac3tab.h"
#import "CommonUtils.h"
//ffmpeg's struct Picture conflicts with QuickDraw's
#define Picture MPEGPICTURE
#include "avcodec.h"
#include "bswap.h"
#include "libavutil/intmath.h"
#include "libavutil/internal.h"
#include "mpegvideo.h"
#include "parser.h"
#include "golomb.h"
#include "mpeg4video.h"
#include "mpeg4video_parser.h"
#include "Codecprintf.h"
#include "CodecIDs.h"
#undef malloc
#undef free
static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
static const int ac3_layout_no_lfe[8] = {
kAudioChannelLayoutTag_Stereo,
kAudioChannelLayoutTag_Mono,
kAudioChannelLayoutTag_Stereo,
kAudioChannelLayoutTag_ITU_3_0,
kAudioChannelLayoutTag_ITU_2_1,
kAudioChannelLayoutTag_ITU_3_1,
kAudioChannelLayoutTag_ITU_2_2,
kAudioChannelLayoutTag_ITU_3_2};
static const int ac3_layout_lfe[8] = {
kAudioChannelLayoutTag_DVD_4,
kAudioChannelLayoutTag_Mono, //No layout for this, hopefully we never have it
kAudioChannelLayoutTag_DVD_4,
kAudioChannelLayoutTag_DVD_10,
kAudioChannelLayoutTag_DVD_5,
kAudioChannelLayoutTag_DVD_11,
kAudioChannelLayoutTag_DVD_6,
kAudioChannelLayoutTag_ITU_3_2_1};
static const uint16_t ac3_freqs[3] = { 48000, 44100, 32000 };
static const uint16_t ac3_bitratetab[] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640};
static const uint8_t ac3_halfrate[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
/* From: http://svn.mplayerhq.hu/ac3/ (LGPL)
* Synchronize to ac3 bitstream.
* This function searches for the syncword '0xb77'.
*
* @param buf Pointer to "probable" ac3 bitstream buffer
* @param buf_size Size of buffer
* @return Returns the position where syncword is found, -1 if no syncword is found
*/
static int ac3_synchronize(uint8_t *buf, int buf_size)
{
int i;
for (i = 0; i < buf_size - 1; i++)
if (buf[i] == 0x0b && buf[i + 1] == 0x77)
return i;
return -1;
}
/* A lot of this was stolen from: http://svn.mplayerhq.hu/ac3/ (LGPL)
* Fill info from an ac3 stream
*
* @param asdb Pointer to the AudioStreamBasicDescription to fill
* @param acl Pointer to the AudioChannelLayout to fill
* @param buffer Pointer to the buffer data to scan
* @param buff_size Size of the buffer
* @return 1 if successfull, 0 otherwise
*/
int parse_ac3_bitstream(AudioStreamBasicDescription *asbd, AudioChannelLayout *acl, uint8_t *buffer, int buff_size)
{
int offset = ac3_synchronize(buffer, buff_size);
if(offset == -1)
return 0;
if(buff_size < offset + 7)
return 0;
uint8_t fscod_and_frmsizecod = buffer[offset + 4];
uint8_t fscod = fscod_and_frmsizecod >> 6;
uint8_t frmsizecod = fscod_and_frmsizecod & 0x3f;
if(frmsizecod >= 38)
return 0;
uint8_t bsid = buffer[offset + 5] >> 3;
if(bsid >= 0x12)
return 0;
uint8_t acmod = buffer[offset + 6] >> 5;
uint8_t shift = 4;
if(acmod & 0x01 && acmod != 0x01)
shift -= 2;
if(acmod & 0x04)
shift -= 2;
if(acmod == 0x02)
shift -= 2;
uint8_t lfe = (buffer[offset + 6] >> shift) & 0x01;
/* This is a valid frame!!! */
uint16_t bitrate = ac3_bitratetab[frmsizecod >> 1];
uint8_t half = ac3_halfrate[bsid];
int sample_rate = ac3_freqs[fscod] >> half;
int framesize = 0;
switch (fscod) {
case 0:
framesize = 4 * bitrate;
break;
case 1:
framesize = (320 * bitrate / 147 + (frmsizecod & 1 ? 1 : 0)) * 2;
break;
case 2:
framesize = 6 * bitrate;
break;
default:
break;
}
shift = 0;
if(bsid > 8)
shift = bsid - 8;
/* Setup the AudioStreamBasicDescription and AudioChannelLayout */
memset(asbd, 0, sizeof(AudioStreamBasicDescription));
asbd->mSampleRate = sample_rate >> shift;
asbd->mFormatID = kAudioFormatAC3MS;
asbd->mChannelsPerFrame = nfchans_tbl[acmod] + lfe;
asbd->mFramesPerPacket = 1536;
memset(acl, 0, sizeof(AudioChannelLayout));
if(lfe)
acl->mChannelLayoutTag = ac3_layout_lfe[acmod];
else
acl->mChannelLayoutTag = ac3_layout_no_lfe[acmod];
return 1;
}
static int parse_mpeg4_extra(FFusionParserContext *parser, const uint8_t *buf, int buf_size)
{
ParseContext1 *pc1 = (ParseContext1 *)parser->pc->priv_data;
pc1->pc.frame_start_found = 0;
MpegEncContext *s = pc1->enc;
GetBitContext gb1, *gb = &gb1;
s->avctx = parser->avctx;
s->current_picture_ptr = &s->current_picture;
init_get_bits(gb, buf, 8 * buf_size);
ff_mpeg4_decode_picture_header(s, gb);
return 1;
}
/*
* Long story short, FFMpeg's parsers suck for our use. This function parses an mpeg4 bitstream,
* and assumes that it is given at least a full frame of data.
* @param parser A FFusionParserContext structure containg all our info
* @param buf The buffer to parse
* @param buf_size Size of the input buffer
* @param out_buf_size The number of bytes present in the first frame of data
* @param type The frame Type: FF_*_TYPE
* @param pts The PTS of the frame
* @return 1 if a frame is found, 0 otherwise
*/
static int parse_mpeg4_stream(FFusionParserContext *parser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped)
{
ParseContext1 *pc1 = (ParseContext1 *)parser->pc->priv_data;
pc1->pc.frame_start_found = 0;
int endOfFrame = ff_mpeg4_find_frame_end(&(pc1->pc), buf, buf_size);
MpegEncContext *s = pc1->enc;
GetBitContext gb1, *gb = &gb1;
s->avctx = parser->avctx;
s->current_picture_ptr = &s->current_picture;
init_get_bits(gb, buf, 8 * buf_size);
int parse_res = ff_mpeg4_decode_picture_header(s, gb);
if(parse_res == FRAME_SKIPPED) {
*out_buf_size = buf_size;
*type = FF_P_TYPE;
*skippable = 1;
*skipped = 1;
}
if(parse_res != 0)
return 0;
*type = s->pict_type;
*skippable = (*type == FF_B_TYPE);
*skipped = 0;
#if 0 /*this was an attempt to figure out the PTS information and detect an out of order P frame before we hit its B frame */
int64_t *lastPtsPtr = (int64_t *)parser->internalContext;
int64_t lastPts = *lastPtsPtr;
int64_t currentPts = s->current_picture.pts;
switch(s->pict_type)
{
case FF_I_TYPE:
*lastPtsPtr = currentPts;
*precedesAPastFrame = 0;
break;
case FF_P_TYPE:
if(currentPts > lastPts + 1)
*precedesAPastFrame = 1;
else
*precedesAPastFrame = 0;
*lastPtsPtr = currentPts;
break;
case FF_B_TYPE:
*precedesAPastFrame = 0;
break;
default:
break;
}
#endif
if(endOfFrame == END_NOT_FOUND)
*out_buf_size = buf_size;
else
*out_buf_size = endOfFrame;
return 1;
}
static int parse_mpeg12_stream(FFusionParserContext *ffparser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped)
{
const uint8_t *out_unused;
int size_unused;
AVCodecParser *parser = ffparser->pc->parser;
parser->parser_parse(ffparser->pc, ffparser->avctx, &out_unused, &size_unused, buf, buf_size);
*out_buf_size = buf_size;
*type = ffparser->pc->pict_type;
*skippable = *type == FF_B_TYPE;
*skipped = 0;
return 1;
}
extern AVCodecParser mpeg4video_parser;
static FFusionParser ffusionMpeg4VideoParser = {
&mpeg4video_parser,
0,
NULL,
parse_mpeg4_extra,
parse_mpeg4_stream,
};
extern AVCodecParser mpegvideo_parser;
static FFusionParser ffusionMpeg12VideoParser = {
&mpegvideo_parser,
0,
NULL,
NULL,
parse_mpeg12_stream,
};
typedef struct H264ParserContext_s
{
int is_avc;
int nal_length_size;
int prevPts;
int profile_idc;
int level_idc;
int poc_type;
int log2_max_frame_num;
int frame_mbs_only_flag;
int num_reorder_frames;
int pic_order_present_flag;
int log2_max_poc_lsb;
int poc_msb;
int prev_poc_lsb;
int delta_pic_order_always_zero_flag;
int offset_for_non_ref_pic;
int num_ref_frames_in_pic_order_cnt_cycle;
int sum_of_offset_for_ref_frames;
int chroma_format_idc;
int gaps_in_frame_num_value_allowed_flag;
}H264ParserContext;
static int decode_nal(const uint8_t *buf, int buf_size, uint8_t *out_buf, int *out_buf_size, int *type, int *nal_ref_idc)
{
int i;
int outIndex = 0;
int numNulls = 0;
for(i=1; i<buf_size; i++)
{
if(buf[i] == 0)
numNulls++;
else if(numNulls == 2)
{
numNulls = 0;
if(buf[i] < 3)
{
/* end of nal */
outIndex -= 2;
break;
}
else if(buf[i] == 3)
/* This is just a simple escape of 0x00 00 03 */
continue;
}
else
numNulls = 0;
out_buf[outIndex] = buf[i];
outIndex++;
}
if(outIndex <= 0)
return 0;
*type = buf[0] & 0x1f;
*nal_ref_idc = (buf[0] >> 5) & 0x03;
*out_buf_size = outIndex;
return 1;
}
static void skip_scaling_list(GetBitContext *gb, int size){
int i, next = 8, last = 8;
if(get_bits1(gb)) /* matrix not written, we use the predicted one */
for(i=0;i<size;i++){
if(next)
next = (last + get_se_golomb(gb)) & 0xff;
if(!i && !next){ /* matrix not written, we use the preset one */
break;
}
last = next ? next : last;
}
}
static void skip_scaling_matrices(GetBitContext *gb){
if(get_bits1(gb)){
skip_scaling_list(gb, 16); // Intra, Y
skip_scaling_list(gb, 16); // Intra, Cr
skip_scaling_list(gb, 16); // Intra, Cb
skip_scaling_list(gb, 16); // Inter, Y
skip_scaling_list(gb, 16); // Inter, Cr
skip_scaling_list(gb, 16); // Inter, Cb
skip_scaling_list(gb, 64); // Intra, Y
skip_scaling_list(gb, 64); // Inter, Y
}
}
static void skip_hrd_parameters(GetBitContext *gb)
{
int cpb_cnt_minus1 = get_ue_golomb(gb);
get_bits(gb, 4); //bit_rate_scale
get_bits(gb, 4); //cpb_size_scale
int i;
for(i=0; i<=cpb_cnt_minus1; i++)
{
get_ue_golomb(gb); //bit_rate_value_minus1[i]
get_ue_golomb(gb); //cpb_size_value_minus1[i]
get_bits1(gb); //cbr_flag[i]
}
get_bits(gb, 5); //initial_cpb_removal_delay_length_minus1
get_bits(gb, 5); //cpb_removal_delay_length_minus1
get_bits(gb, 5); //dpb_output_delay_length_minus1
get_bits(gb, 5); //time_offset_length
}
static void decode_sps(H264ParserContext *context, const uint8_t *buf, int buf_size)
{
GetBitContext getbit, *gb = &getbit;
init_get_bits(gb, buf, 8 * buf_size);
context->profile_idc= get_bits(gb, 8);
get_bits1(gb); //constraint_set0_flag
get_bits1(gb); //constraint_set1_flag
get_bits1(gb); //constraint_set2_flag
get_bits1(gb); //constraint_set3_flag
get_bits(gb, 4); //reserved
context->level_idc = get_bits(gb, 8); //level_idc
get_ue_golomb(gb); //seq_parameter_set_id
if(context->profile_idc >= 100)
{
context->chroma_format_idc = get_ue_golomb(gb);
//high profile
if(context->chroma_format_idc == 3) //chroma_format_idc
get_bits1(gb); //residual_color_transfrom_flag
get_ue_golomb(gb); //bit_depth_luma_minus8
get_ue_golomb(gb); //bit_depth_chroma_minus8
get_bits1(gb); //transform_bypass
skip_scaling_matrices(gb);
}
context->log2_max_frame_num = get_ue_golomb(gb) + 4;
context->poc_type = get_ue_golomb(gb);
if(context->poc_type == 0)
context->log2_max_poc_lsb = get_ue_golomb(gb) + 4;
else if(context->poc_type == 1)
{
int i;
context->delta_pic_order_always_zero_flag = get_bits1(gb);
context->offset_for_non_ref_pic = get_se_golomb(gb);
get_se_golomb(gb); //offset_for_top_to_bottom_field
context->num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(gb);
context->sum_of_offset_for_ref_frames = 0;
for(i=0; i<context->num_ref_frames_in_pic_order_cnt_cycle; i++)
context->sum_of_offset_for_ref_frames += get_se_golomb(gb); //offset_for_ref_frame[i]
}
get_ue_golomb(gb); //num_ref_frames
context->gaps_in_frame_num_value_allowed_flag = get_bits1(gb); //gaps_in_frame_num_value_allowed_flag
get_ue_golomb(gb); //pic_width_in_mbs_minus1
get_ue_golomb(gb); //pic_height_in_map_units_minus1
int mbs_only = get_bits1(gb);
context->frame_mbs_only_flag = mbs_only;
#if 1 //This is a test to get num_reorder_frames
if(!mbs_only)
get_bits1(gb); //mb_adaptive_frame_field_flag
get_bits1(gb); //direct_8x8_inference_flag
if(get_bits1(gb)) //frame_cropping_flag
{
get_ue_golomb(gb); //frame_crop_left_offset
get_ue_golomb(gb); //frame_crop_right_offset
get_ue_golomb(gb); //frame_crop_top_offset
get_ue_golomb(gb); //frame_crop_bottom_offset
}
if(get_bits1(gb)) //vui_parameters_present_flag
{
if(get_bits1(gb)) //aspect_ratio_info_present_flag
{
if(get_bits(gb, 8) == 255) //aspect_ratio_idc
{
get_bits(gb, 16); //sar_width
get_bits(gb, 16); //sar_height
}
}
if(get_bits1(gb)) //overscan_info_present_flag
get_bits1(gb); //overscan_appropriate_flag
if(get_bits1(gb)) //video_signal_type_present_flag
{
get_bits(gb, 3); //video_format
get_bits1(gb); //video_full_range_flag
if(get_bits1(gb)) //colour_description_present_flag
{
get_bits(gb, 8); //colour_primaries
get_bits(gb, 8); //transfer_characteristics
get_bits(gb, 8); //matrix_coefficients
}
}
if(get_bits1(gb)) //chroma_loc_info_present_flag
{
get_ue_golomb(gb); //chroma_sample_loc_type_top_field
get_ue_golomb(gb); //chroma_sample_loc_type_bottom_field
}
if(get_bits1(gb)) //timing_info_present_flag
{
get_bits(gb, 32); //num_units_in_tick
get_bits(gb, 32); //time_scale
get_bits1(gb); //fixed_frame_rate_flag
}
int nal_hrd_parameters_present_flag = get_bits1(gb);
if(nal_hrd_parameters_present_flag)
{
skip_hrd_parameters(gb);
}
int vcl_hrd_parameters_present_flag = get_bits1(gb);
if(vcl_hrd_parameters_present_flag)
{
skip_hrd_parameters(gb);
}
if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
get_bits1(gb); //low_delay_hrd_flag
get_bits1(gb); //pic_struct_present_flag
if(get_bits1(gb)) //bitstream_restriction_flag
{
get_bits1(gb); //motion_vectors_over_pic_boundaries_flag
get_ue_golomb(gb); //max_bytes_per_pic_denom
get_ue_golomb(gb); //max_bits_per_mb_denom
get_ue_golomb(gb); //log2_max_mv_length_horizontal
get_ue_golomb(gb); //log2_max_mv_length_vertical
context->num_reorder_frames = get_ue_golomb(gb);
get_ue_golomb(gb); //max_dec_frame_buffering
}
}
#endif
}
static void decode_pps(H264ParserContext *context, const uint8_t *buf, int buf_size)
{
GetBitContext getbit, *gb = &getbit;
init_get_bits(gb, buf, 8 * buf_size);
get_ue_golomb(gb); //pic_parameter_set_id
get_ue_golomb(gb); //seq_parameter_set_id
get_bits1(gb); //entropy_coding_mode_flag
context->pic_order_present_flag = get_bits1(gb);
}
static int inline decode_slice_header(H264ParserContext *context, const uint8_t *buf, int buf_size, int nal_ref_idc, int nal_type, int just_type, int *type, int *pts)
{
GetBitContext getbit, *gb = &getbit;
int slice_type;
int field_pic_flag = 0;
int bottom_field_flag = 0;
int frame_number;
// static const uint8_t slice_type_map[5] = {FF_P_TYPE, FF_B_TYPE, FF_I_TYPE, FF_SP_TYPE, FF_SI_TYPE};
static const uint8_t slice_type_map[5] = {FF_P_TYPE, FF_P_TYPE, FF_I_TYPE, FF_SP_TYPE, FF_SI_TYPE};
init_get_bits(gb, buf, 8 * buf_size);
get_ue_golomb(gb); //first_mb_in_slice
slice_type = get_ue_golomb(gb);
if(slice_type > 9)
return 0;
if(slice_type > 4)
slice_type -= 5;
*type = slice_type_map[slice_type];
if(just_type)
return 1;
get_ue_golomb(gb); //pic_parameter_set_id
frame_number = get_bits(gb, context->log2_max_frame_num);
if(!context->frame_mbs_only_flag)
{
field_pic_flag = get_bits1(gb);
if(field_pic_flag)
{
bottom_field_flag = get_bits1(gb);
}
}
if(nal_type == 5)
get_ue_golomb(gb); //idr_pic_id
if(context->poc_type == 0)
{
int pts_lsb = get_bits(gb, context->log2_max_poc_lsb);
int delta_pic_order_cnt_bottom = 0;
int maxPicOrderCntLsb = 1 << context->log2_max_poc_lsb;
int pic_order_msb;
if(context->pic_order_present_flag && !field_pic_flag)
delta_pic_order_cnt_bottom = get_se_golomb(gb);
if((pts_lsb < context->prev_poc_lsb) && (context->prev_poc_lsb - pts_lsb) >= maxPicOrderCntLsb)
pic_order_msb = context->poc_msb + maxPicOrderCntLsb;
else if((pts_lsb > context->prev_poc_lsb) && (pts_lsb - context->prev_poc_lsb) > maxPicOrderCntLsb)
pic_order_msb = context->poc_msb - maxPicOrderCntLsb;
else
pic_order_msb = context->poc_msb;
context->poc_msb = pic_order_msb;
*pts = pic_order_msb + pts_lsb;
if(delta_pic_order_cnt_bottom < 0)
*pts += delta_pic_order_cnt_bottom;
}
else if(context->poc_type == 1 && !context->delta_pic_order_always_zero_flag)
{
int delta_pic_order_cnt[2] = {0, 0};
delta_pic_order_cnt[0] = get_se_golomb(gb);
if(context->pic_order_present_flag && !field_pic_flag)
delta_pic_order_cnt[1] = get_se_golomb(gb);
int frame_num_offset = 0; //I think this is wrong, but the pts code isn't used anywhere, so no harm yet and this removes a warning.
int abs_frame_num = 0;
int num_ref_frames_in_pic_order_cnt_cycle = context->num_ref_frames_in_pic_order_cnt_cycle;
if(num_ref_frames_in_pic_order_cnt_cycle != 0)
abs_frame_num = frame_num_offset + frame_number;
if(nal_ref_idc == 0 && abs_frame_num > 0)
abs_frame_num--;
int expected_delta_per_poc_cycle = context->sum_of_offset_for_ref_frames;
int expectedpoc = 0;
if(abs_frame_num > 0)
{
int poc_cycle_cnt = (abs_frame_num - 1) / num_ref_frames_in_pic_order_cnt_cycle;
expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle + context->sum_of_offset_for_ref_frames;
}
if(nal_ref_idc == 0)
expectedpoc = expectedpoc + context->offset_for_non_ref_pic;
*pts = expectedpoc + delta_pic_order_cnt[0];
}
return 1;
}
#define NAL_PEEK_SIZE 32
static int inline decode_nals(H264ParserContext *context, const uint8_t *buf, int buf_size, int *type, int *skippable)
{
int nalsize = 0;
int buf_index = 0;
int ret = 0;
int pts_decoded = 0;
int lowestType = 20;
*skippable = 1;
#if 0 /*this was an attempt to figure out the PTS information and detect an out of order P frame before we hit its B frame */
if(context->poc_type == 2)
{
//decode and display order are the same
pts_decoded = 1;
*precedesAPastFrame = 0;
}
#endif
for(;;)
{
if(context->is_avc)
{
if(buf_index >= buf_size)
break;
nalsize = 0;
switch (context->nal_length_size) {
case 1:
nalsize = buf[buf_index];
buf_index++;
break;
case 2:
nalsize = (buf[buf_index] << 8) | buf[buf_index+1];
buf_index += 2;
break;
case 3:
nalsize = (buf[buf_index] << 16) | (buf[buf_index+1] << 8) | buf[buf_index + 2];
buf_index += 3;
break;
case 4:
nalsize = (buf[buf_index] << 24) | (buf[buf_index+1] << 16) | (buf[buf_index + 2] << 8) | buf[buf_index + 3];
buf_index += 4;
break;
default:
break;
}
if(nalsize <= 1 || nalsize > buf_size)
{
if(nalsize == 1)
{
buf_index++;
continue;
}
else
break;
}
}
else
{
int num_nuls = 0;
int start_offset = 0;
//do start code prefix search
for(; buf_index < buf_size; buf_index++)
{
if(buf[buf_index] == 0)
num_nuls++;
else
{
if(num_nuls >= 2 && buf[buf_index] == 1)
break;
num_nuls = 0;
}
}
start_offset = buf_index + 1;
//do start code prefix search
for(buf_index++; buf_index < buf_size; buf_index++)
{
if(buf[buf_index] == 0)
{
if(num_nuls == 2)
break;
num_nuls++;
}
else
{
if(num_nuls == 2 && buf[buf_index] == 1)
break;
num_nuls = 0;
}
}
if(start_offset >= buf_size)
//no more
break;
nalsize = buf_index - start_offset;
if(buf_index < buf_size)
//Take off the next NAL's startcode
nalsize -= 2;
//skip the start code
buf_index = start_offset;
}
uint8_t partOfNal[NAL_PEEK_SIZE];
int decodedNalSize, nalType;
int nal_ref_idc;
int slice_type = 0;
if(decode_nal(buf + buf_index, FFMIN(nalsize, NAL_PEEK_SIZE), partOfNal, &decodedNalSize, &nalType, &nal_ref_idc))
{
int pts = 0;
if(nalType == 1 || nalType == 2)
{
if(decode_slice_header(context, partOfNal, decodedNalSize, nal_ref_idc, nalType, pts_decoded, &slice_type, &pts))
{
ret = 1;
if(slice_type < lowestType)
lowestType = slice_type;
if(nal_ref_idc)
*skippable = 0;
if(pts_decoded == 0)
{
pts_decoded = 1;
if(pts > context->prevPts)
{
if(pts < context->prevPts)
lowestType = FF_B_TYPE;
context->prevPts = pts;
}
}
}
// Parser users assume I-frames are IDR-frames
// but in H.264 they don't have to be.
// Mark these as P-frames if they effectively are.
if (lowestType == FF_I_TYPE) lowestType = FF_P_TYPE;
}
else if(nalType == 5)
{
ret = 1;
#if 0 /*this was an attempt to figure out the PTS information and detect an out of order P frame before we hit its B frame */
context->prev_poc_lsb = 0;
context->poc_msb = 0;
context->prevPts = 0;
*precedesAPastFrame = 0;
#endif
*skippable = 0;
lowestType = FF_I_TYPE;
}
}
buf_index += nalsize;
}
if(lowestType != 20)
*type = lowestType;
return ret;
}
/*
* This function parses an h.264 bitstream, and assumes that it is given at least a full frame of data.
* @param parser A FFusionParserContext structure containg all our info
* @param buf The buffer to parse
* @param buf_size Size of the input buffer
* @param out_buf_size The number of bytes present in the first frame of data
* @param type The frame Type: FF_*_TYPE
* @param pts The PTS of the frame
* @return 1 if a frame is found, 0 otherwise
*/
static int parse_h264_stream(FFusionParserContext *parser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped)
{
int endOfFrame;
int size = 0;
const uint8_t *parseBuf = buf;
int parseSize;
/*
* Somehow figure out of frame type
* For our use, a frame with any B slices is a B frame, and then a frame with any P slices is a P frame.
* An I frame has only I slices.
* I expect this involves a NAL decoder, and then look at the slice types.
* Nal is a f(1) always set to 0, u(2) of nal_ref_idc, and then u(5) of nal_unit_type.
* Nal types 1, 2 start a non-I frame, and type 5 starts an I frame. Each start with a slice header.
* Slice header has a ue(v) for first_mb_in_slice and then a ue(v) for the slice_type
* Slice types 0, 5 are P, 1, 6 are B, 2, 7 are I
*/
do
{
parseBuf = parseBuf + size;
parseSize = buf_size - size;
endOfFrame = (parser->parserStructure->avparse->split)(parser->avctx, parseBuf, parseSize);
if(endOfFrame == 0)
size = buf_size;
else
{
size += endOfFrame;
parseSize = endOfFrame;
}
}while(decode_nals(parser->internalContext, parseBuf, parseSize, type, skippable) == 0 && size < buf_size);
*skipped = 0;
*out_buf_size = size;
return 1;
}
static int init_h264_parser(FFusionParserContext *parser)
{
H264ParserContext *context = parser->internalContext;
context->nal_length_size = 2;
context->is_avc = 0;
return 1;
}
static int parse_extra_data_h264(FFusionParserContext *parser, const uint8_t *buf, int buf_size)
{
H264ParserContext *context = parser->internalContext;
const uint8_t *cur = buf;
int count, i, type, ref;
context->is_avc = 1;
count = *(cur+5) & 0x1f;
cur += 6;
for (i=0; i<count; i++)
{
int size = AV_RB16(cur);
int out_size = 0;
uint8_t *decoded = av_mallocz(size+FF_INPUT_BUFFER_PADDING_SIZE);
if(decode_nal(cur + 2, size, decoded, &out_size, &type, &ref))
decode_sps(context, decoded, out_size);
cur += size + 2;
free(decoded);
}
count = *(cur++);
for (i=0; i<count; i++)
{
int size = AV_RB16(cur);
int out_size = 0;
uint8_t *decoded = av_mallocz(size+FF_INPUT_BUFFER_PADDING_SIZE);
if(decode_nal(cur + 2, size, decoded, &out_size, &type, &ref))
decode_pps(context, decoded, out_size);
cur += size + 2;
free(decoded);
}
context->nal_length_size = ((*(buf+4)) & 0x03) + 1;
return 1;
}
extern AVCodecParser h264_parser;
static FFusionParser ffusionH264Parser = {
&h264_parser,
sizeof(H264ParserContext),
init_h264_parser,
parse_extra_data_h264,
parse_h264_stream,
};
static FFusionParser *ffusionFirstParser = NULL;
void registerFFusionParsers(FFusionParser *parser)
{
parser->next = ffusionFirstParser;
ffusionFirstParser = parser;
}
void initFFusionParsers()
{
static Boolean inited = FALSE;
int unlock = PerianInitEnter(&inited);
if(!inited)
{
inited = TRUE;
registerFFusionParsers(&ffusionMpeg4VideoParser);
registerFFusionParsers(&ffusionH264Parser);
registerFFusionParsers(&ffusionMpeg12VideoParser);
}
PerianInitExit(unlock);
}
void ffusionParserFree(FFusionParserContext *parser)
{
AVCodecParser *avparse = parser->parserStructure->avparse;
if(parser->pc)
{
if (avparse->parser_close)
avparse->parser_close(parser->pc);
av_free(parser->pc->priv_data);
av_free(parser->pc);
}
av_free(parser->avctx);
av_free(parser->internalContext);
free(parser);
}
FFusionParserContext *ffusionParserInit(int codec_id)
{
AVCodecParserContext *s;
AVCodecParser *parser;
FFusionParser *ffParser;
int ret, i;
if(codec_id == CODEC_ID_NONE)
return NULL;
if (!ffusionFirstParser) initFFusionParsers();
for(ffParser = ffusionFirstParser; ffParser != NULL; ffParser = ffParser->next) {
parser = ffParser->avparse;
for (i = 0; i < 5; i++)
if (parser->codec_ids[i] == codec_id)
goto found;
}
return NULL;
found:
s = av_mallocz(sizeof(AVCodecParserContext));
if (!s)
return NULL;
s->parser = parser;
s->priv_data = av_mallocz(parser->priv_data_size);
if (!s->priv_data) {
av_free(s);
return NULL;
}
if (parser->parser_init) {
ret = parser->parser_init(s);
if (ret != 0) {
av_free(s->priv_data);
av_free(s);
return NULL;
}
}
s->fetch_timestamp=1;
s->flags |= PARSER_FLAG_COMPLETE_FRAMES;
FFusionParserContext *parserContext = malloc(sizeof(FFusionParserContext));
parserContext->avctx = avcodec_alloc_context();
parserContext->pc = s;
parserContext->parserStructure = ffParser;
if(ffParser->internalContextSize)
parserContext->internalContext = av_mallocz(ffParser->internalContextSize);
else
parserContext->internalContext = NULL;
if(ffParser->init)
(ffParser->init)(parserContext);
return parserContext;
}
/*
* @param parser FFusionParserContext pointer
* @param buf The buffer to parse
* @param buf_size Size of the input buffer
* @return 1 if successful, 0 otherwise
*/
int ffusionParseExtraData(FFusionParserContext *parser, const uint8_t *buf, int buf_size)
{
if(parser->parserStructure->extra_data)
return (parser->parserStructure->extra_data)(parser, buf, buf_size);
return 1;
}
/*
* @param parser FFusionParserContext pointer
* @param buf The buffer to parse
* @param buf_size Size of the input buffer
* @param out_buf_size The number of bytes present in the first frame of data