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