forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h264_slice.c
2873 lines (2512 loc) · 108 KB
/
h264_slice.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
/*
* H.26L/H.264/AVC/JVT/14496-10/... decoder
* Copyright (c) 2003 Michael Niedermayer <[email protected]>
*
* This file is part of FFmpeg.
*
* FFmpeg 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.
*
* FFmpeg 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
*/
/**
* @file
* H.264 / AVC / MPEG-4 part10 codec.
* @author Michael Niedermayer <[email protected]>
*/
#include "libavutil/avassert.h"
#include "libavutil/display.h"
#include "libavutil/imgutils.h"
#include "libavutil/stereo3d.h"
#include "internal.h"
#include "cabac.h"
#include "cabac_functions.h"
#include "error_resilience.h"
#include "avcodec.h"
#include "h264.h"
#include "h264dec.h"
#include "h264data.h"
#include "h264chroma.h"
#include "h264_mvpred.h"
#include "h264_ps.h"
#include "golomb.h"
#include "mathops.h"
#include "mpegutils.h"
#include "mpegvideo.h"
#include "rectangle.h"
#include "thread.h"
static const uint8_t field_scan[16+1] = {
0 + 0 * 4, 0 + 1 * 4, 1 + 0 * 4, 0 + 2 * 4,
0 + 3 * 4, 1 + 1 * 4, 1 + 2 * 4, 1 + 3 * 4,
2 + 0 * 4, 2 + 1 * 4, 2 + 2 * 4, 2 + 3 * 4,
3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4,
};
static const uint8_t field_scan8x8[64+1] = {
0 + 0 * 8, 0 + 1 * 8, 0 + 2 * 8, 1 + 0 * 8,
1 + 1 * 8, 0 + 3 * 8, 0 + 4 * 8, 1 + 2 * 8,
2 + 0 * 8, 1 + 3 * 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 + 2 * 8, 3 + 6 * 8,
5 + 5 * 8, 6 + 5 * 8, 6 + 6 * 8, 7 + 7 * 8,
};
static void release_unused_pictures(H264Context *h, int remove_current)
{
int i;
/* release non reference frames */
for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
if (h->DPB[i].f->buf[0] && !h->DPB[i].reference &&
(remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
ff_h264_unref_picture(h, &h->DPB[i]);
}
}
}
static int alloc_scratch_buffers(H264SliceContext *sl, int linesize)
{
const H264Context *h = sl->h264;
int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
av_fast_malloc(&sl->bipred_scratchpad, &sl->bipred_scratchpad_allocated, 16 * 6 * alloc_size);
// edge emu needs blocksize + filter length - 1
// (= 21x21 for H.264)
av_fast_malloc(&sl->edge_emu_buffer, &sl->edge_emu_buffer_allocated, alloc_size * 2 * 21);
av_fast_mallocz(&sl->top_borders[0], &sl->top_borders_allocated[0],
h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
av_fast_mallocz(&sl->top_borders[1], &sl->top_borders_allocated[1],
h->mb_width * 16 * 3 * sizeof(uint8_t) * 2);
if (!sl->bipred_scratchpad || !sl->edge_emu_buffer ||
!sl->top_borders[0] || !sl->top_borders[1]) {
av_freep(&sl->bipred_scratchpad);
av_freep(&sl->edge_emu_buffer);
av_freep(&sl->top_borders[0]);
av_freep(&sl->top_borders[1]);
sl->bipred_scratchpad_allocated = 0;
sl->edge_emu_buffer_allocated = 0;
sl->top_borders_allocated[0] = 0;
sl->top_borders_allocated[1] = 0;
return AVERROR(ENOMEM);
}
return 0;
}
static int init_table_pools(H264Context *h)
{
const int big_mb_num = h->mb_stride * (h->mb_height + 1) + 1;
const int mb_array_size = h->mb_stride * h->mb_height;
const int b4_stride = h->mb_width * 4 + 1;
const int b4_array_size = b4_stride * h->mb_height * 4;
h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
av_buffer_allocz);
h->mb_type_pool = av_buffer_pool_init((big_mb_num + h->mb_stride) *
sizeof(uint32_t), av_buffer_allocz);
h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
sizeof(int16_t), av_buffer_allocz);
h->ref_index_pool = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
!h->ref_index_pool) {
av_buffer_pool_uninit(&h->qscale_table_pool);
av_buffer_pool_uninit(&h->mb_type_pool);
av_buffer_pool_uninit(&h->motion_val_pool);
av_buffer_pool_uninit(&h->ref_index_pool);
return AVERROR(ENOMEM);
}
return 0;
}
static int alloc_picture(H264Context *h, H264Picture *pic)
{
int i, ret = 0;
av_assert0(!pic->f->data[0]);
pic->tf.f = pic->f;
ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
AV_GET_BUFFER_FLAG_REF : 0);
if (ret < 0)
goto fail;
if (h->avctx->hwaccel) {
const AVHWAccel *hwaccel = h->avctx->hwaccel;
av_assert0(!pic->hwaccel_picture_private);
if (hwaccel->frame_priv_data_size) {
pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
if (!pic->hwaccel_priv_buf)
return AVERROR(ENOMEM);
pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
}
}
if (CONFIG_GRAY && !h->avctx->hwaccel && h->flags & AV_CODEC_FLAG_GRAY && pic->f->data[2]) {
int h_chroma_shift, v_chroma_shift;
av_pix_fmt_get_chroma_sub_sample(pic->f->format,
&h_chroma_shift, &v_chroma_shift);
for(i=0; i<AV_CEIL_RSHIFT(pic->f->height, v_chroma_shift); i++) {
memset(pic->f->data[1] + pic->f->linesize[1]*i,
0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
memset(pic->f->data[2] + pic->f->linesize[2]*i,
0x80, AV_CEIL_RSHIFT(pic->f->width, h_chroma_shift));
}
}
if (!h->qscale_table_pool) {
ret = init_table_pools(h);
if (ret < 0)
goto fail;
}
pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
pic->mb_type_buf = av_buffer_pool_get(h->mb_type_pool);
if (!pic->qscale_table_buf || !pic->mb_type_buf)
goto fail;
pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
for (i = 0; i < 2; i++) {
pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
pic->ref_index_buf[i] = av_buffer_pool_get(h->ref_index_pool);
if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
goto fail;
pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
pic->ref_index[i] = pic->ref_index_buf[i]->data;
}
pic->pps_buf = av_buffer_ref(h->ps.pps_ref);
if (!pic->pps_buf)
goto fail;
pic->pps = (const PPS*)pic->pps_buf->data;
pic->mb_width = h->mb_width;
pic->mb_height = h->mb_height;
pic->mb_stride = h->mb_stride;
return 0;
fail:
ff_h264_unref_picture(h, pic);
return (ret < 0) ? ret : AVERROR(ENOMEM);
}
static int find_unused_picture(H264Context *h)
{
int i;
for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
if (!h->DPB[i].f->buf[0])
return i;
}
return AVERROR_INVALIDDATA;
}
#define IN_RANGE(a, b, size) (((void*)(a) >= (void*)(b)) && ((void*)(a) < (void*)((b) + (size))))
#define REBASE_PICTURE(pic, new_ctx, old_ctx) \
(((pic) && (pic) >= (old_ctx)->DPB && \
(pic) < (old_ctx)->DPB + H264_MAX_PICTURE_COUNT) ? \
&(new_ctx)->DPB[(pic) - (old_ctx)->DPB] : NULL)
static void copy_picture_range(H264Picture **to, H264Picture **from, int count,
H264Context *new_base,
H264Context *old_base)
{
int i;
for (i = 0; i < count; i++) {
av_assert1(!from[i] ||
IN_RANGE(from[i], old_base, 1) ||
IN_RANGE(from[i], old_base->DPB, H264_MAX_PICTURE_COUNT));
to[i] = REBASE_PICTURE(from[i], new_base, old_base);
}
}
static int h264_slice_header_init(H264Context *h);
int ff_h264_update_thread_context(AVCodecContext *dst,
const AVCodecContext *src)
{
H264Context *h = dst->priv_data, *h1 = src->priv_data;
int inited = h->context_initialized, err = 0;
int need_reinit = 0;
int i, ret;
if (dst == src)
return 0;
if (inited && !h1->ps.sps)
return AVERROR_INVALIDDATA;
if (inited &&
(h->width != h1->width ||
h->height != h1->height ||
h->mb_width != h1->mb_width ||
h->mb_height != h1->mb_height ||
!h->ps.sps ||
h->ps.sps->bit_depth_luma != h1->ps.sps->bit_depth_luma ||
h->ps.sps->chroma_format_idc != h1->ps.sps->chroma_format_idc ||
h->ps.sps->colorspace != h1->ps.sps->colorspace)) {
need_reinit = 1;
}
/* copy block_offset since frame_start may not be called */
memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
// SPS/PPS
for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++) {
av_buffer_unref(&h->ps.sps_list[i]);
if (h1->ps.sps_list[i]) {
h->ps.sps_list[i] = av_buffer_ref(h1->ps.sps_list[i]);
if (!h->ps.sps_list[i])
return AVERROR(ENOMEM);
}
}
for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
av_buffer_unref(&h->ps.pps_list[i]);
if (h1->ps.pps_list[i]) {
h->ps.pps_list[i] = av_buffer_ref(h1->ps.pps_list[i]);
if (!h->ps.pps_list[i])
return AVERROR(ENOMEM);
}
}
av_buffer_unref(&h->ps.pps_ref);
h->ps.pps = NULL;
h->ps.sps = NULL;
if (h1->ps.pps_ref) {
h->ps.pps_ref = av_buffer_ref(h1->ps.pps_ref);
if (!h->ps.pps_ref)
return AVERROR(ENOMEM);
h->ps.pps = (const PPS*)h->ps.pps_ref->data;
h->ps.sps = h->ps.pps->sps;
}
if (need_reinit || !inited) {
h->width = h1->width;
h->height = h1->height;
h->mb_height = h1->mb_height;
h->mb_width = h1->mb_width;
h->mb_num = h1->mb_num;
h->mb_stride = h1->mb_stride;
h->b_stride = h1->b_stride;
h->x264_build = h1->x264_build;
if (h->context_initialized || h1->context_initialized) {
if ((err = h264_slice_header_init(h)) < 0) {
av_log(h->avctx, AV_LOG_ERROR, "h264_slice_header_init() failed");
return err;
}
}
/* copy block_offset since frame_start may not be called */
memcpy(h->block_offset, h1->block_offset, sizeof(h->block_offset));
}
h->avctx->coded_height = h1->avctx->coded_height;
h->avctx->coded_width = h1->avctx->coded_width;
h->avctx->width = h1->avctx->width;
h->avctx->height = h1->avctx->height;
h->width_from_caller = h1->width_from_caller;
h->height_from_caller = h1->height_from_caller;
h->coded_picture_number = h1->coded_picture_number;
h->first_field = h1->first_field;
h->picture_structure = h1->picture_structure;
h->mb_aff_frame = h1->mb_aff_frame;
h->droppable = h1->droppable;
for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
ff_h264_unref_picture(h, &h->DPB[i]);
if (h1->DPB[i].f->buf[0] &&
(ret = ff_h264_ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
return ret;
}
h->cur_pic_ptr = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
ff_h264_unref_picture(h, &h->cur_pic);
if (h1->cur_pic.f->buf[0]) {
ret = ff_h264_ref_picture(h, &h->cur_pic, &h1->cur_pic);
if (ret < 0)
return ret;
}
h->enable_er = h1->enable_er;
h->workaround_bugs = h1->workaround_bugs;
h->droppable = h1->droppable;
// extradata/NAL handling
h->is_avc = h1->is_avc;
h->nal_length_size = h1->nal_length_size;
memcpy(&h->poc, &h1->poc, sizeof(h->poc));
memcpy(h->short_ref, h1->short_ref, sizeof(h->short_ref));
memcpy(h->long_ref, h1->long_ref, sizeof(h->long_ref));
memcpy(h->delayed_pic, h1->delayed_pic, sizeof(h->delayed_pic));
memcpy(h->last_pocs, h1->last_pocs, sizeof(h->last_pocs));
h->next_output_pic = h1->next_output_pic;
h->next_outputed_poc = h1->next_outputed_poc;
memcpy(h->mmco, h1->mmco, sizeof(h->mmco));
h->nb_mmco = h1->nb_mmco;
h->mmco_reset = h1->mmco_reset;
h->explicit_ref_marking = h1->explicit_ref_marking;
h->long_ref_count = h1->long_ref_count;
h->short_ref_count = h1->short_ref_count;
copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
copy_picture_range(h->long_ref, h1->long_ref, 32, h, h1);
copy_picture_range(h->delayed_pic, h1->delayed_pic,
MAX_DELAYED_PIC_COUNT + 2, h, h1);
h->frame_recovered = h1->frame_recovered;
av_buffer_unref(&h->sei.a53_caption.buf_ref);
if (h1->sei.a53_caption.buf_ref) {
h->sei.a53_caption.buf_ref = av_buffer_ref(h1->sei.a53_caption.buf_ref);
if (!h->sei.a53_caption.buf_ref)
return AVERROR(ENOMEM);
}
if (!h->cur_pic_ptr)
return 0;
if (!h->droppable) {
err = ff_h264_execute_ref_pic_marking(h);
h->poc.prev_poc_msb = h->poc.poc_msb;
h->poc.prev_poc_lsb = h->poc.poc_lsb;
}
h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
h->poc.prev_frame_num = h->poc.frame_num;
h->recovery_frame = h1->recovery_frame;
return err;
}
static int h264_frame_start(H264Context *h)
{
H264Picture *pic;
int i, ret;
const int pixel_shift = h->pixel_shift;
if (!ff_thread_can_start_frame(h->avctx)) {
av_log(h->avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
return -1;
}
release_unused_pictures(h, 1);
h->cur_pic_ptr = NULL;
i = find_unused_picture(h);
if (i < 0) {
av_log(h->avctx, AV_LOG_ERROR, "no frame buffer available\n");
return i;
}
pic = &h->DPB[i];
pic->reference = h->droppable ? 0 : h->picture_structure;
pic->f->coded_picture_number = h->coded_picture_number++;
pic->field_picture = h->picture_structure != PICT_FRAME;
pic->frame_num = h->poc.frame_num;
/*
* Zero key_frame here; IDR markings per slice in frame or fields are ORed
* in later.
* See decode_nal_units().
*/
pic->f->key_frame = 0;
pic->mmco_reset = 0;
pic->recovered = 0;
pic->invalid_gap = 0;
pic->sei_recovery_frame_cnt = h->sei.recovery_point.recovery_frame_cnt;
pic->f->pict_type = h->slice_ctx[0].slice_type;
pic->f->crop_left = h->crop_left;
pic->f->crop_right = h->crop_right;
pic->f->crop_top = h->crop_top;
pic->f->crop_bottom = h->crop_bottom;
if ((ret = alloc_picture(h, pic)) < 0)
return ret;
h->cur_pic_ptr = pic;
ff_h264_unref_picture(h, &h->cur_pic);
if (CONFIG_ERROR_RESILIENCE) {
ff_h264_set_erpic(&h->slice_ctx[0].er.cur_pic, NULL);
}
if ((ret = ff_h264_ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
return ret;
for (i = 0; i < h->nb_slice_ctx; i++) {
h->slice_ctx[i].linesize = h->cur_pic_ptr->f->linesize[0];
h->slice_ctx[i].uvlinesize = h->cur_pic_ptr->f->linesize[1];
}
if (CONFIG_ERROR_RESILIENCE && h->enable_er) {
ff_er_frame_start(&h->slice_ctx[0].er);
ff_h264_set_erpic(&h->slice_ctx[0].er.last_pic, NULL);
ff_h264_set_erpic(&h->slice_ctx[0].er.next_pic, NULL);
}
for (i = 0; i < 16; i++) {
h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[0] * ((scan8[i] - scan8[0]) >> 3);
}
for (i = 0; i < 16; i++) {
h->block_offset[16 + i] =
h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
h->block_offset[48 + 16 + i] =
h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * pic->f->linesize[1] * ((scan8[i] - scan8[0]) >> 3);
}
/* We mark the current picture as non-reference after allocating it, so
* that if we break out due to an error it can be released automatically
* in the next ff_mpv_frame_start().
*/
h->cur_pic_ptr->reference = 0;
h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
h->next_output_pic = NULL;
h->postpone_filter = 0;
h->mb_aff_frame = h->ps.sps->mb_aff && (h->picture_structure == PICT_FRAME);
if (h->sei.unregistered.x264_build >= 0)
h->x264_build = h->sei.unregistered.x264_build;
assert(h->cur_pic_ptr->long_ref == 0);
return 0;
}
static av_always_inline void backup_mb_border(const H264Context *h, H264SliceContext *sl,
uint8_t *src_y,
uint8_t *src_cb, uint8_t *src_cr,
int linesize, int uvlinesize,
int simple)
{
uint8_t *top_border;
int top_idx = 1;
const int pixel_shift = h->pixel_shift;
int chroma444 = CHROMA444(h);
int chroma422 = CHROMA422(h);
src_y -= linesize;
src_cb -= uvlinesize;
src_cr -= uvlinesize;
if (!simple && FRAME_MBAFF(h)) {
if (sl->mb_y & 1) {
if (!MB_MBAFF(sl)) {
top_border = sl->top_borders[0][sl->mb_x];
AV_COPY128(top_border, src_y + 15 * linesize);
if (pixel_shift)
AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
if (chroma444) {
if (pixel_shift) {
AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
} else {
AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
}
} else if (chroma422) {
if (pixel_shift) {
AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
} else {
AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
}
} else {
if (pixel_shift) {
AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
} else {
AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
}
}
}
}
} else if (MB_MBAFF(sl)) {
top_idx = 0;
} else
return;
}
top_border = sl->top_borders[top_idx][sl->mb_x];
/* There are two lines saved, the line above the top macroblock
* of a pair, and the line above the bottom macroblock. */
AV_COPY128(top_border, src_y + 16 * linesize);
if (pixel_shift)
AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
if (chroma444) {
if (pixel_shift) {
AV_COPY128(top_border + 32, src_cb + 16 * linesize);
AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
AV_COPY128(top_border + 64, src_cr + 16 * linesize);
AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
} else {
AV_COPY128(top_border + 16, src_cb + 16 * linesize);
AV_COPY128(top_border + 32, src_cr + 16 * linesize);
}
} else if (chroma422) {
if (pixel_shift) {
AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
} else {
AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
}
} else {
if (pixel_shift) {
AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
} else {
AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
}
}
}
}
/**
* Initialize implicit_weight table.
* @param field 0/1 initialize the weight for interlaced MBAFF
* -1 initializes the rest
*/
static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
{
int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
for (i = 0; i < 2; i++) {
sl->pwt.luma_weight_flag[i] = 0;
sl->pwt.chroma_weight_flag[i] = 0;
}
if (field < 0) {
if (h->picture_structure == PICT_FRAME) {
cur_poc = h->cur_pic_ptr->poc;
} else {
cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
}
if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
sl->ref_list[0][0].poc + (int64_t)sl->ref_list[1][0].poc == 2LL * cur_poc) {
sl->pwt.use_weight = 0;
sl->pwt.use_weight_chroma = 0;
return;
}
ref_start = 0;
ref_count0 = sl->ref_count[0];
ref_count1 = sl->ref_count[1];
} else {
cur_poc = h->cur_pic_ptr->field_poc[field];
ref_start = 16;
ref_count0 = 16 + 2 * sl->ref_count[0];
ref_count1 = 16 + 2 * sl->ref_count[1];
}
sl->pwt.use_weight = 2;
sl->pwt.use_weight_chroma = 2;
sl->pwt.luma_log2_weight_denom = 5;
sl->pwt.chroma_log2_weight_denom = 5;
for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
int64_t poc0 = sl->ref_list[0][ref0].poc;
for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
int w = 32;
if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
int poc1 = sl->ref_list[1][ref1].poc;
int td = av_clip_int8(poc1 - poc0);
if (td) {
int tb = av_clip_int8(cur_poc - poc0);
int tx = (16384 + (FFABS(td) >> 1)) / td;
int dist_scale_factor = (tb * tx + 32) >> 8;
if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
w = 64 - dist_scale_factor;
}
}
if (field < 0) {
sl->pwt.implicit_weight[ref0][ref1][0] =
sl->pwt.implicit_weight[ref0][ref1][1] = w;
} else {
sl->pwt.implicit_weight[ref0][ref1][field] = w;
}
}
}
}
/**
* initialize scan tables
*/
static void init_scan_tables(H264Context *h)
{
int i;
for (i = 0; i < 16; i++) {
#define TRANSPOSE(x) ((x) >> 2) | (((x) << 2) & 0xF)
h->zigzag_scan[i] = TRANSPOSE(ff_zigzag_scan[i]);
h->field_scan[i] = TRANSPOSE(field_scan[i]);
#undef TRANSPOSE
}
for (i = 0; i < 64; i++) {
#define TRANSPOSE(x) ((x) >> 3) | (((x) & 7) << 3)
h->zigzag_scan8x8[i] = TRANSPOSE(ff_zigzag_direct[i]);
h->zigzag_scan8x8_cavlc[i] = TRANSPOSE(zigzag_scan8x8_cavlc[i]);
h->field_scan8x8[i] = TRANSPOSE(field_scan8x8[i]);
h->field_scan8x8_cavlc[i] = TRANSPOSE(field_scan8x8_cavlc[i]);
#undef TRANSPOSE
}
if (h->ps.sps->transform_bypass) { // FIXME same ugly
memcpy(h->zigzag_scan_q0 , ff_zigzag_scan , sizeof(h->zigzag_scan_q0 ));
memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
} else {
memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
}
}
static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
{
#define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
(CONFIG_H264_D3D11VA_HWACCEL * 2) + \
CONFIG_H264_NVDEC_HWACCEL + \
CONFIG_H264_VAAPI_HWACCEL + \
CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
CONFIG_H264_VDPAU_HWACCEL)
enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
const enum AVPixelFormat *choices = pix_fmts;
int i;
switch (h->ps.sps->bit_depth_luma) {
case 9:
if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
*fmt++ = AV_PIX_FMT_GBRP9;
} else
*fmt++ = AV_PIX_FMT_YUV444P9;
} else if (CHROMA422(h))
*fmt++ = AV_PIX_FMT_YUV422P9;
else
*fmt++ = AV_PIX_FMT_YUV420P9;
break;
case 10:
if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
*fmt++ = AV_PIX_FMT_GBRP10;
} else
*fmt++ = AV_PIX_FMT_YUV444P10;
} else if (CHROMA422(h))
*fmt++ = AV_PIX_FMT_YUV422P10;
else
*fmt++ = AV_PIX_FMT_YUV420P10;
break;
case 12:
if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
*fmt++ = AV_PIX_FMT_GBRP12;
} else
*fmt++ = AV_PIX_FMT_YUV444P12;
} else if (CHROMA422(h))
*fmt++ = AV_PIX_FMT_YUV422P12;
else
*fmt++ = AV_PIX_FMT_YUV420P12;
break;
case 14:
if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB) {
*fmt++ = AV_PIX_FMT_GBRP14;
} else
*fmt++ = AV_PIX_FMT_YUV444P14;
} else if (CHROMA422(h))
*fmt++ = AV_PIX_FMT_YUV422P14;
else
*fmt++ = AV_PIX_FMT_YUV420P14;
break;
case 8:
#if CONFIG_H264_VDPAU_HWACCEL
*fmt++ = AV_PIX_FMT_VDPAU;
#endif
#if CONFIG_H264_NVDEC_HWACCEL
*fmt++ = AV_PIX_FMT_CUDA;
#endif
if (CHROMA444(h)) {
if (h->avctx->colorspace == AVCOL_SPC_RGB)
*fmt++ = AV_PIX_FMT_GBRP;
else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
*fmt++ = AV_PIX_FMT_YUVJ444P;
else
*fmt++ = AV_PIX_FMT_YUV444P;
} else if (CHROMA422(h)) {
if (h->avctx->color_range == AVCOL_RANGE_JPEG)
*fmt++ = AV_PIX_FMT_YUVJ422P;
else
*fmt++ = AV_PIX_FMT_YUV422P;
} else {
#if CONFIG_H264_DXVA2_HWACCEL
*fmt++ = AV_PIX_FMT_DXVA2_VLD;
#endif
#if CONFIG_H264_D3D11VA_HWACCEL
*fmt++ = AV_PIX_FMT_D3D11VA_VLD;
*fmt++ = AV_PIX_FMT_D3D11;
#endif
#if CONFIG_H264_VAAPI_HWACCEL
*fmt++ = AV_PIX_FMT_VAAPI;
#endif
#if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
*fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
#endif
if (h->avctx->codec->pix_fmts)
choices = h->avctx->codec->pix_fmts;
else if (h->avctx->color_range == AVCOL_RANGE_JPEG)
*fmt++ = AV_PIX_FMT_YUVJ420P;
else
*fmt++ = AV_PIX_FMT_YUV420P;
}
break;
default:
av_log(h->avctx, AV_LOG_ERROR,
"Unsupported bit depth %d\n", h->ps.sps->bit_depth_luma);
return AVERROR_INVALIDDATA;
}
*fmt = AV_PIX_FMT_NONE;
for (i=0; choices[i] != AV_PIX_FMT_NONE; i++)
if (choices[i] == h->avctx->pix_fmt && !force_callback)
return choices[i];
return ff_thread_get_format(h->avctx, choices);
}
/* export coded and cropped frame dimensions to AVCodecContext */
static void init_dimensions(H264Context *h)
{
const SPS *sps = (const SPS*)h->ps.sps;
int cr = sps->crop_right;
int cl = sps->crop_left;
int ct = sps->crop_top;
int cb = sps->crop_bottom;
int width = h->width - (cr + cl);
int height = h->height - (ct + cb);
av_assert0(sps->crop_right + sps->crop_left < (unsigned)h->width);
av_assert0(sps->crop_top + sps->crop_bottom < (unsigned)h->height);
/* handle container cropping */
if (h->width_from_caller > 0 && h->height_from_caller > 0 &&
!sps->crop_top && !sps->crop_left &&
FFALIGN(h->width_from_caller, 16) == FFALIGN(width, 16) &&
FFALIGN(h->height_from_caller, 16) == FFALIGN(height, 16) &&
h->width_from_caller <= width &&
h->height_from_caller <= height) {
width = h->width_from_caller;
height = h->height_from_caller;
cl = 0;
ct = 0;
cr = h->width - width;
cb = h->height - height;
} else {
h->width_from_caller = 0;
h->height_from_caller = 0;
}
h->avctx->coded_width = h->width;
h->avctx->coded_height = h->height;
h->avctx->width = width;
h->avctx->height = height;
h->crop_right = cr;
h->crop_left = cl;
h->crop_top = ct;
h->crop_bottom = cb;
}
static int h264_slice_header_init(H264Context *h)
{
const SPS *sps = h->ps.sps;
int i, ret;
if (!sps) {
ret = AVERROR_INVALIDDATA;
goto fail;
}
ff_set_sar(h->avctx, sps->sar);
av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
&h->chroma_x_shift, &h->chroma_y_shift);
if (sps->timing_info_present_flag) {
int64_t den = sps->time_scale;
if (h->x264_build < 44U)
den *= 2;
av_reduce(&h->avctx->framerate.den, &h->avctx->framerate.num,
sps->num_units_in_tick * h->avctx->ticks_per_frame, den, 1 << 30);
}
ff_h264_free_tables(h);
h->first_field = 0;
h->prev_interlaced_frame = 1;
init_scan_tables(h);
ret = ff_h264_alloc_tables(h);
if (ret < 0) {
av_log(h->avctx, AV_LOG_ERROR, "Could not allocate memory\n");
goto fail;
}
if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 14 ||
sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13
) {
av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth %d\n",
sps->bit_depth_luma);
ret = AVERROR_INVALIDDATA;
goto fail;
}
h->cur_bit_depth_luma =
h->avctx->bits_per_raw_sample = sps->bit_depth_luma;
h->cur_chroma_format_idc = sps->chroma_format_idc;
h->pixel_shift = sps->bit_depth_luma > 8;
h->chroma_format_idc = sps->chroma_format_idc;
h->bit_depth_luma = sps->bit_depth_luma;
ff_h264dsp_init(&h->h264dsp, sps->bit_depth_luma,
sps->chroma_format_idc);
ff_h264chroma_init(&h->h264chroma, sps->bit_depth_chroma);
ff_h264qpel_init(&h->h264qpel, sps->bit_depth_luma);
ff_h264_pred_init(&h->hpc, h->avctx->codec_id, sps->bit_depth_luma,
sps->chroma_format_idc);
ff_videodsp_init(&h->vdsp, sps->bit_depth_luma);
if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_SLICE)) {
ret = ff_h264_slice_context_init(h, &h->slice_ctx[0]);
if (ret < 0) {
av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
goto fail;
}
} else {
for (i = 0; i < h->nb_slice_ctx; i++) {
H264SliceContext *sl = &h->slice_ctx[i];
sl->h264 = h;
sl->intra4x4_pred_mode = h->intra4x4_pred_mode + i * 8 * 2 * h->mb_stride;
sl->mvd_table[0] = h->mvd_table[0] + i * 8 * 2 * h->mb_stride;
sl->mvd_table[1] = h->mvd_table[1] + i * 8 * 2 * h->mb_stride;
if ((ret = ff_h264_slice_context_init(h, sl)) < 0) {
av_log(h->avctx, AV_LOG_ERROR, "context_init() failed.\n");
goto fail;
}
}
}
h->context_initialized = 1;
return 0;
fail:
ff_h264_free_tables(h);
h->context_initialized = 0;
return ret;
}
static enum AVPixelFormat non_j_pixfmt(enum AVPixelFormat a)
{
switch (a) {