]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_parse.c
h264: factor out pred weight table parsing into a separate file
[ffmpeg] / libavcodec / h264_parse.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "get_bits.h"
20 #include "golomb.h"
21 #include "h264.h"
22 #include "h264_parse.h"
23
24 int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps,
25                               const int *ref_count, int slice_type_nos,
26                               H264PredWeightTable *pwt)
27 {
28     int list, i;
29     int luma_def, chroma_def;
30
31     pwt->use_weight             = 0;
32     pwt->use_weight_chroma      = 0;
33     pwt->luma_log2_weight_denom = get_ue_golomb(gb);
34     if (sps->chroma_format_idc)
35         pwt->chroma_log2_weight_denom = get_ue_golomb(gb);
36     luma_def   = 1 << pwt->luma_log2_weight_denom;
37     chroma_def = 1 << pwt->chroma_log2_weight_denom;
38
39     for (list = 0; list < 2; list++) {
40         pwt->luma_weight_flag[list]   = 0;
41         pwt->chroma_weight_flag[list] = 0;
42         for (i = 0; i < ref_count[list]; i++) {
43             int luma_weight_flag, chroma_weight_flag;
44
45             luma_weight_flag = get_bits1(gb);
46             if (luma_weight_flag) {
47                 pwt->luma_weight[i][list][0] = get_se_golomb(gb);
48                 pwt->luma_weight[i][list][1] = get_se_golomb(gb);
49                 if (pwt->luma_weight[i][list][0] != luma_def ||
50                     pwt->luma_weight[i][list][1] != 0) {
51                     pwt->use_weight             = 1;
52                     pwt->luma_weight_flag[list] = 1;
53                 }
54             } else {
55                 pwt->luma_weight[i][list][0] = luma_def;
56                 pwt->luma_weight[i][list][1] = 0;
57             }
58
59             if (sps->chroma_format_idc) {
60                 chroma_weight_flag = get_bits1(gb);
61                 if (chroma_weight_flag) {
62                     int j;
63                     for (j = 0; j < 2; j++) {
64                         pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
65                         pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
66                         if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
67                             pwt->chroma_weight[i][list][j][1] != 0) {
68                             pwt->use_weight_chroma        = 1;
69                             pwt->chroma_weight_flag[list] = 1;
70                         }
71                     }
72                 } else {
73                     int j;
74                     for (j = 0; j < 2; j++) {
75                         pwt->chroma_weight[i][list][j][0] = chroma_def;
76                         pwt->chroma_weight[i][list][j][1] = 0;
77                     }
78                 }
79             }
80         }
81         if (slice_type_nos != AV_PICTURE_TYPE_B)
82             break;
83     }
84     pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
85     return 0;
86 }