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