xref: /aosp_15_r20/external/libaom/av1/encoder/thirdpass.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/thirdpass.h"
12*77c1e3ccSAndroid Build Coastguard Worker 
13*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_THREE_PASS && CONFIG_AV1_DECODER
14*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_codec.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomdx.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/psnr.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/av1_iface_common.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/firstpass.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/blockd.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "common/ivfdec.h"
23*77c1e3ccSAndroid Build Coastguard Worker 
setup_two_pass_stream_input(struct AvxInputContext ** input_ctx_ptr,const char * input_file_name,struct aom_internal_error_info * err_info)24*77c1e3ccSAndroid Build Coastguard Worker static void setup_two_pass_stream_input(
25*77c1e3ccSAndroid Build Coastguard Worker     struct AvxInputContext **input_ctx_ptr, const char *input_file_name,
26*77c1e3ccSAndroid Build Coastguard Worker     struct aom_internal_error_info *err_info) {
27*77c1e3ccSAndroid Build Coastguard Worker   FILE *infile;
28*77c1e3ccSAndroid Build Coastguard Worker   infile = fopen(input_file_name, "rb");
29*77c1e3ccSAndroid Build Coastguard Worker   if (!infile) {
30*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(err_info, AOM_CODEC_INVALID_PARAM,
31*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to open input file '%s'.", input_file_name);
32*77c1e3ccSAndroid Build Coastguard Worker   }
33*77c1e3ccSAndroid Build Coastguard Worker   struct AvxInputContext *aom_input_ctx = aom_malloc(sizeof(*aom_input_ctx));
34*77c1e3ccSAndroid Build Coastguard Worker   if (!aom_input_ctx) {
35*77c1e3ccSAndroid Build Coastguard Worker     fclose(infile);
36*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(err_info, AOM_CODEC_MEM_ERROR,
37*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to allocate memory for third-pass context.");
38*77c1e3ccSAndroid Build Coastguard Worker   }
39*77c1e3ccSAndroid Build Coastguard Worker   memset(aom_input_ctx, 0, sizeof(*aom_input_ctx));
40*77c1e3ccSAndroid Build Coastguard Worker   aom_input_ctx->filename = input_file_name;
41*77c1e3ccSAndroid Build Coastguard Worker   aom_input_ctx->file = infile;
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker   if (file_is_ivf(aom_input_ctx)) {
44*77c1e3ccSAndroid Build Coastguard Worker     aom_input_ctx->file_type = FILE_TYPE_IVF;
45*77c1e3ccSAndroid Build Coastguard Worker   } else {
46*77c1e3ccSAndroid Build Coastguard Worker     fclose(infile);
47*77c1e3ccSAndroid Build Coastguard Worker     aom_free(aom_input_ctx);
48*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(err_info, AOM_CODEC_INVALID_PARAM,
49*77c1e3ccSAndroid Build Coastguard Worker                        "Unrecognized input file type.");
50*77c1e3ccSAndroid Build Coastguard Worker   }
51*77c1e3ccSAndroid Build Coastguard Worker   *input_ctx_ptr = aom_input_ctx;
52*77c1e3ccSAndroid Build Coastguard Worker }
53*77c1e3ccSAndroid Build Coastguard Worker 
init_third_pass(THIRD_PASS_DEC_CTX * ctx)54*77c1e3ccSAndroid Build Coastguard Worker static void init_third_pass(THIRD_PASS_DEC_CTX *ctx) {
55*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx->input_ctx) {
56*77c1e3ccSAndroid Build Coastguard Worker     if (ctx->input_file_name == NULL) {
57*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(ctx->err_info, AOM_CODEC_INVALID_PARAM,
58*77c1e3ccSAndroid Build Coastguard Worker                          "No third pass input specified.");
59*77c1e3ccSAndroid Build Coastguard Worker     }
60*77c1e3ccSAndroid Build Coastguard Worker     setup_two_pass_stream_input(&ctx->input_ctx, ctx->input_file_name,
61*77c1e3ccSAndroid Build Coastguard Worker                                 ctx->err_info);
62*77c1e3ccSAndroid Build Coastguard Worker   }
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx->decoder.iface) {
65*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_iface_t *decoder_iface = &aom_codec_av1_inspect_algo;
66*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_dec_init(&ctx->decoder, decoder_iface, NULL, 0)) {
67*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
68*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to initialize decoder.");
69*77c1e3ccSAndroid Build Coastguard Worker     }
70*77c1e3ccSAndroid Build Coastguard Worker   }
71*77c1e3ccSAndroid Build Coastguard Worker }
72*77c1e3ccSAndroid Build Coastguard Worker 
73*77c1e3ccSAndroid Build Coastguard Worker // Return 0: success
74*77c1e3ccSAndroid Build Coastguard Worker //        1: cannot read because this is end of file
75*77c1e3ccSAndroid Build Coastguard Worker //       -1: failure to read the frame
read_frame(THIRD_PASS_DEC_CTX * ctx)76*77c1e3ccSAndroid Build Coastguard Worker static int read_frame(THIRD_PASS_DEC_CTX *ctx) {
77*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx->input_ctx || !ctx->decoder.iface) {
78*77c1e3ccSAndroid Build Coastguard Worker     init_third_pass(ctx);
79*77c1e3ccSAndroid Build Coastguard Worker   }
80*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx->have_frame) {
81*77c1e3ccSAndroid Build Coastguard Worker     if (ivf_read_frame(ctx->input_ctx, &ctx->buf, &ctx->bytes_in_buffer,
82*77c1e3ccSAndroid Build Coastguard Worker                        &ctx->buffer_size, NULL) != 0) {
83*77c1e3ccSAndroid Build Coastguard Worker       if (feof(ctx->input_ctx->file)) {
84*77c1e3ccSAndroid Build Coastguard Worker         return 1;
85*77c1e3ccSAndroid Build Coastguard Worker       } else {
86*77c1e3ccSAndroid Build Coastguard Worker         return -1;
87*77c1e3ccSAndroid Build Coastguard Worker       }
88*77c1e3ccSAndroid Build Coastguard Worker     }
89*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame = ctx->buf;
90*77c1e3ccSAndroid Build Coastguard Worker     ctx->end_frame = ctx->frame + ctx->bytes_in_buffer;
91*77c1e3ccSAndroid Build Coastguard Worker     ctx->have_frame = 1;
92*77c1e3ccSAndroid Build Coastguard Worker   }
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker   Av1DecodeReturn adr;
95*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_decode(&ctx->decoder, ctx->frame,
96*77c1e3ccSAndroid Build Coastguard Worker                        (unsigned int)ctx->bytes_in_buffer,
97*77c1e3ccSAndroid Build Coastguard Worker                        &adr) != AOM_CODEC_OK) {
98*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
99*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to decode frame for third pass.");
100*77c1e3ccSAndroid Build Coastguard Worker   }
101*77c1e3ccSAndroid Build Coastguard Worker   ctx->this_frame_bits = (int)(adr.buf - ctx->frame) << 3;
102*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame = adr.buf;
103*77c1e3ccSAndroid Build Coastguard Worker   ctx->bytes_in_buffer = ctx->end_frame - ctx->frame;
104*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->frame == ctx->end_frame) ctx->have_frame = 0;
105*77c1e3ccSAndroid Build Coastguard Worker   return 0;
106*77c1e3ccSAndroid Build Coastguard Worker }
107*77c1e3ccSAndroid Build Coastguard Worker 
free_frame_info(THIRD_PASS_FRAME_INFO * frame_info)108*77c1e3ccSAndroid Build Coastguard Worker static void free_frame_info(THIRD_PASS_FRAME_INFO *frame_info) {
109*77c1e3ccSAndroid Build Coastguard Worker   if (!frame_info) return;
110*77c1e3ccSAndroid Build Coastguard Worker   aom_free(frame_info->mi_info);
111*77c1e3ccSAndroid Build Coastguard Worker   frame_info->mi_info = NULL;
112*77c1e3ccSAndroid Build Coastguard Worker }
113*77c1e3ccSAndroid Build Coastguard Worker 
114*77c1e3ccSAndroid Build Coastguard Worker // This function gets the information needed from the recently decoded frame,
115*77c1e3ccSAndroid Build Coastguard Worker // via various decoder APIs, and saves the info into ctx->frame_info.
116*77c1e3ccSAndroid Build Coastguard Worker // Return 0: success
117*77c1e3ccSAndroid Build Coastguard Worker //        1: cannot read because this is end of file
118*77c1e3ccSAndroid Build Coastguard Worker //       -1: failure to read the frame
get_frame_info(THIRD_PASS_DEC_CTX * ctx)119*77c1e3ccSAndroid Build Coastguard Worker static int get_frame_info(THIRD_PASS_DEC_CTX *ctx) {
120*77c1e3ccSAndroid Build Coastguard Worker   int ret = read_frame(ctx);
121*77c1e3ccSAndroid Build Coastguard Worker   if (ret != 0) return ret;
122*77c1e3ccSAndroid Build Coastguard Worker   int cur = ctx->frame_info_count;
123*77c1e3ccSAndroid Build Coastguard Worker 
124*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[cur].actual_bits = ctx->this_frame_bits;
125*77c1e3ccSAndroid Build Coastguard Worker 
126*77c1e3ccSAndroid Build Coastguard Worker   if (cur >= MAX_THIRD_PASS_BUF) {
127*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
128*77c1e3ccSAndroid Build Coastguard Worker                        "Third pass frame info ran out of available slots.");
129*77c1e3ccSAndroid Build Coastguard Worker   }
130*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_frame_flags_t frame_type_flags = 0;
131*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&ctx->decoder, AOMD_GET_FRAME_FLAGS,
132*77c1e3ccSAndroid Build Coastguard Worker                         &frame_type_flags) != AOM_CODEC_OK) {
133*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
134*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to read frame flags.");
135*77c1e3ccSAndroid Build Coastguard Worker   }
136*77c1e3ccSAndroid Build Coastguard Worker   if (frame_type_flags & AOM_FRAME_IS_KEY) {
137*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame_info[cur].frame_type = KEY_FRAME;
138*77c1e3ccSAndroid Build Coastguard Worker   } else if (frame_type_flags & AOM_FRAME_IS_INTRAONLY) {
139*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame_info[cur].frame_type = INTRA_ONLY_FRAME;
140*77c1e3ccSAndroid Build Coastguard Worker   } else if (frame_type_flags & AOM_FRAME_IS_SWITCH) {
141*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame_info[cur].frame_type = S_FRAME;
142*77c1e3ccSAndroid Build Coastguard Worker   } else {
143*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame_info[cur].frame_type = INTER_FRAME;
144*77c1e3ccSAndroid Build Coastguard Worker   }
145*77c1e3ccSAndroid Build Coastguard Worker 
146*77c1e3ccSAndroid Build Coastguard Worker   // Get frame width and height
147*77c1e3ccSAndroid Build Coastguard Worker   int frame_size[2];
148*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&ctx->decoder, AV1D_GET_FRAME_SIZE, frame_size) !=
149*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_OK) {
150*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
151*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to read frame size.");
152*77c1e3ccSAndroid Build Coastguard Worker   }
153*77c1e3ccSAndroid Build Coastguard Worker 
154*77c1e3ccSAndroid Build Coastguard Worker   // Check if we need to re-alloc the mi fields.
155*77c1e3ccSAndroid Build Coastguard Worker   const int mi_cols = (frame_size[0] + 3) >> 2;
156*77c1e3ccSAndroid Build Coastguard Worker   const int mi_rows = (frame_size[1] + 3) >> 2;
157*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[cur].mi_stride = mi_cols;
158*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[cur].mi_rows = mi_rows;
159*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[cur].mi_cols = mi_cols;
160*77c1e3ccSAndroid Build Coastguard Worker 
161*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->frame_info[cur].width != frame_size[0] ||
162*77c1e3ccSAndroid Build Coastguard Worker       ctx->frame_info[cur].height != frame_size[1] ||
163*77c1e3ccSAndroid Build Coastguard Worker       !ctx->frame_info[cur].mi_info) {
164*77c1e3ccSAndroid Build Coastguard Worker     free_frame_info(&ctx->frame_info[cur]);
165*77c1e3ccSAndroid Build Coastguard Worker 
166*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame_info[cur].mi_info =
167*77c1e3ccSAndroid Build Coastguard Worker         aom_malloc(mi_cols * mi_rows * sizeof(*ctx->frame_info[cur].mi_info));
168*77c1e3ccSAndroid Build Coastguard Worker 
169*77c1e3ccSAndroid Build Coastguard Worker     if (!ctx->frame_info[cur].mi_info) {
170*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(ctx->err_info, AOM_CODEC_MEM_ERROR,
171*77c1e3ccSAndroid Build Coastguard Worker                          "Failed to allocate mi buffer for the third pass.");
172*77c1e3ccSAndroid Build Coastguard Worker     }
173*77c1e3ccSAndroid Build Coastguard Worker   }
174*77c1e3ccSAndroid Build Coastguard Worker 
175*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[cur].width = frame_size[0];
176*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[cur].height = frame_size[1];
177*77c1e3ccSAndroid Build Coastguard Worker 
178*77c1e3ccSAndroid Build Coastguard Worker   // Get frame base q idx
179*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&ctx->decoder, AOMD_GET_BASE_Q_IDX,
180*77c1e3ccSAndroid Build Coastguard Worker                         &ctx->frame_info[cur].base_q_idx) != AOM_CODEC_OK) {
181*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
182*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to read base q index.");
183*77c1e3ccSAndroid Build Coastguard Worker   }
184*77c1e3ccSAndroid Build Coastguard Worker 
185*77c1e3ccSAndroid Build Coastguard Worker   // Get show existing frame flag
186*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&ctx->decoder, AOMD_GET_SHOW_EXISTING_FRAME_FLAG,
187*77c1e3ccSAndroid Build Coastguard Worker                         &ctx->frame_info[cur].is_show_existing_frame) !=
188*77c1e3ccSAndroid Build Coastguard Worker       AOM_CODEC_OK) {
189*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
190*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to read show existing frame flag.");
191*77c1e3ccSAndroid Build Coastguard Worker   }
192*77c1e3ccSAndroid Build Coastguard Worker 
193*77c1e3ccSAndroid Build Coastguard Worker   // Get show frame flag
194*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&ctx->decoder, AOMD_GET_SHOW_FRAME_FLAG,
195*77c1e3ccSAndroid Build Coastguard Worker                         &ctx->frame_info[cur].is_show_frame) != AOM_CODEC_OK) {
196*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
197*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to read show frame flag.");
198*77c1e3ccSAndroid Build Coastguard Worker   }
199*77c1e3ccSAndroid Build Coastguard Worker 
200*77c1e3ccSAndroid Build Coastguard Worker   // Get order hint
201*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&ctx->decoder, AOMD_GET_ORDER_HINT,
202*77c1e3ccSAndroid Build Coastguard Worker                         &ctx->frame_info[cur].order_hint) != AOM_CODEC_OK) {
203*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
204*77c1e3ccSAndroid Build Coastguard Worker                        "Failed to read order hint.");
205*77c1e3ccSAndroid Build Coastguard Worker   }
206*77c1e3ccSAndroid Build Coastguard Worker 
207*77c1e3ccSAndroid Build Coastguard Worker   // Clear MI info
208*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = 0; mi_row < mi_rows; mi_row++) {
209*77c1e3ccSAndroid Build Coastguard Worker     for (int mi_col = 0; mi_col < mi_cols; mi_col++) {
210*77c1e3ccSAndroid Build Coastguard Worker       ctx->frame_info[cur].mi_info[mi_row * mi_cols + mi_col].bsize =
211*77c1e3ccSAndroid Build Coastguard Worker           BLOCK_INVALID;
212*77c1e3ccSAndroid Build Coastguard Worker     }
213*77c1e3ccSAndroid Build Coastguard Worker   }
214*77c1e3ccSAndroid Build Coastguard Worker 
215*77c1e3ccSAndroid Build Coastguard Worker   // Get relevant information regarding each 4x4 MI
216*77c1e3ccSAndroid Build Coastguard Worker   MB_MODE_INFO cur_mi_info;
217*77c1e3ccSAndroid Build Coastguard Worker   THIRD_PASS_MI_INFO *const this_mi = ctx->frame_info[cur].mi_info;
218*77c1e3ccSAndroid Build Coastguard Worker   for (int mi_row = 0; mi_row < mi_rows; mi_row++) {
219*77c1e3ccSAndroid Build Coastguard Worker     for (int mi_col = 0; mi_col < mi_cols; mi_col++) {
220*77c1e3ccSAndroid Build Coastguard Worker       const int offset = mi_row * mi_cols + mi_col;
221*77c1e3ccSAndroid Build Coastguard Worker       if (this_mi[offset].bsize != BLOCK_INVALID) {
222*77c1e3ccSAndroid Build Coastguard Worker         continue;
223*77c1e3ccSAndroid Build Coastguard Worker       }
224*77c1e3ccSAndroid Build Coastguard Worker       // Get info of this MI
225*77c1e3ccSAndroid Build Coastguard Worker       if (aom_codec_control(&ctx->decoder, AV1D_GET_MI_INFO, mi_row, mi_col,
226*77c1e3ccSAndroid Build Coastguard Worker                             &cur_mi_info) != AOM_CODEC_OK) {
227*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
228*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to read mi info.");
229*77c1e3ccSAndroid Build Coastguard Worker       }
230*77c1e3ccSAndroid Build Coastguard Worker       const int blk_mi_rows = mi_size_high[cur_mi_info.bsize];
231*77c1e3ccSAndroid Build Coastguard Worker       const int blk_mi_cols = mi_size_wide[cur_mi_info.bsize];
232*77c1e3ccSAndroid Build Coastguard Worker 
233*77c1e3ccSAndroid Build Coastguard Worker       for (int h = 0; h < blk_mi_rows; h++) {
234*77c1e3ccSAndroid Build Coastguard Worker         for (int w = 0; w < blk_mi_cols; w++) {
235*77c1e3ccSAndroid Build Coastguard Worker           if (h + mi_row >= mi_rows || w + mi_col >= mi_cols) {
236*77c1e3ccSAndroid Build Coastguard Worker             continue;
237*77c1e3ccSAndroid Build Coastguard Worker           }
238*77c1e3ccSAndroid Build Coastguard Worker           const int this_offset = offset + h * mi_cols + w;
239*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].bsize = cur_mi_info.bsize;
240*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].partition = cur_mi_info.partition;
241*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].mi_row_start = mi_row;
242*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].mi_col_start = mi_col;
243*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].mv[0] = cur_mi_info.mv[0];
244*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].mv[1] = cur_mi_info.mv[1];
245*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].ref_frame[0] = cur_mi_info.ref_frame[0];
246*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].ref_frame[1] = cur_mi_info.ref_frame[1];
247*77c1e3ccSAndroid Build Coastguard Worker           this_mi[this_offset].pred_mode = cur_mi_info.mode;
248*77c1e3ccSAndroid Build Coastguard Worker         }
249*77c1e3ccSAndroid Build Coastguard Worker       }
250*77c1e3ccSAndroid Build Coastguard Worker     }
251*77c1e3ccSAndroid Build Coastguard Worker   }
252*77c1e3ccSAndroid Build Coastguard Worker 
253*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info_count++;
254*77c1e3ccSAndroid Build Coastguard Worker 
255*77c1e3ccSAndroid Build Coastguard Worker   return 0;
256*77c1e3ccSAndroid Build Coastguard Worker }
257*77c1e3ccSAndroid Build Coastguard Worker 
258*77c1e3ccSAndroid Build Coastguard Worker #define USE_SECOND_PASS_FILE 1
259*77c1e3ccSAndroid Build Coastguard Worker 
260*77c1e3ccSAndroid Build Coastguard Worker #if !USE_SECOND_PASS_FILE
261*77c1e3ccSAndroid Build Coastguard Worker // Parse the frames in the gop and determine the last frame of the current GOP.
262*77c1e3ccSAndroid Build Coastguard Worker // Decode more frames if necessary. The variable max_num is the maximum static
263*77c1e3ccSAndroid Build Coastguard Worker // GOP length if we detect an IPPP structure, and it is expected that max_mum >=
264*77c1e3ccSAndroid Build Coastguard Worker // MAX_GF_INTERVAL.
get_current_gop_end(THIRD_PASS_DEC_CTX * ctx,int max_num,int * last_idx)265*77c1e3ccSAndroid Build Coastguard Worker static void get_current_gop_end(THIRD_PASS_DEC_CTX *ctx, int max_num,
266*77c1e3ccSAndroid Build Coastguard Worker                                 int *last_idx) {
267*77c1e3ccSAndroid Build Coastguard Worker   assert(max_num >= MAX_GF_INTERVAL);
268*77c1e3ccSAndroid Build Coastguard Worker   *last_idx = 0;
269*77c1e3ccSAndroid Build Coastguard Worker   int cur_idx = 0;
270*77c1e3ccSAndroid Build Coastguard Worker   int arf_order_hint = -1;
271*77c1e3ccSAndroid Build Coastguard Worker   int num_show_frames = 0;
272*77c1e3ccSAndroid Build Coastguard Worker   while (num_show_frames < max_num) {
273*77c1e3ccSAndroid Build Coastguard Worker     assert(cur_idx < MAX_THIRD_PASS_BUF);
274*77c1e3ccSAndroid Build Coastguard Worker     // Read in from bitstream if needed.
275*77c1e3ccSAndroid Build Coastguard Worker     if (cur_idx >= ctx->frame_info_count) {
276*77c1e3ccSAndroid Build Coastguard Worker       int ret = get_frame_info(ctx);
277*77c1e3ccSAndroid Build Coastguard Worker       if (ret == 1) {
278*77c1e3ccSAndroid Build Coastguard Worker         // At the end of the file, GOP ends in the prev frame.
279*77c1e3ccSAndroid Build Coastguard Worker         if (arf_order_hint >= 0) {
280*77c1e3ccSAndroid Build Coastguard Worker           aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
281*77c1e3ccSAndroid Build Coastguard Worker                              "Failed to derive GOP length.");
282*77c1e3ccSAndroid Build Coastguard Worker         }
283*77c1e3ccSAndroid Build Coastguard Worker         *last_idx = cur_idx - 1;
284*77c1e3ccSAndroid Build Coastguard Worker         return;
285*77c1e3ccSAndroid Build Coastguard Worker       }
286*77c1e3ccSAndroid Build Coastguard Worker       if (ret < 0) {
287*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
288*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to read frame for third pass.");
289*77c1e3ccSAndroid Build Coastguard Worker       }
290*77c1e3ccSAndroid Build Coastguard Worker     }
291*77c1e3ccSAndroid Build Coastguard Worker 
292*77c1e3ccSAndroid Build Coastguard Worker     // TODO(bohanli): verify that fwd_kf works here.
293*77c1e3ccSAndroid Build Coastguard Worker     if (ctx->frame_info[cur_idx].frame_type == KEY_FRAME &&
294*77c1e3ccSAndroid Build Coastguard Worker         ctx->frame_info[cur_idx].is_show_frame) {
295*77c1e3ccSAndroid Build Coastguard Worker       if (cur_idx != 0) {
296*77c1e3ccSAndroid Build Coastguard Worker         // If this is a key frame and is not the first kf in this kf group, we
297*77c1e3ccSAndroid Build Coastguard Worker         // have reached the next key frame. Stop here.
298*77c1e3ccSAndroid Build Coastguard Worker         *last_idx = cur_idx - 1;
299*77c1e3ccSAndroid Build Coastguard Worker         return;
300*77c1e3ccSAndroid Build Coastguard Worker       }
301*77c1e3ccSAndroid Build Coastguard Worker     } else if (!ctx->frame_info[cur_idx].is_show_frame &&
302*77c1e3ccSAndroid Build Coastguard Worker                arf_order_hint == -1) {
303*77c1e3ccSAndroid Build Coastguard Worker       // If this is an arf (the first no show)
304*77c1e3ccSAndroid Build Coastguard Worker       if (num_show_frames <= 1) {
305*77c1e3ccSAndroid Build Coastguard Worker         // This is an arf and we should end the GOP with its overlay.
306*77c1e3ccSAndroid Build Coastguard Worker         arf_order_hint = ctx->frame_info[cur_idx].order_hint;
307*77c1e3ccSAndroid Build Coastguard Worker       } else {
308*77c1e3ccSAndroid Build Coastguard Worker         // There are multiple show frames before the this arf, so we treat the
309*77c1e3ccSAndroid Build Coastguard Worker         // frames previous to this arf as a GOP.
310*77c1e3ccSAndroid Build Coastguard Worker         *last_idx = cur_idx - 1;
311*77c1e3ccSAndroid Build Coastguard Worker         return;
312*77c1e3ccSAndroid Build Coastguard Worker       }
313*77c1e3ccSAndroid Build Coastguard Worker     } else if (arf_order_hint >= 0 && ctx->frame_info[cur_idx].order_hint ==
314*77c1e3ccSAndroid Build Coastguard Worker                                           (unsigned int)arf_order_hint) {
315*77c1e3ccSAndroid Build Coastguard Worker       // If this is the overlay/show existing of the arf
316*77c1e3ccSAndroid Build Coastguard Worker       assert(ctx->frame_info[cur_idx].is_show_frame);
317*77c1e3ccSAndroid Build Coastguard Worker       *last_idx = cur_idx;
318*77c1e3ccSAndroid Build Coastguard Worker       return;
319*77c1e3ccSAndroid Build Coastguard Worker     } else {
320*77c1e3ccSAndroid Build Coastguard Worker       // This frame is part of the GOP.
321*77c1e3ccSAndroid Build Coastguard Worker       if (ctx->frame_info[cur_idx].is_show_frame) num_show_frames++;
322*77c1e3ccSAndroid Build Coastguard Worker     }
323*77c1e3ccSAndroid Build Coastguard Worker     cur_idx++;
324*77c1e3ccSAndroid Build Coastguard Worker   }
325*77c1e3ccSAndroid Build Coastguard Worker   // This is a long IPPP GOP and we will use a length of max_num here.
326*77c1e3ccSAndroid Build Coastguard Worker   assert(arf_order_hint < 0);
327*77c1e3ccSAndroid Build Coastguard Worker   *last_idx = max_num - 1;
328*77c1e3ccSAndroid Build Coastguard Worker   return;
329*77c1e3ccSAndroid Build Coastguard Worker }
330*77c1e3ccSAndroid Build Coastguard Worker #endif
331*77c1e3ccSAndroid Build Coastguard Worker 
read_gop_frames(THIRD_PASS_DEC_CTX * ctx)332*77c1e3ccSAndroid Build Coastguard Worker static inline void read_gop_frames(THIRD_PASS_DEC_CTX *ctx) {
333*77c1e3ccSAndroid Build Coastguard Worker   int cur_idx = 0;
334*77c1e3ccSAndroid Build Coastguard Worker   while (cur_idx < ctx->gop_info.num_frames) {
335*77c1e3ccSAndroid Build Coastguard Worker     assert(cur_idx < MAX_THIRD_PASS_BUF);
336*77c1e3ccSAndroid Build Coastguard Worker     // Read in from bitstream if needed.
337*77c1e3ccSAndroid Build Coastguard Worker     if (cur_idx >= ctx->frame_info_count) {
338*77c1e3ccSAndroid Build Coastguard Worker       int ret = get_frame_info(ctx);
339*77c1e3ccSAndroid Build Coastguard Worker       if (ret != 0) {
340*77c1e3ccSAndroid Build Coastguard Worker         aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
341*77c1e3ccSAndroid Build Coastguard Worker                            "Failed to read frame for third pass.");
342*77c1e3ccSAndroid Build Coastguard Worker       }
343*77c1e3ccSAndroid Build Coastguard Worker     }
344*77c1e3ccSAndroid Build Coastguard Worker     cur_idx++;
345*77c1e3ccSAndroid Build Coastguard Worker   }
346*77c1e3ccSAndroid Build Coastguard Worker   return;
347*77c1e3ccSAndroid Build Coastguard Worker }
348*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_gop_third_pass(THIRD_PASS_DEC_CTX * ctx)349*77c1e3ccSAndroid Build Coastguard Worker void av1_set_gop_third_pass(THIRD_PASS_DEC_CTX *ctx) {
350*77c1e3ccSAndroid Build Coastguard Worker   // Read in future frames in the current GOP.
351*77c1e3ccSAndroid Build Coastguard Worker   read_gop_frames(ctx);
352*77c1e3ccSAndroid Build Coastguard Worker 
353*77c1e3ccSAndroid Build Coastguard Worker   int gf_len = 0;
354*77c1e3ccSAndroid Build Coastguard Worker   // Check the GOP length against the value read from second_pass_file
355*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < ctx->gop_info.num_frames; i++) {
356*77c1e3ccSAndroid Build Coastguard Worker     if (ctx->frame_info[i].is_show_frame) gf_len++;
357*77c1e3ccSAndroid Build Coastguard Worker   }
358*77c1e3ccSAndroid Build Coastguard Worker 
359*77c1e3ccSAndroid Build Coastguard Worker   if (gf_len != ctx->gop_info.gf_length) {
360*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
361*77c1e3ccSAndroid Build Coastguard Worker                        "Mismatch in third pass GOP length!");
362*77c1e3ccSAndroid Build Coastguard Worker   }
363*77c1e3ccSAndroid Build Coastguard Worker }
364*77c1e3ccSAndroid Build Coastguard Worker 
av1_pop_third_pass_info(THIRD_PASS_DEC_CTX * ctx)365*77c1e3ccSAndroid Build Coastguard Worker void av1_pop_third_pass_info(THIRD_PASS_DEC_CTX *ctx) {
366*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->frame_info_count == 0) {
367*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
368*77c1e3ccSAndroid Build Coastguard Worker                        "No available frame info for third pass.");
369*77c1e3ccSAndroid Build Coastguard Worker   }
370*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info_count--;
371*77c1e3ccSAndroid Build Coastguard Worker   free_frame_info(&ctx->frame_info[0]);
372*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < ctx->frame_info_count; i++) {
373*77c1e3ccSAndroid Build Coastguard Worker     ctx->frame_info[i] = ctx->frame_info[i + 1];
374*77c1e3ccSAndroid Build Coastguard Worker   }
375*77c1e3ccSAndroid Build Coastguard Worker   ctx->frame_info[ctx->frame_info_count].mi_info = NULL;
376*77c1e3ccSAndroid Build Coastguard Worker }
377*77c1e3ccSAndroid Build Coastguard Worker 
av1_init_thirdpass_ctx(AV1_COMMON * cm,THIRD_PASS_DEC_CTX ** ctx,const char * file)378*77c1e3ccSAndroid Build Coastguard Worker void av1_init_thirdpass_ctx(AV1_COMMON *cm, THIRD_PASS_DEC_CTX **ctx,
379*77c1e3ccSAndroid Build Coastguard Worker                             const char *file) {
380*77c1e3ccSAndroid Build Coastguard Worker   av1_free_thirdpass_ctx(*ctx);
381*77c1e3ccSAndroid Build Coastguard Worker   CHECK_MEM_ERROR(cm, *ctx, aom_calloc(1, sizeof(**ctx)));
382*77c1e3ccSAndroid Build Coastguard Worker   THIRD_PASS_DEC_CTX *ctx_ptr = *ctx;
383*77c1e3ccSAndroid Build Coastguard Worker   ctx_ptr->input_file_name = file;
384*77c1e3ccSAndroid Build Coastguard Worker   ctx_ptr->prev_gop_end = -1;
385*77c1e3ccSAndroid Build Coastguard Worker   ctx_ptr->err_info = cm->error;
386*77c1e3ccSAndroid Build Coastguard Worker }
387*77c1e3ccSAndroid Build Coastguard Worker 
av1_free_thirdpass_ctx(THIRD_PASS_DEC_CTX * ctx)388*77c1e3ccSAndroid Build Coastguard Worker void av1_free_thirdpass_ctx(THIRD_PASS_DEC_CTX *ctx) {
389*77c1e3ccSAndroid Build Coastguard Worker   if (ctx == NULL) return;
390*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->decoder.iface) {
391*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_destroy(&ctx->decoder);
392*77c1e3ccSAndroid Build Coastguard Worker   }
393*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->input_ctx && ctx->input_ctx->file) fclose(ctx->input_ctx->file);
394*77c1e3ccSAndroid Build Coastguard Worker   aom_free(ctx->input_ctx);
395*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->buf) free(ctx->buf);
396*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < MAX_THIRD_PASS_BUF; i++) {
397*77c1e3ccSAndroid Build Coastguard Worker     free_frame_info(&ctx->frame_info[i]);
398*77c1e3ccSAndroid Build Coastguard Worker   }
399*77c1e3ccSAndroid Build Coastguard Worker   aom_free(ctx);
400*77c1e3ccSAndroid Build Coastguard Worker }
401*77c1e3ccSAndroid Build Coastguard Worker 
av1_write_second_pass_gop_info(AV1_COMP * cpi)402*77c1e3ccSAndroid Build Coastguard Worker void av1_write_second_pass_gop_info(AV1_COMP *cpi) {
403*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
404*77c1e3ccSAndroid Build Coastguard Worker   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
405*77c1e3ccSAndroid Build Coastguard Worker   const PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
406*77c1e3ccSAndroid Build Coastguard Worker 
407*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->pass == AOM_RC_SECOND_PASS && oxcf->second_pass_log) {
408*77c1e3ccSAndroid Build Coastguard Worker     // Write the GOP length to a log file.
409*77c1e3ccSAndroid Build Coastguard Worker     av1_open_second_pass_log(cpi, 0);
410*77c1e3ccSAndroid Build Coastguard Worker 
411*77c1e3ccSAndroid Build Coastguard Worker     THIRD_PASS_GOP_INFO gop_info;
412*77c1e3ccSAndroid Build Coastguard Worker 
413*77c1e3ccSAndroid Build Coastguard Worker     gop_info.num_frames = gf_group->size;
414*77c1e3ccSAndroid Build Coastguard Worker     gop_info.use_arf = (gf_group->arf_index >= 0);
415*77c1e3ccSAndroid Build Coastguard Worker     gop_info.gf_length = p_rc->baseline_gf_interval;
416*77c1e3ccSAndroid Build Coastguard Worker 
417*77c1e3ccSAndroid Build Coastguard Worker     size_t count =
418*77c1e3ccSAndroid Build Coastguard Worker         fwrite(&gop_info, sizeof(gop_info), 1, cpi->second_pass_log_stream);
419*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
420*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
421*77c1e3ccSAndroid Build Coastguard Worker                          "Could not write to second pass log file!");
422*77c1e3ccSAndroid Build Coastguard Worker     }
423*77c1e3ccSAndroid Build Coastguard Worker   }
424*77c1e3ccSAndroid Build Coastguard Worker }
425*77c1e3ccSAndroid Build Coastguard Worker 
av1_write_second_pass_per_frame_info(AV1_COMP * cpi,int gf_index)426*77c1e3ccSAndroid Build Coastguard Worker void av1_write_second_pass_per_frame_info(AV1_COMP *cpi, int gf_index) {
427*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
428*77c1e3ccSAndroid Build Coastguard Worker   const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
429*77c1e3ccSAndroid Build Coastguard Worker 
430*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->pass == AOM_RC_SECOND_PASS && oxcf->second_pass_log) {
431*77c1e3ccSAndroid Build Coastguard Worker     // write target bitrate
432*77c1e3ccSAndroid Build Coastguard Worker     int bits = gf_group->bit_allocation[gf_index];
433*77c1e3ccSAndroid Build Coastguard Worker     size_t count = fwrite(&bits, sizeof(bits), 1, cpi->second_pass_log_stream);
434*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
435*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
436*77c1e3ccSAndroid Build Coastguard Worker                          "Could not write to second pass log file!");
437*77c1e3ccSAndroid Build Coastguard Worker     }
438*77c1e3ccSAndroid Build Coastguard Worker 
439*77c1e3ccSAndroid Build Coastguard Worker     // write sse
440*77c1e3ccSAndroid Build Coastguard Worker     uint64_t sse = 0;
441*77c1e3ccSAndroid Build Coastguard Worker     int pkt_idx = cpi->ppi->output_pkt_list->cnt - 1;
442*77c1e3ccSAndroid Build Coastguard Worker     if (pkt_idx >= 0 &&
443*77c1e3ccSAndroid Build Coastguard Worker         cpi->ppi->output_pkt_list->pkts[pkt_idx].kind == AOM_CODEC_PSNR_PKT) {
444*77c1e3ccSAndroid Build Coastguard Worker       sse = cpi->ppi->output_pkt_list->pkts[pkt_idx].data.psnr.sse[0];
445*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
446*77c1e3ccSAndroid Build Coastguard Worker     } else if (cpi->ppi->b_calculate_psnr) {
447*77c1e3ccSAndroid Build Coastguard Worker       sse = cpi->ppi->total_sq_error[0];
448*77c1e3ccSAndroid Build Coastguard Worker #endif
449*77c1e3ccSAndroid Build Coastguard Worker     } else {
450*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *orig = cpi->source;
451*77c1e3ccSAndroid Build Coastguard Worker       const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
452*77c1e3ccSAndroid Build Coastguard Worker       PSNR_STATS psnr;
453*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
454*77c1e3ccSAndroid Build Coastguard Worker       const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth;
455*77c1e3ccSAndroid Build Coastguard Worker       const uint32_t bit_depth = cpi->td.mb.e_mbd.bd;
456*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
457*77c1e3ccSAndroid Build Coastguard Worker #else
458*77c1e3ccSAndroid Build Coastguard Worker       aom_calc_psnr(orig, recon, &psnr);
459*77c1e3ccSAndroid Build Coastguard Worker #endif
460*77c1e3ccSAndroid Build Coastguard Worker       sse = psnr.sse[0];
461*77c1e3ccSAndroid Build Coastguard Worker     }
462*77c1e3ccSAndroid Build Coastguard Worker 
463*77c1e3ccSAndroid Build Coastguard Worker     count = fwrite(&sse, sizeof(sse), 1, cpi->second_pass_log_stream);
464*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
465*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
466*77c1e3ccSAndroid Build Coastguard Worker                          "Could not write to second pass log file!");
467*77c1e3ccSAndroid Build Coastguard Worker     }
468*77c1e3ccSAndroid Build Coastguard Worker 
469*77c1e3ccSAndroid Build Coastguard Worker     // write bpm_factor
470*77c1e3ccSAndroid Build Coastguard Worker     double factor = cpi->ppi->twopass.bpm_factor;
471*77c1e3ccSAndroid Build Coastguard Worker     count = fwrite(&factor, sizeof(factor), 1, cpi->second_pass_log_stream);
472*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
473*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
474*77c1e3ccSAndroid Build Coastguard Worker                          "Could not write to second pass log file!");
475*77c1e3ccSAndroid Build Coastguard Worker     }
476*77c1e3ccSAndroid Build Coastguard Worker   }
477*77c1e3ccSAndroid Build Coastguard Worker }
av1_open_second_pass_log(AV1_COMP * cpi,int is_read)478*77c1e3ccSAndroid Build Coastguard Worker void av1_open_second_pass_log(AV1_COMP *cpi, int is_read) {
479*77c1e3ccSAndroid Build Coastguard Worker   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
480*77c1e3ccSAndroid Build Coastguard Worker   if (oxcf->second_pass_log == NULL) {
481*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(cpi->common.error, AOM_CODEC_INVALID_PARAM,
482*77c1e3ccSAndroid Build Coastguard Worker                        "No second pass log file specified for the third pass!");
483*77c1e3ccSAndroid Build Coastguard Worker   }
484*77c1e3ccSAndroid Build Coastguard Worker   // Read the GOP length from a file.
485*77c1e3ccSAndroid Build Coastguard Worker   if (!cpi->second_pass_log_stream) {
486*77c1e3ccSAndroid Build Coastguard Worker     if (is_read) {
487*77c1e3ccSAndroid Build Coastguard Worker       cpi->second_pass_log_stream = fopen(cpi->oxcf.second_pass_log, "rb");
488*77c1e3ccSAndroid Build Coastguard Worker     } else {
489*77c1e3ccSAndroid Build Coastguard Worker       cpi->second_pass_log_stream = fopen(cpi->oxcf.second_pass_log, "wb");
490*77c1e3ccSAndroid Build Coastguard Worker     }
491*77c1e3ccSAndroid Build Coastguard Worker     if (!cpi->second_pass_log_stream) {
492*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
493*77c1e3ccSAndroid Build Coastguard Worker                          "Could not open second pass log file!");
494*77c1e3ccSAndroid Build Coastguard Worker     }
495*77c1e3ccSAndroid Build Coastguard Worker   }
496*77c1e3ccSAndroid Build Coastguard Worker }
497*77c1e3ccSAndroid Build Coastguard Worker 
av1_close_second_pass_log(AV1_COMP * cpi)498*77c1e3ccSAndroid Build Coastguard Worker void av1_close_second_pass_log(AV1_COMP *cpi) {
499*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->second_pass_log_stream) {
500*77c1e3ccSAndroid Build Coastguard Worker     int ret = fclose(cpi->second_pass_log_stream);
501*77c1e3ccSAndroid Build Coastguard Worker     if (ret != 0) {
502*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(cpi->common.error, AOM_CODEC_ERROR,
503*77c1e3ccSAndroid Build Coastguard Worker                          "Could not close second pass log file!");
504*77c1e3ccSAndroid Build Coastguard Worker     }
505*77c1e3ccSAndroid Build Coastguard Worker     cpi->second_pass_log_stream = 0;
506*77c1e3ccSAndroid Build Coastguard Worker   }
507*77c1e3ccSAndroid Build Coastguard Worker }
508*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_second_pass_gop_info(FILE * second_pass_log_stream,THIRD_PASS_GOP_INFO * gop_info,struct aom_internal_error_info * error)509*77c1e3ccSAndroid Build Coastguard Worker void av1_read_second_pass_gop_info(FILE *second_pass_log_stream,
510*77c1e3ccSAndroid Build Coastguard Worker                                    THIRD_PASS_GOP_INFO *gop_info,
511*77c1e3ccSAndroid Build Coastguard Worker                                    struct aom_internal_error_info *error) {
512*77c1e3ccSAndroid Build Coastguard Worker   size_t count = fread(gop_info, sizeof(*gop_info), 1, second_pass_log_stream);
513*77c1e3ccSAndroid Build Coastguard Worker   if (count < 1) {
514*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(error, AOM_CODEC_ERROR,
515*77c1e3ccSAndroid Build Coastguard Worker                        "Could not read from second pass log file!");
516*77c1e3ccSAndroid Build Coastguard Worker   }
517*77c1e3ccSAndroid Build Coastguard Worker }
518*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_second_pass_per_frame_info(FILE * second_pass_log_stream,THIRD_PASS_FRAME_INFO * frame_info_arr,int frame_info_count,struct aom_internal_error_info * error)519*77c1e3ccSAndroid Build Coastguard Worker void av1_read_second_pass_per_frame_info(
520*77c1e3ccSAndroid Build Coastguard Worker     FILE *second_pass_log_stream, THIRD_PASS_FRAME_INFO *frame_info_arr,
521*77c1e3ccSAndroid Build Coastguard Worker     int frame_info_count, struct aom_internal_error_info *error) {
522*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < frame_info_count; i++) {
523*77c1e3ccSAndroid Build Coastguard Worker     // read target bits
524*77c1e3ccSAndroid Build Coastguard Worker     int bits = 0;
525*77c1e3ccSAndroid Build Coastguard Worker     size_t count = fread(&bits, sizeof(bits), 1, second_pass_log_stream);
526*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
527*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(error, AOM_CODEC_ERROR,
528*77c1e3ccSAndroid Build Coastguard Worker                          "Could not read from second pass log file!");
529*77c1e3ccSAndroid Build Coastguard Worker     }
530*77c1e3ccSAndroid Build Coastguard Worker     frame_info_arr[i].bits_allocated = bits;
531*77c1e3ccSAndroid Build Coastguard Worker 
532*77c1e3ccSAndroid Build Coastguard Worker     // read distortion
533*77c1e3ccSAndroid Build Coastguard Worker     uint64_t sse;
534*77c1e3ccSAndroid Build Coastguard Worker     count = fread(&sse, sizeof(sse), 1, second_pass_log_stream);
535*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
536*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(error, AOM_CODEC_ERROR,
537*77c1e3ccSAndroid Build Coastguard Worker                          "Could not read from second pass log file!");
538*77c1e3ccSAndroid Build Coastguard Worker     }
539*77c1e3ccSAndroid Build Coastguard Worker     frame_info_arr[i].sse = sse;
540*77c1e3ccSAndroid Build Coastguard Worker 
541*77c1e3ccSAndroid Build Coastguard Worker     // read bpm factor
542*77c1e3ccSAndroid Build Coastguard Worker     double factor;
543*77c1e3ccSAndroid Build Coastguard Worker     count = fread(&factor, sizeof(factor), 1, second_pass_log_stream);
544*77c1e3ccSAndroid Build Coastguard Worker     if (count < 1) {
545*77c1e3ccSAndroid Build Coastguard Worker       aom_internal_error(error, AOM_CODEC_ERROR,
546*77c1e3ccSAndroid Build Coastguard Worker                          "Could not read from second pass log file!");
547*77c1e3ccSAndroid Build Coastguard Worker     }
548*77c1e3ccSAndroid Build Coastguard Worker     frame_info_arr[i].bpm_factor = factor;
549*77c1e3ccSAndroid Build Coastguard Worker   }
550*77c1e3ccSAndroid Build Coastguard Worker }
551*77c1e3ccSAndroid Build Coastguard Worker 
av1_check_use_arf(THIRD_PASS_DEC_CTX * ctx)552*77c1e3ccSAndroid Build Coastguard Worker int av1_check_use_arf(THIRD_PASS_DEC_CTX *ctx) {
553*77c1e3ccSAndroid Build Coastguard Worker   if (ctx == NULL) return -1;
554*77c1e3ccSAndroid Build Coastguard Worker   int use_arf = 0;
555*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < ctx->gop_info.gf_length; i++) {
556*77c1e3ccSAndroid Build Coastguard Worker     if (ctx->frame_info[i].order_hint != 0 &&
557*77c1e3ccSAndroid Build Coastguard Worker         ctx->frame_info[i].is_show_frame == 0) {
558*77c1e3ccSAndroid Build Coastguard Worker       use_arf = 1;
559*77c1e3ccSAndroid Build Coastguard Worker     }
560*77c1e3ccSAndroid Build Coastguard Worker   }
561*77c1e3ccSAndroid Build Coastguard Worker   if (use_arf != ctx->gop_info.use_arf) {
562*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(ctx->err_info, AOM_CODEC_ERROR,
563*77c1e3ccSAndroid Build Coastguard Worker                        "Mismatch in third pass GOP length!");
564*77c1e3ccSAndroid Build Coastguard Worker   }
565*77c1e3ccSAndroid Build Coastguard Worker   return use_arf;
566*77c1e3ccSAndroid Build Coastguard Worker }
567*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_ratio(THIRD_PASS_DEC_CTX * ctx,int fidx,int fheight,int fwidth,double * ratio_h,double * ratio_w)568*77c1e3ccSAndroid Build Coastguard Worker void av1_get_third_pass_ratio(THIRD_PASS_DEC_CTX *ctx, int fidx, int fheight,
569*77c1e3ccSAndroid Build Coastguard Worker                               int fwidth, double *ratio_h, double *ratio_w) {
570*77c1e3ccSAndroid Build Coastguard Worker   assert(ctx);
571*77c1e3ccSAndroid Build Coastguard Worker   assert(fidx < ctx->frame_info_count);
572*77c1e3ccSAndroid Build Coastguard Worker   const int fheight_second_pass = ctx->frame_info[fidx].height;
573*77c1e3ccSAndroid Build Coastguard Worker   const int fwidth_second_pass = ctx->frame_info[fidx].width;
574*77c1e3ccSAndroid Build Coastguard Worker   assert(fheight_second_pass <= fheight && fwidth_second_pass <= fwidth);
575*77c1e3ccSAndroid Build Coastguard Worker 
576*77c1e3ccSAndroid Build Coastguard Worker   *ratio_h = (double)fheight / fheight_second_pass;
577*77c1e3ccSAndroid Build Coastguard Worker   *ratio_w = (double)fwidth / fwidth_second_pass;
578*77c1e3ccSAndroid Build Coastguard Worker }
579*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_mi(THIRD_PASS_DEC_CTX * ctx,int fidx,int mi_row,int mi_col,double ratio_h,double ratio_w)580*77c1e3ccSAndroid Build Coastguard Worker THIRD_PASS_MI_INFO *av1_get_third_pass_mi(THIRD_PASS_DEC_CTX *ctx, int fidx,
581*77c1e3ccSAndroid Build Coastguard Worker                                           int mi_row, int mi_col,
582*77c1e3ccSAndroid Build Coastguard Worker                                           double ratio_h, double ratio_w) {
583*77c1e3ccSAndroid Build Coastguard Worker   assert(ctx);
584*77c1e3ccSAndroid Build Coastguard Worker   assert(fidx < ctx->frame_info_count);
585*77c1e3ccSAndroid Build Coastguard Worker 
586*77c1e3ccSAndroid Build Coastguard Worker   const int mi_rows_second_pass = ctx->frame_info[fidx].mi_rows;
587*77c1e3ccSAndroid Build Coastguard Worker   const int mi_cols_second_pass = ctx->frame_info[fidx].mi_cols;
588*77c1e3ccSAndroid Build Coastguard Worker 
589*77c1e3ccSAndroid Build Coastguard Worker   const int mi_row_second_pass =
590*77c1e3ccSAndroid Build Coastguard Worker       clamp((int)round(mi_row / ratio_h), 0, mi_rows_second_pass - 1);
591*77c1e3ccSAndroid Build Coastguard Worker   const int mi_col_second_pass =
592*77c1e3ccSAndroid Build Coastguard Worker       clamp((int)round(mi_col / ratio_w), 0, mi_cols_second_pass - 1);
593*77c1e3ccSAndroid Build Coastguard Worker 
594*77c1e3ccSAndroid Build Coastguard Worker   const int mi_stride_second_pass = ctx->frame_info[fidx].mi_stride;
595*77c1e3ccSAndroid Build Coastguard Worker   THIRD_PASS_MI_INFO *this_mi = ctx->frame_info[fidx].mi_info +
596*77c1e3ccSAndroid Build Coastguard Worker                                 mi_row_second_pass * mi_stride_second_pass +
597*77c1e3ccSAndroid Build Coastguard Worker                                 mi_col_second_pass;
598*77c1e3ccSAndroid Build Coastguard Worker   return this_mi;
599*77c1e3ccSAndroid Build Coastguard Worker }
600*77c1e3ccSAndroid Build Coastguard Worker 
av1_third_pass_get_adjusted_mi(THIRD_PASS_MI_INFO * third_pass_mi,double ratio_h,double ratio_w,int * mi_row,int * mi_col)601*77c1e3ccSAndroid Build Coastguard Worker void av1_third_pass_get_adjusted_mi(THIRD_PASS_MI_INFO *third_pass_mi,
602*77c1e3ccSAndroid Build Coastguard Worker                                     double ratio_h, double ratio_w, int *mi_row,
603*77c1e3ccSAndroid Build Coastguard Worker                                     int *mi_col) {
604*77c1e3ccSAndroid Build Coastguard Worker   *mi_row = (int)round(third_pass_mi->mi_row_start * ratio_h);
605*77c1e3ccSAndroid Build Coastguard Worker   *mi_col = (int)round(third_pass_mi->mi_col_start * ratio_w);
606*77c1e3ccSAndroid Build Coastguard Worker }
607*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_adjusted_mv(THIRD_PASS_MI_INFO * this_mi,double ratio_h,double ratio_w,MV_REFERENCE_FRAME frame)608*77c1e3ccSAndroid Build Coastguard Worker int_mv av1_get_third_pass_adjusted_mv(THIRD_PASS_MI_INFO *this_mi,
609*77c1e3ccSAndroid Build Coastguard Worker                                       double ratio_h, double ratio_w,
610*77c1e3ccSAndroid Build Coastguard Worker                                       MV_REFERENCE_FRAME frame) {
611*77c1e3ccSAndroid Build Coastguard Worker   assert(this_mi != NULL);
612*77c1e3ccSAndroid Build Coastguard Worker   int_mv cur_mv;
613*77c1e3ccSAndroid Build Coastguard Worker   cur_mv.as_int = INVALID_MV;
614*77c1e3ccSAndroid Build Coastguard Worker 
615*77c1e3ccSAndroid Build Coastguard Worker   if (frame < LAST_FRAME || frame > ALTREF_FRAME) return cur_mv;
616*77c1e3ccSAndroid Build Coastguard Worker 
617*77c1e3ccSAndroid Build Coastguard Worker   for (int r = 0; r < 2; r++) {
618*77c1e3ccSAndroid Build Coastguard Worker     if (this_mi->ref_frame[r] == frame) {
619*77c1e3ccSAndroid Build Coastguard Worker       cur_mv.as_mv.row = (int16_t)round(this_mi->mv[r].as_mv.row * ratio_h);
620*77c1e3ccSAndroid Build Coastguard Worker       cur_mv.as_mv.col = (int16_t)round(this_mi->mv[r].as_mv.col * ratio_w);
621*77c1e3ccSAndroid Build Coastguard Worker     }
622*77c1e3ccSAndroid Build Coastguard Worker   }
623*77c1e3ccSAndroid Build Coastguard Worker 
624*77c1e3ccSAndroid Build Coastguard Worker   return cur_mv;
625*77c1e3ccSAndroid Build Coastguard Worker }
626*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_adjusted_blk_size(THIRD_PASS_MI_INFO * this_mi,double ratio_h,double ratio_w)627*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE av1_get_third_pass_adjusted_blk_size(THIRD_PASS_MI_INFO *this_mi,
628*77c1e3ccSAndroid Build Coastguard Worker                                                 double ratio_h,
629*77c1e3ccSAndroid Build Coastguard Worker                                                 double ratio_w) {
630*77c1e3ccSAndroid Build Coastguard Worker   assert(this_mi != NULL);
631*77c1e3ccSAndroid Build Coastguard Worker   BLOCK_SIZE bsize = BLOCK_INVALID;
632*77c1e3ccSAndroid Build Coastguard Worker 
633*77c1e3ccSAndroid Build Coastguard Worker   const BLOCK_SIZE bsize_second_pass = this_mi->bsize;
634*77c1e3ccSAndroid Build Coastguard Worker   assert(bsize_second_pass != BLOCK_INVALID);
635*77c1e3ccSAndroid Build Coastguard Worker 
636*77c1e3ccSAndroid Build Coastguard Worker   const int w_second_pass = block_size_wide[bsize_second_pass];
637*77c1e3ccSAndroid Build Coastguard Worker   const int h_second_pass = block_size_high[bsize_second_pass];
638*77c1e3ccSAndroid Build Coastguard Worker 
639*77c1e3ccSAndroid Build Coastguard Worker   int part_type;
640*77c1e3ccSAndroid Build Coastguard Worker 
641*77c1e3ccSAndroid Build Coastguard Worker   if (w_second_pass == h_second_pass) {
642*77c1e3ccSAndroid Build Coastguard Worker     part_type = PARTITION_NONE;
643*77c1e3ccSAndroid Build Coastguard Worker   } else if (w_second_pass / h_second_pass == 2) {
644*77c1e3ccSAndroid Build Coastguard Worker     part_type = PARTITION_HORZ;
645*77c1e3ccSAndroid Build Coastguard Worker   } else if (w_second_pass / h_second_pass == 4) {
646*77c1e3ccSAndroid Build Coastguard Worker     part_type = PARTITION_HORZ_4;
647*77c1e3ccSAndroid Build Coastguard Worker   } else if (h_second_pass / w_second_pass == 2) {
648*77c1e3ccSAndroid Build Coastguard Worker     part_type = PARTITION_VERT;
649*77c1e3ccSAndroid Build Coastguard Worker   } else if (h_second_pass / w_second_pass == 4) {
650*77c1e3ccSAndroid Build Coastguard Worker     part_type = PARTITION_VERT_4;
651*77c1e3ccSAndroid Build Coastguard Worker   } else {
652*77c1e3ccSAndroid Build Coastguard Worker     part_type = PARTITION_INVALID;
653*77c1e3ccSAndroid Build Coastguard Worker   }
654*77c1e3ccSAndroid Build Coastguard Worker   assert(part_type != PARTITION_INVALID);
655*77c1e3ccSAndroid Build Coastguard Worker 
656*77c1e3ccSAndroid Build Coastguard Worker   const int w = (int)(round(w_second_pass * ratio_w));
657*77c1e3ccSAndroid Build Coastguard Worker   const int h = (int)(round(h_second_pass * ratio_h));
658*77c1e3ccSAndroid Build Coastguard Worker 
659*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < SQR_BLOCK_SIZES; i++) {
660*77c1e3ccSAndroid Build Coastguard Worker     const BLOCK_SIZE this_bsize = subsize_lookup[part_type][i];
661*77c1e3ccSAndroid Build Coastguard Worker     if (this_bsize == BLOCK_INVALID) continue;
662*77c1e3ccSAndroid Build Coastguard Worker 
663*77c1e3ccSAndroid Build Coastguard Worker     const int this_w = block_size_wide[this_bsize];
664*77c1e3ccSAndroid Build Coastguard Worker     const int this_h = block_size_high[this_bsize];
665*77c1e3ccSAndroid Build Coastguard Worker 
666*77c1e3ccSAndroid Build Coastguard Worker     if (this_w >= w && this_h >= h) {
667*77c1e3ccSAndroid Build Coastguard Worker       // find the smallest block size that contains the mapped block
668*77c1e3ccSAndroid Build Coastguard Worker       bsize = this_bsize;
669*77c1e3ccSAndroid Build Coastguard Worker       break;
670*77c1e3ccSAndroid Build Coastguard Worker     }
671*77c1e3ccSAndroid Build Coastguard Worker   }
672*77c1e3ccSAndroid Build Coastguard Worker   if (bsize == BLOCK_INVALID) {
673*77c1e3ccSAndroid Build Coastguard Worker     // could not find a proper one, just use the largest then.
674*77c1e3ccSAndroid Build Coastguard Worker     bsize = BLOCK_128X128;
675*77c1e3ccSAndroid Build Coastguard Worker   }
676*77c1e3ccSAndroid Build Coastguard Worker 
677*77c1e3ccSAndroid Build Coastguard Worker   return bsize;
678*77c1e3ccSAndroid Build Coastguard Worker }
679*77c1e3ccSAndroid Build Coastguard Worker 
av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX * ctx,THIRD_PASS_MI_INFO * this_mi)680*77c1e3ccSAndroid Build Coastguard Worker PARTITION_TYPE av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX *ctx,
681*77c1e3ccSAndroid Build Coastguard Worker                                                THIRD_PASS_MI_INFO *this_mi) {
682*77c1e3ccSAndroid Build Coastguard Worker   int mi_stride = ctx->frame_info[0].mi_stride;
683*77c1e3ccSAndroid Build Coastguard Worker 
684*77c1e3ccSAndroid Build Coastguard Worker   int mi_row = this_mi->mi_row_start;
685*77c1e3ccSAndroid Build Coastguard Worker   int mi_col = this_mi->mi_col_start;
686*77c1e3ccSAndroid Build Coastguard Worker 
687*77c1e3ccSAndroid Build Coastguard Worker   THIRD_PASS_MI_INFO *corner_mi =
688*77c1e3ccSAndroid Build Coastguard Worker       &ctx->frame_info[0].mi_info[mi_row * mi_stride + mi_col];
689*77c1e3ccSAndroid Build Coastguard Worker 
690*77c1e3ccSAndroid Build Coastguard Worker   return corner_mi->partition;
691*77c1e3ccSAndroid Build Coastguard Worker }
692*77c1e3ccSAndroid Build Coastguard Worker 
693*77c1e3ccSAndroid Build Coastguard Worker #else   // !(CONFIG_THREE_PASS && CONFIG_AV1_DECODER)
av1_init_thirdpass_ctx(AV1_COMMON * cm,THIRD_PASS_DEC_CTX ** ctx,const char * file)694*77c1e3ccSAndroid Build Coastguard Worker void av1_init_thirdpass_ctx(AV1_COMMON *cm, THIRD_PASS_DEC_CTX **ctx,
695*77c1e3ccSAndroid Build Coastguard Worker                             const char *file) {
696*77c1e3ccSAndroid Build Coastguard Worker   (void)ctx;
697*77c1e3ccSAndroid Build Coastguard Worker   (void)file;
698*77c1e3ccSAndroid Build Coastguard Worker   aom_internal_error(cm->error, AOM_CODEC_ERROR,
699*77c1e3ccSAndroid Build Coastguard Worker                      "To utilize three-pass encoding, libaom must be built "
700*77c1e3ccSAndroid Build Coastguard Worker                      "with CONFIG_THREE_PASS=1 & CONFIG_AV1_DECODER=1.");
701*77c1e3ccSAndroid Build Coastguard Worker }
702*77c1e3ccSAndroid Build Coastguard Worker 
av1_free_thirdpass_ctx(THIRD_PASS_DEC_CTX * ctx)703*77c1e3ccSAndroid Build Coastguard Worker void av1_free_thirdpass_ctx(THIRD_PASS_DEC_CTX *ctx) { (void)ctx; }
704*77c1e3ccSAndroid Build Coastguard Worker 
av1_set_gop_third_pass(THIRD_PASS_DEC_CTX * ctx)705*77c1e3ccSAndroid Build Coastguard Worker void av1_set_gop_third_pass(THIRD_PASS_DEC_CTX *ctx) { (void)ctx; }
706*77c1e3ccSAndroid Build Coastguard Worker 
av1_pop_third_pass_info(THIRD_PASS_DEC_CTX * ctx)707*77c1e3ccSAndroid Build Coastguard Worker void av1_pop_third_pass_info(THIRD_PASS_DEC_CTX *ctx) { (void)ctx; }
708*77c1e3ccSAndroid Build Coastguard Worker 
av1_open_second_pass_log(struct AV1_COMP * cpi,int is_read)709*77c1e3ccSAndroid Build Coastguard Worker void av1_open_second_pass_log(struct AV1_COMP *cpi, int is_read) {
710*77c1e3ccSAndroid Build Coastguard Worker   (void)cpi;
711*77c1e3ccSAndroid Build Coastguard Worker   (void)is_read;
712*77c1e3ccSAndroid Build Coastguard Worker }
713*77c1e3ccSAndroid Build Coastguard Worker 
av1_close_second_pass_log(struct AV1_COMP * cpi)714*77c1e3ccSAndroid Build Coastguard Worker void av1_close_second_pass_log(struct AV1_COMP *cpi) { (void)cpi; }
715*77c1e3ccSAndroid Build Coastguard Worker 
av1_write_second_pass_gop_info(struct AV1_COMP * cpi)716*77c1e3ccSAndroid Build Coastguard Worker void av1_write_second_pass_gop_info(struct AV1_COMP *cpi) { (void)cpi; }
717*77c1e3ccSAndroid Build Coastguard Worker 
av1_write_second_pass_per_frame_info(struct AV1_COMP * cpi,int gf_index)718*77c1e3ccSAndroid Build Coastguard Worker void av1_write_second_pass_per_frame_info(struct AV1_COMP *cpi, int gf_index) {
719*77c1e3ccSAndroid Build Coastguard Worker   (void)cpi;
720*77c1e3ccSAndroid Build Coastguard Worker   (void)gf_index;
721*77c1e3ccSAndroid Build Coastguard Worker }
722*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_second_pass_gop_info(FILE * second_pass_log_stream,THIRD_PASS_GOP_INFO * gop_info,struct aom_internal_error_info * error)723*77c1e3ccSAndroid Build Coastguard Worker void av1_read_second_pass_gop_info(FILE *second_pass_log_stream,
724*77c1e3ccSAndroid Build Coastguard Worker                                    THIRD_PASS_GOP_INFO *gop_info,
725*77c1e3ccSAndroid Build Coastguard Worker                                    struct aom_internal_error_info *error) {
726*77c1e3ccSAndroid Build Coastguard Worker   (void)second_pass_log_stream;
727*77c1e3ccSAndroid Build Coastguard Worker   (void)gop_info;
728*77c1e3ccSAndroid Build Coastguard Worker   (void)error;
729*77c1e3ccSAndroid Build Coastguard Worker }
730*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_second_pass_per_frame_info(FILE * second_pass_log_stream,THIRD_PASS_FRAME_INFO * frame_info_arr,int frame_info_count,struct aom_internal_error_info * error)731*77c1e3ccSAndroid Build Coastguard Worker void av1_read_second_pass_per_frame_info(
732*77c1e3ccSAndroid Build Coastguard Worker     FILE *second_pass_log_stream, THIRD_PASS_FRAME_INFO *frame_info_arr,
733*77c1e3ccSAndroid Build Coastguard Worker     int frame_info_count, struct aom_internal_error_info *error) {
734*77c1e3ccSAndroid Build Coastguard Worker   (void)second_pass_log_stream;
735*77c1e3ccSAndroid Build Coastguard Worker   (void)frame_info_arr;
736*77c1e3ccSAndroid Build Coastguard Worker   (void)frame_info_count;
737*77c1e3ccSAndroid Build Coastguard Worker   (void)error;
738*77c1e3ccSAndroid Build Coastguard Worker }
739*77c1e3ccSAndroid Build Coastguard Worker 
av1_check_use_arf(THIRD_PASS_DEC_CTX * ctx)740*77c1e3ccSAndroid Build Coastguard Worker int av1_check_use_arf(THIRD_PASS_DEC_CTX *ctx) {
741*77c1e3ccSAndroid Build Coastguard Worker   (void)ctx;
742*77c1e3ccSAndroid Build Coastguard Worker   return 1;
743*77c1e3ccSAndroid Build Coastguard Worker }
744*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_ratio(THIRD_PASS_DEC_CTX * ctx,int fidx,int fheight,int fwidth,double * ratio_h,double * ratio_w)745*77c1e3ccSAndroid Build Coastguard Worker void av1_get_third_pass_ratio(THIRD_PASS_DEC_CTX *ctx, int fidx, int fheight,
746*77c1e3ccSAndroid Build Coastguard Worker                               int fwidth, double *ratio_h, double *ratio_w) {
747*77c1e3ccSAndroid Build Coastguard Worker   (void)ctx;
748*77c1e3ccSAndroid Build Coastguard Worker   (void)fidx;
749*77c1e3ccSAndroid Build Coastguard Worker   (void)fheight;
750*77c1e3ccSAndroid Build Coastguard Worker   (void)fwidth;
751*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_h;
752*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_w;
753*77c1e3ccSAndroid Build Coastguard Worker }
754*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_mi(THIRD_PASS_DEC_CTX * ctx,int fidx,int mi_row,int mi_col,double ratio_h,double ratio_w)755*77c1e3ccSAndroid Build Coastguard Worker THIRD_PASS_MI_INFO *av1_get_third_pass_mi(THIRD_PASS_DEC_CTX *ctx, int fidx,
756*77c1e3ccSAndroid Build Coastguard Worker                                           int mi_row, int mi_col,
757*77c1e3ccSAndroid Build Coastguard Worker                                           double ratio_h, double ratio_w) {
758*77c1e3ccSAndroid Build Coastguard Worker   (void)ctx;
759*77c1e3ccSAndroid Build Coastguard Worker   (void)fidx;
760*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_row;
761*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_col;
762*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_h;
763*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_w;
764*77c1e3ccSAndroid Build Coastguard Worker   return NULL;
765*77c1e3ccSAndroid Build Coastguard Worker }
766*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_adjusted_mv(THIRD_PASS_MI_INFO * this_mi,double ratio_h,double ratio_w,MV_REFERENCE_FRAME frame)767*77c1e3ccSAndroid Build Coastguard Worker int_mv av1_get_third_pass_adjusted_mv(THIRD_PASS_MI_INFO *this_mi,
768*77c1e3ccSAndroid Build Coastguard Worker                                       double ratio_h, double ratio_w,
769*77c1e3ccSAndroid Build Coastguard Worker                                       MV_REFERENCE_FRAME frame) {
770*77c1e3ccSAndroid Build Coastguard Worker   (void)this_mi;
771*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_h;
772*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_w;
773*77c1e3ccSAndroid Build Coastguard Worker   (void)frame;
774*77c1e3ccSAndroid Build Coastguard Worker   int_mv mv;
775*77c1e3ccSAndroid Build Coastguard Worker   mv.as_int = INVALID_MV;
776*77c1e3ccSAndroid Build Coastguard Worker   return mv;
777*77c1e3ccSAndroid Build Coastguard Worker }
778*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_third_pass_adjusted_blk_size(THIRD_PASS_MI_INFO * this_mi,double ratio_h,double ratio_w)779*77c1e3ccSAndroid Build Coastguard Worker BLOCK_SIZE av1_get_third_pass_adjusted_blk_size(THIRD_PASS_MI_INFO *this_mi,
780*77c1e3ccSAndroid Build Coastguard Worker                                                 double ratio_h,
781*77c1e3ccSAndroid Build Coastguard Worker                                                 double ratio_w) {
782*77c1e3ccSAndroid Build Coastguard Worker   (void)this_mi;
783*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_h;
784*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_w;
785*77c1e3ccSAndroid Build Coastguard Worker   return BLOCK_INVALID;
786*77c1e3ccSAndroid Build Coastguard Worker }
787*77c1e3ccSAndroid Build Coastguard Worker 
av1_third_pass_get_adjusted_mi(THIRD_PASS_MI_INFO * third_pass_mi,double ratio_h,double ratio_w,int * mi_row,int * mi_col)788*77c1e3ccSAndroid Build Coastguard Worker void av1_third_pass_get_adjusted_mi(THIRD_PASS_MI_INFO *third_pass_mi,
789*77c1e3ccSAndroid Build Coastguard Worker                                     double ratio_h, double ratio_w, int *mi_row,
790*77c1e3ccSAndroid Build Coastguard Worker                                     int *mi_col) {
791*77c1e3ccSAndroid Build Coastguard Worker   (void)third_pass_mi;
792*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_h;
793*77c1e3ccSAndroid Build Coastguard Worker   (void)ratio_w;
794*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_row;
795*77c1e3ccSAndroid Build Coastguard Worker   (void)mi_col;
796*77c1e3ccSAndroid Build Coastguard Worker }
797*77c1e3ccSAndroid Build Coastguard Worker 
av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX * ctx,THIRD_PASS_MI_INFO * this_mi)798*77c1e3ccSAndroid Build Coastguard Worker PARTITION_TYPE av1_third_pass_get_sb_part_type(THIRD_PASS_DEC_CTX *ctx,
799*77c1e3ccSAndroid Build Coastguard Worker                                                THIRD_PASS_MI_INFO *this_mi) {
800*77c1e3ccSAndroid Build Coastguard Worker   (void)ctx;
801*77c1e3ccSAndroid Build Coastguard Worker   (void)this_mi;
802*77c1e3ccSAndroid Build Coastguard Worker   return PARTITION_INVALID;
803*77c1e3ccSAndroid Build Coastguard Worker }
804*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_THREE_PASS && CONFIG_AV1_DECODER
805*77c1e3ccSAndroid Build Coastguard Worker 
806*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_BITRATE_ACCURACY
fwrite_and_check(const void * ptr,size_t size,size_t nmemb,FILE * stream,struct aom_internal_error_info * error)807*77c1e3ccSAndroid Build Coastguard Worker static void fwrite_and_check(const void *ptr, size_t size, size_t nmemb,
808*77c1e3ccSAndroid Build Coastguard Worker                              FILE *stream,
809*77c1e3ccSAndroid Build Coastguard Worker                              struct aom_internal_error_info *error) {
810*77c1e3ccSAndroid Build Coastguard Worker   size_t count = fwrite(ptr, size, nmemb, stream);
811*77c1e3ccSAndroid Build Coastguard Worker   if (count < nmemb) {
812*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(error, AOM_CODEC_ERROR, "fwrite_and_check failed\n");
813*77c1e3ccSAndroid Build Coastguard Worker   }
814*77c1e3ccSAndroid Build Coastguard Worker }
815*77c1e3ccSAndroid Build Coastguard Worker 
fread_and_check(void * ptr,size_t size,size_t nmemb,FILE * stream,struct aom_internal_error_info * error)816*77c1e3ccSAndroid Build Coastguard Worker static void fread_and_check(void *ptr, size_t size, size_t nmemb, FILE *stream,
817*77c1e3ccSAndroid Build Coastguard Worker                             struct aom_internal_error_info *error) {
818*77c1e3ccSAndroid Build Coastguard Worker   size_t count = fread(ptr, size, nmemb, stream);
819*77c1e3ccSAndroid Build Coastguard Worker   if (count < nmemb) {
820*77c1e3ccSAndroid Build Coastguard Worker     aom_internal_error(error, AOM_CODEC_ERROR, "fread_and_check failed\n");
821*77c1e3ccSAndroid Build Coastguard Worker   }
822*77c1e3ccSAndroid Build Coastguard Worker }
823*77c1e3ccSAndroid Build Coastguard Worker 
av1_pack_tpl_info(TPL_INFO * tpl_info,const GF_GROUP * gf_group,const TplParams * tpl_data)824*77c1e3ccSAndroid Build Coastguard Worker void av1_pack_tpl_info(TPL_INFO *tpl_info, const GF_GROUP *gf_group,
825*77c1e3ccSAndroid Build Coastguard Worker                        const TplParams *tpl_data) {
826*77c1e3ccSAndroid Build Coastguard Worker   tpl_info->tpl_ready = tpl_data->ready;
827*77c1e3ccSAndroid Build Coastguard Worker   if (tpl_info->tpl_ready) {
828*77c1e3ccSAndroid Build Coastguard Worker     tpl_info->gf_length = gf_group->size;
829*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < tpl_info->gf_length; ++i) {
830*77c1e3ccSAndroid Build Coastguard Worker       tpl_info->txfm_stats_list[i] = tpl_data->txfm_stats_list[i];
831*77c1e3ccSAndroid Build Coastguard Worker       tpl_info->qstep_ratio_ls[i] = av1_tpl_get_qstep_ratio(tpl_data, i);
832*77c1e3ccSAndroid Build Coastguard Worker       tpl_info->update_type_list[i] = gf_group->update_type[i];
833*77c1e3ccSAndroid Build Coastguard Worker     }
834*77c1e3ccSAndroid Build Coastguard Worker   }
835*77c1e3ccSAndroid Build Coastguard Worker }
836*77c1e3ccSAndroid Build Coastguard Worker 
av1_write_tpl_info(const TPL_INFO * tpl_info,FILE * log_stream,struct aom_internal_error_info * error)837*77c1e3ccSAndroid Build Coastguard Worker void av1_write_tpl_info(const TPL_INFO *tpl_info, FILE *log_stream,
838*77c1e3ccSAndroid Build Coastguard Worker                         struct aom_internal_error_info *error) {
839*77c1e3ccSAndroid Build Coastguard Worker   fwrite_and_check(&tpl_info->tpl_ready, sizeof(tpl_info->tpl_ready), 1,
840*77c1e3ccSAndroid Build Coastguard Worker                    log_stream, error);
841*77c1e3ccSAndroid Build Coastguard Worker   if (tpl_info->tpl_ready) {
842*77c1e3ccSAndroid Build Coastguard Worker     fwrite_and_check(&tpl_info->gf_length, sizeof(tpl_info->gf_length), 1,
843*77c1e3ccSAndroid Build Coastguard Worker                      log_stream, error);
844*77c1e3ccSAndroid Build Coastguard Worker     assert(tpl_info->gf_length <= MAX_LENGTH_TPL_FRAME_STATS);
845*77c1e3ccSAndroid Build Coastguard Worker     fwrite_and_check(&tpl_info->txfm_stats_list,
846*77c1e3ccSAndroid Build Coastguard Worker                      sizeof(tpl_info->txfm_stats_list[0]), tpl_info->gf_length,
847*77c1e3ccSAndroid Build Coastguard Worker                      log_stream, error);
848*77c1e3ccSAndroid Build Coastguard Worker     fwrite_and_check(&tpl_info->qstep_ratio_ls,
849*77c1e3ccSAndroid Build Coastguard Worker                      sizeof(tpl_info->qstep_ratio_ls[0]), tpl_info->gf_length,
850*77c1e3ccSAndroid Build Coastguard Worker                      log_stream, error);
851*77c1e3ccSAndroid Build Coastguard Worker     fwrite_and_check(&tpl_info->update_type_list,
852*77c1e3ccSAndroid Build Coastguard Worker                      sizeof(tpl_info->update_type_list[0]), tpl_info->gf_length,
853*77c1e3ccSAndroid Build Coastguard Worker                      log_stream, error);
854*77c1e3ccSAndroid Build Coastguard Worker   }
855*77c1e3ccSAndroid Build Coastguard Worker }
856*77c1e3ccSAndroid Build Coastguard Worker 
av1_read_tpl_info(TPL_INFO * tpl_info,FILE * log_stream,struct aom_internal_error_info * error)857*77c1e3ccSAndroid Build Coastguard Worker void av1_read_tpl_info(TPL_INFO *tpl_info, FILE *log_stream,
858*77c1e3ccSAndroid Build Coastguard Worker                        struct aom_internal_error_info *error) {
859*77c1e3ccSAndroid Build Coastguard Worker   av1_zero(*tpl_info);
860*77c1e3ccSAndroid Build Coastguard Worker   fread_and_check(&tpl_info->tpl_ready, sizeof(tpl_info->tpl_ready), 1,
861*77c1e3ccSAndroid Build Coastguard Worker                   log_stream, error);
862*77c1e3ccSAndroid Build Coastguard Worker   if (tpl_info->tpl_ready) {
863*77c1e3ccSAndroid Build Coastguard Worker     fread_and_check(&tpl_info->gf_length, sizeof(tpl_info->gf_length), 1,
864*77c1e3ccSAndroid Build Coastguard Worker                     log_stream, error);
865*77c1e3ccSAndroid Build Coastguard Worker     assert(tpl_info->gf_length <= MAX_LENGTH_TPL_FRAME_STATS);
866*77c1e3ccSAndroid Build Coastguard Worker     fread_and_check(&tpl_info->txfm_stats_list,
867*77c1e3ccSAndroid Build Coastguard Worker                     sizeof(tpl_info->txfm_stats_list[0]), tpl_info->gf_length,
868*77c1e3ccSAndroid Build Coastguard Worker                     log_stream, error);
869*77c1e3ccSAndroid Build Coastguard Worker     fread_and_check(&tpl_info->qstep_ratio_ls,
870*77c1e3ccSAndroid Build Coastguard Worker                     sizeof(tpl_info->qstep_ratio_ls[0]), tpl_info->gf_length,
871*77c1e3ccSAndroid Build Coastguard Worker                     log_stream, error);
872*77c1e3ccSAndroid Build Coastguard Worker     fread_and_check(&tpl_info->update_type_list,
873*77c1e3ccSAndroid Build Coastguard Worker                     sizeof(tpl_info->update_type_list[0]), tpl_info->gf_length,
874*77c1e3ccSAndroid Build Coastguard Worker                     log_stream, error);
875*77c1e3ccSAndroid Build Coastguard Worker   }
876*77c1e3ccSAndroid Build Coastguard Worker }
877*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_BITRATE_ACCURACY
878