• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <assert.h>
13 #include <limits.h>
14 #include <stdio.h>
15 
16 #include "config/av1_rtcd.h"
17 #include "config/aom_dsp_rtcd.h"
18 #include "config/aom_scale_rtcd.h"
19 
20 #include "aom_dsp/aom_dsp_common.h"
21 #include "aom_mem/aom_mem.h"
22 #include "aom_ports/system_state.h"
23 #include "aom_ports/aom_once.h"
24 #include "aom_ports/aom_timer.h"
25 #include "aom_scale/aom_scale.h"
26 #include "aom_util/aom_thread.h"
27 
28 #include "av1/common/alloccommon.h"
29 #include "av1/common/av1_common_int.h"
30 #include "av1/common/av1_loopfilter.h"
31 #include "av1/common/quant_common.h"
32 #include "av1/common/reconinter.h"
33 #include "av1/common/reconintra.h"
34 
35 #include "av1/decoder/decodeframe.h"
36 #include "av1/decoder/decoder.h"
37 #include "av1/decoder/detokenize.h"
38 #include "av1/decoder/obu.h"
39 
initialize_dec(void)40 static void initialize_dec(void) {
41   av1_rtcd();
42   aom_dsp_rtcd();
43   aom_scale_rtcd();
44   av1_init_intra_predictors();
45   av1_init_wedge_masks();
46 }
47 
dec_set_mb_mi(CommonModeInfoParams * mi_params,int width,int height)48 static void dec_set_mb_mi(CommonModeInfoParams *mi_params, int width,
49                           int height) {
50   // Ensure that the decoded width and height are both multiples of
51   // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
52   // subsampling is used).
53   // This simplifies the implementation of various experiments,
54   // eg. cdef, which operates on units of 8x8 luma pixels.
55   const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
56   const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
57 
58   mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2;
59   mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2;
60   mi_params->mi_stride = calc_mi_size(mi_params->mi_cols);
61 
62   mi_params->mb_cols = (mi_params->mi_cols + 2) >> 2;
63   mi_params->mb_rows = (mi_params->mi_rows + 2) >> 2;
64   mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols;
65 
66   mi_params->mi_alloc_bsize = BLOCK_4X4;
67   mi_params->mi_alloc_stride = mi_params->mi_stride;
68 
69   assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
70          mi_size_high[mi_params->mi_alloc_bsize]);
71 
72 #if CONFIG_LPF_MASK
73   av1_alloc_loop_filter_mask(mi_params);
74 #endif
75 }
76 
dec_setup_mi(CommonModeInfoParams * mi_params)77 static void dec_setup_mi(CommonModeInfoParams *mi_params) {
78   const int mi_grid_size =
79       mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
80   memset(mi_params->mi_grid_base, 0,
81          mi_grid_size * sizeof(*mi_params->mi_grid_base));
82 }
83 
dec_free_mi(CommonModeInfoParams * mi_params)84 static void dec_free_mi(CommonModeInfoParams *mi_params) {
85   aom_free(mi_params->mi_alloc);
86   mi_params->mi_alloc = NULL;
87   aom_free(mi_params->mi_grid_base);
88   mi_params->mi_grid_base = NULL;
89   mi_params->mi_alloc_size = 0;
90   aom_free(mi_params->tx_type_map);
91   mi_params->tx_type_map = NULL;
92 }
93 
av1_decoder_create(BufferPool * const pool)94 AV1Decoder *av1_decoder_create(BufferPool *const pool) {
95   AV1Decoder *volatile const pbi = aom_memalign(32, sizeof(*pbi));
96   if (!pbi) return NULL;
97   av1_zero(*pbi);
98 
99   AV1_COMMON *volatile const cm = &pbi->common;
100 
101   // The jmp_buf is valid only for the duration of the function that calls
102   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
103   // before it returns.
104   if (setjmp(cm->error.jmp)) {
105     cm->error.setjmp = 0;
106     av1_decoder_remove(pbi);
107     return NULL;
108   }
109 
110   cm->error.setjmp = 1;
111 
112   CHECK_MEM_ERROR(cm, cm->fc,
113                   (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
114   CHECK_MEM_ERROR(
115       cm, cm->default_frame_context,
116       (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
117   memset(cm->fc, 0, sizeof(*cm->fc));
118   memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
119 
120   pbi->need_resync = 1;
121   aom_once(initialize_dec);
122 
123   // Initialize the references to not point to any frame buffers.
124   for (int i = 0; i < REF_FRAMES; i++) {
125     cm->ref_frame_map[i] = NULL;
126   }
127 
128   cm->current_frame.frame_number = 0;
129   pbi->decoding_first_frame = 1;
130   pbi->common.buffer_pool = pool;
131 
132   cm->seq_params.bit_depth = AOM_BITS_8;
133 
134   cm->mi_params.free_mi = dec_free_mi;
135   cm->mi_params.setup_mi = dec_setup_mi;
136   cm->mi_params.set_mb_mi = dec_set_mb_mi;
137 
138   av1_loop_filter_init(cm);
139 
140   av1_qm_init(&cm->quant_params, av1_num_planes(cm));
141   av1_loop_restoration_precal();
142 #if CONFIG_ACCOUNTING
143   pbi->acct_enabled = 1;
144   aom_accounting_init(&pbi->accounting);
145 #endif
146 
147   cm->error.setjmp = 0;
148 
149   aom_get_worker_interface()->init(&pbi->lf_worker);
150   pbi->lf_worker.thread_name = "aom lf worker";
151 
152   return pbi;
153 }
154 
av1_dealloc_dec_jobs(struct AV1DecTileMTData * tile_mt_info)155 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info) {
156   if (tile_mt_info != NULL) {
157 #if CONFIG_MULTITHREAD
158     if (tile_mt_info->job_mutex != NULL) {
159       pthread_mutex_destroy(tile_mt_info->job_mutex);
160       aom_free(tile_mt_info->job_mutex);
161     }
162 #endif
163     aom_free(tile_mt_info->job_queue);
164     // clear the structure as the source of this call may be a resize in which
165     // case this call will be followed by an _alloc() which may fail.
166     av1_zero(*tile_mt_info);
167   }
168 }
169 
av1_dec_free_cb_buf(AV1Decoder * pbi)170 void av1_dec_free_cb_buf(AV1Decoder *pbi) {
171   aom_free(pbi->cb_buffer_base);
172   pbi->cb_buffer_base = NULL;
173   pbi->cb_buffer_alloc_size = 0;
174 }
175 
av1_decoder_remove(AV1Decoder * pbi)176 void av1_decoder_remove(AV1Decoder *pbi) {
177   int i;
178 
179   if (!pbi) return;
180 
181   // Free the tile list output buffer.
182   aom_free_frame_buffer(&pbi->tile_list_outbuf);
183 
184   aom_get_worker_interface()->end(&pbi->lf_worker);
185   aom_free(pbi->lf_worker.data1);
186 
187   if (pbi->thread_data) {
188     for (int worker_idx = 0; worker_idx < pbi->max_threads - 1; worker_idx++) {
189       DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
190       av1_free_mc_tmp_buf(thread_data->td);
191       aom_free(thread_data->td);
192     }
193     aom_free(pbi->thread_data);
194   }
195 
196   for (i = 0; i < pbi->num_workers; ++i) {
197     AVxWorker *const worker = &pbi->tile_workers[i];
198     aom_get_worker_interface()->end(worker);
199   }
200 #if CONFIG_MULTITHREAD
201   if (pbi->row_mt_mutex_ != NULL) {
202     pthread_mutex_destroy(pbi->row_mt_mutex_);
203     aom_free(pbi->row_mt_mutex_);
204   }
205   if (pbi->row_mt_cond_ != NULL) {
206     pthread_cond_destroy(pbi->row_mt_cond_);
207     aom_free(pbi->row_mt_cond_);
208   }
209 #endif
210   for (i = 0; i < pbi->allocated_tiles; i++) {
211     TileDataDec *const tile_data = pbi->tile_data + i;
212     av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
213   }
214   aom_free(pbi->tile_data);
215   aom_free(pbi->tile_workers);
216 
217   if (pbi->num_workers > 0) {
218     av1_loop_filter_dealloc(&pbi->lf_row_sync);
219     av1_loop_restoration_dealloc(&pbi->lr_row_sync, pbi->num_workers);
220     av1_dealloc_dec_jobs(&pbi->tile_mt_info);
221   }
222 
223   av1_dec_free_cb_buf(pbi);
224 #if CONFIG_ACCOUNTING
225   aom_accounting_clear(&pbi->accounting);
226 #endif
227   av1_free_mc_tmp_buf(&pbi->td);
228   aom_img_metadata_array_free(pbi->metadata);
229   aom_free(pbi);
230 }
231 
av1_visit_palette(AV1Decoder * const pbi,MACROBLOCKD * const xd,aom_reader * r,palette_visitor_fn_t visit)232 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
233                        aom_reader *r, palette_visitor_fn_t visit) {
234   if (!is_inter_block(xd->mi[0])) {
235     for (int plane = 0; plane < AOMMIN(2, av1_num_planes(&pbi->common));
236          ++plane) {
237       if (plane == 0 || xd->is_chroma_ref) {
238         if (xd->mi[0]->palette_mode_info.palette_size[plane])
239           visit(xd, plane, r);
240       } else {
241         assert(xd->mi[0]->palette_mode_info.palette_size[plane] == 0);
242       }
243     }
244   }
245 }
246 
equal_dimensions(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)247 static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
248                             const YV12_BUFFER_CONFIG *b) {
249   return a->y_height == b->y_height && a->y_width == b->y_width &&
250          a->uv_height == b->uv_height && a->uv_width == b->uv_width;
251 }
252 
av1_copy_reference_dec(AV1Decoder * pbi,int idx,YV12_BUFFER_CONFIG * sd)253 aom_codec_err_t av1_copy_reference_dec(AV1Decoder *pbi, int idx,
254                                        YV12_BUFFER_CONFIG *sd) {
255   AV1_COMMON *cm = &pbi->common;
256   const int num_planes = av1_num_planes(cm);
257 
258   const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, idx);
259   if (cfg == NULL) {
260     aom_internal_error(&cm->error, AOM_CODEC_ERROR, "No reference frame");
261     return AOM_CODEC_ERROR;
262   }
263   if (!equal_dimensions(cfg, sd))
264     aom_internal_error(&cm->error, AOM_CODEC_ERROR,
265                        "Incorrect buffer dimensions");
266   else
267     aom_yv12_copy_frame(cfg, sd, num_planes);
268 
269   return cm->error.error_code;
270 }
271 
equal_dimensions_and_border(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)272 static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
273                                        const YV12_BUFFER_CONFIG *b) {
274   return a->y_height == b->y_height && a->y_width == b->y_width &&
275          a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
276          a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
277          a->border == b->border &&
278          (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
279              (b->flags & YV12_FLAG_HIGHBITDEPTH);
280 }
281 
av1_set_reference_dec(AV1_COMMON * cm,int idx,int use_external_ref,YV12_BUFFER_CONFIG * sd)282 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
283                                       int use_external_ref,
284                                       YV12_BUFFER_CONFIG *sd) {
285   const int num_planes = av1_num_planes(cm);
286   YV12_BUFFER_CONFIG *ref_buf = NULL;
287 
288   // Get the destination reference buffer.
289   ref_buf = get_ref_frame(cm, idx);
290 
291   if (ref_buf == NULL) {
292     aom_internal_error(&cm->error, AOM_CODEC_ERROR, "No reference frame");
293     return AOM_CODEC_ERROR;
294   }
295 
296   if (!use_external_ref) {
297     if (!equal_dimensions(ref_buf, sd)) {
298       aom_internal_error(&cm->error, AOM_CODEC_ERROR,
299                          "Incorrect buffer dimensions");
300     } else {
301       // Overwrite the reference frame buffer.
302       aom_yv12_copy_frame(sd, ref_buf, num_planes);
303     }
304   } else {
305     if (!equal_dimensions_and_border(ref_buf, sd)) {
306       aom_internal_error(&cm->error, AOM_CODEC_ERROR,
307                          "Incorrect buffer dimensions");
308     } else {
309       // Overwrite the reference frame buffer pointers.
310       // Once we no longer need the external reference buffer, these pointers
311       // are restored.
312       ref_buf->store_buf_adr[0] = ref_buf->y_buffer;
313       ref_buf->store_buf_adr[1] = ref_buf->u_buffer;
314       ref_buf->store_buf_adr[2] = ref_buf->v_buffer;
315       ref_buf->y_buffer = sd->y_buffer;
316       ref_buf->u_buffer = sd->u_buffer;
317       ref_buf->v_buffer = sd->v_buffer;
318       ref_buf->use_external_reference_buffers = 1;
319     }
320   }
321 
322   return cm->error.error_code;
323 }
324 
av1_copy_new_frame_dec(AV1_COMMON * cm,YV12_BUFFER_CONFIG * new_frame,YV12_BUFFER_CONFIG * sd)325 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
326                                        YV12_BUFFER_CONFIG *new_frame,
327                                        YV12_BUFFER_CONFIG *sd) {
328   const int num_planes = av1_num_planes(cm);
329 
330   if (!equal_dimensions_and_border(new_frame, sd))
331     aom_internal_error(&cm->error, AOM_CODEC_ERROR,
332                        "Incorrect buffer dimensions");
333   else
334     aom_yv12_copy_frame(new_frame, sd, num_planes);
335 
336   return cm->error.error_code;
337 }
338 
release_current_frame(AV1Decoder * pbi)339 static void release_current_frame(AV1Decoder *pbi) {
340   AV1_COMMON *const cm = &pbi->common;
341   BufferPool *const pool = cm->buffer_pool;
342 
343   cm->cur_frame->buf.corrupted = 1;
344   lock_buffer_pool(pool);
345   decrease_ref_count(cm->cur_frame, pool);
346   unlock_buffer_pool(pool);
347   cm->cur_frame = NULL;
348 }
349 
350 // If any buffer updating is signaled it should be done here.
351 // Consumes a reference to cm->cur_frame.
352 //
353 // This functions returns void. It reports failure by setting
354 // cm->error.error_code.
update_frame_buffers(AV1Decoder * pbi,int frame_decoded)355 static void update_frame_buffers(AV1Decoder *pbi, int frame_decoded) {
356   int ref_index = 0, mask;
357   AV1_COMMON *const cm = &pbi->common;
358   BufferPool *const pool = cm->buffer_pool;
359 
360   if (frame_decoded) {
361     lock_buffer_pool(pool);
362 
363     // In ext-tile decoding, the camera frame header is only decoded once. So,
364     // we don't update the references here.
365     if (!pbi->camera_frame_header_ready) {
366       // The following for loop needs to release the reference stored in
367       // cm->ref_frame_map[ref_index] before storing a reference to
368       // cm->cur_frame in cm->ref_frame_map[ref_index].
369       for (mask = cm->current_frame.refresh_frame_flags; mask; mask >>= 1) {
370         if (mask & 1) {
371           decrease_ref_count(cm->ref_frame_map[ref_index], pool);
372           cm->ref_frame_map[ref_index] = cm->cur_frame;
373           ++cm->cur_frame->ref_count;
374         }
375         ++ref_index;
376       }
377     }
378 
379     if (cm->show_existing_frame || cm->show_frame) {
380       if (pbi->output_all_layers) {
381         // Append this frame to the output queue
382         if (pbi->num_output_frames >= MAX_NUM_SPATIAL_LAYERS) {
383           // We can't store the new frame anywhere, so drop it and return an
384           // error
385           cm->cur_frame->buf.corrupted = 1;
386           decrease_ref_count(cm->cur_frame, pool);
387           cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
388         } else {
389           pbi->output_frames[pbi->num_output_frames] = cm->cur_frame;
390           pbi->num_output_frames++;
391         }
392       } else {
393         // Replace any existing output frame
394         assert(pbi->num_output_frames == 0 || pbi->num_output_frames == 1);
395         if (pbi->num_output_frames > 0) {
396           decrease_ref_count(pbi->output_frames[0], pool);
397         }
398         pbi->output_frames[0] = cm->cur_frame;
399         pbi->num_output_frames = 1;
400       }
401     } else {
402       decrease_ref_count(cm->cur_frame, pool);
403     }
404 
405     unlock_buffer_pool(pool);
406   } else {
407     // Nothing was decoded, so just drop this frame buffer
408     lock_buffer_pool(pool);
409     decrease_ref_count(cm->cur_frame, pool);
410     unlock_buffer_pool(pool);
411   }
412   cm->cur_frame = NULL;
413 
414   if (!pbi->camera_frame_header_ready) {
415     // Invalidate these references until the next frame starts.
416     for (ref_index = 0; ref_index < INTER_REFS_PER_FRAME; ref_index++) {
417       cm->remapped_ref_idx[ref_index] = INVALID_IDX;
418     }
419   }
420 }
421 
av1_receive_compressed_data(AV1Decoder * pbi,size_t size,const uint8_t ** psource)422 int av1_receive_compressed_data(AV1Decoder *pbi, size_t size,
423                                 const uint8_t **psource) {
424   AV1_COMMON *volatile const cm = &pbi->common;
425   const uint8_t *source = *psource;
426   cm->error.error_code = AOM_CODEC_OK;
427   cm->error.has_detail = 0;
428 
429   if (size == 0) {
430     // This is used to signal that we are missing frames.
431     // We do not know if the missing frame(s) was supposed to update
432     // any of the reference buffers, but we act conservative and
433     // mark only the last buffer as corrupted.
434     //
435     // TODO(jkoleszar): Error concealment is undefined and non-normative
436     // at this point, but if it becomes so, [0] may not always be the correct
437     // thing to do here.
438     RefCntBuffer *ref_buf = get_ref_frame_buf(cm, LAST_FRAME);
439     if (ref_buf != NULL) ref_buf->buf.corrupted = 1;
440   }
441 
442   if (assign_cur_frame_new_fb(cm) == NULL) {
443     cm->error.error_code = AOM_CODEC_MEM_ERROR;
444     return 1;
445   }
446 
447   // The jmp_buf is valid only for the duration of the function that calls
448   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
449   // before it returns.
450   if (setjmp(cm->error.jmp)) {
451     const AVxWorkerInterface *const winterface = aom_get_worker_interface();
452     int i;
453 
454     cm->error.setjmp = 0;
455 
456     // Synchronize all threads immediately as a subsequent decode call may
457     // cause a resize invalidating some allocations.
458     winterface->sync(&pbi->lf_worker);
459     for (i = 0; i < pbi->num_workers; ++i) {
460       winterface->sync(&pbi->tile_workers[i]);
461     }
462 
463     release_current_frame(pbi);
464     aom_clear_system_state();
465     return -1;
466   }
467 
468   cm->error.setjmp = 1;
469 
470   int frame_decoded =
471       aom_decode_frame_from_obus(pbi, source, source + size, psource);
472 
473   if (frame_decoded < 0) {
474     assert(cm->error.error_code != AOM_CODEC_OK);
475     release_current_frame(pbi);
476     cm->error.setjmp = 0;
477     return 1;
478   }
479 
480 #if TXCOEFF_TIMER
481   cm->cum_txcoeff_timer += cm->txcoeff_timer;
482   fprintf(stderr,
483           "txb coeff block number: %d, frame time: %ld, cum time %ld in us\n",
484           cm->txb_count, cm->txcoeff_timer, cm->cum_txcoeff_timer);
485   cm->txcoeff_timer = 0;
486   cm->txb_count = 0;
487 #endif
488 
489   // Note: At this point, this function holds a reference to cm->cur_frame
490   // in the buffer pool. This reference is consumed by update_frame_buffers().
491   update_frame_buffers(pbi, frame_decoded);
492 
493   if (frame_decoded) {
494     pbi->decoding_first_frame = 0;
495   }
496 
497   if (cm->error.error_code != AOM_CODEC_OK) {
498     cm->error.setjmp = 0;
499     return 1;
500   }
501 
502   aom_clear_system_state();
503 
504   if (!cm->show_existing_frame) {
505     if (cm->seg.enabled) {
506       if (cm->prev_frame &&
507           (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
508           (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
509         cm->last_frame_seg_map = cm->prev_frame->seg_map;
510       } else {
511         cm->last_frame_seg_map = NULL;
512       }
513     }
514   }
515 
516   // Update progress in frame parallel decode.
517   cm->error.setjmp = 0;
518 
519   return 0;
520 }
521 
522 // Get the frame at a particular index in the output queue
av1_get_raw_frame(AV1Decoder * pbi,size_t index,YV12_BUFFER_CONFIG ** sd,aom_film_grain_t ** grain_params)523 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
524                       aom_film_grain_t **grain_params) {
525   if (index >= pbi->num_output_frames) return -1;
526   *sd = &pbi->output_frames[index]->buf;
527   *grain_params = &pbi->output_frames[index]->film_grain_params;
528   aom_clear_system_state();
529   return 0;
530 }
531 
532 // Get the highest-spatial-layer output
533 // TODO(david.barker): What should this do?
av1_get_frame_to_show(AV1Decoder * pbi,YV12_BUFFER_CONFIG * frame)534 int av1_get_frame_to_show(AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame) {
535   if (pbi->num_output_frames == 0) return -1;
536 
537   *frame = pbi->output_frames[pbi->num_output_frames - 1]->buf;
538   return 0;
539 }
540