<commit_before>/*Daala video codec
Copyright (c) 2006-2013 Daala project contributors.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "state.h"
#if defined(OD_X86ASM)
# include "x86/x86int.h"
#endif
#include "block_size.h"

/* OD_DC_RES[i] adjusts the quantization of DC for the ith plane.
   These values are based on manual tuning to optimize PSNR-HVS, while also
   attempting to keep a good visual balance between the relative resolution
   of luma, and chroma.
   FIXME: Tune this properly, see also OD_DEFAULT_QMS.*/
const od_coeff OD_DC_RES[3] = {17, 24, 17};

/* Scaling compensation for the Haar equivalent basis function. Left is
   for horizontal/vertical. Right is for diagonal. */
#if OD_DISABLE_FILTER || OD_DEBLOCKING
const od_coeff OD_DC_QM[OD_NBSIZES - 1][2] = {
  {16, 16}, {16, 16}, {16, 16}
};
#else
const od_coeff OD_DC_QM[OD_NBSIZES - 1][2] = {
  {21, 25}, {18, 20}, {17, 18}
};
#endif

/* Haar "quantization matrix" for each decomposition level (starting from LF).
   */
const int OD_HAAR_QM[2][OD_LOG_BSIZE_MAX] = {
  /* horizontal/vertical direction. */
  {16, 16, 16, 24, 32},
  /* "diagonal" direction. */
  {16, 16, 24, 32, 48},
};

void *od_aligned_malloc(size_t _sz,size_t _align) {
  unsigned char *p;
  if (_align - 1 > UCHAR_MAX || (_align&_align-1) || _sz > ~(size_t)0-_align)
    return NULL;
  p = (unsigned char *)malloc(_sz + _align);
  if (p != NULL) {
    int offs;
    offs = ((p-(unsigned char *)0) - 1 & _align - 1);
    p[offs] = offs;
    p += offs+1;
  }
  return p;
}

void od_aligned_free(void *_ptr) {
  unsigned char *p;
  p = (unsigned char *)_ptr;
  if (p != NULL) {
    int offs;
    offs = *--p;
    free(p - offs);
  }
}

void od_img_copy(od_img* dest, od_img* src) {
  int pli;
  OD_ASSERT(dest->width == src->width);
  OD_ASSERT(dest->height == src->height);
  OD_ASSERT(dest->nplanes == src->nplanes);
  for (pli = 0; pli < src->nplanes; pli++) {
    int width;
    int height;
    int row;
    width = dest->width >> dest->planes[pli].xdec;
    height = dest->height >> dest->planes[pli].ydec;
    for (row = 0; row < height; row++) {
      memcpy(dest->planes[pli].data + dest->planes[pli].ystride*row,
       src->planes[pli].data + src->planes[pli].ystride*row, width);
    }
  }
}

/*Initializes the buffers used for reference frames.
  These buffers are padded with 16 extra pixels on each side, to allow
   (relatively) unrestricted motion vectors without special casing reading
   outside the image boundary.
  If chroma is decimated in either direction, the padding is reduced by an
   appropriate factor on the appropriate sides.*/
static int od_state_ref_imgs_init(od_state *state, int nrefs) {
  daala_info *info;
  od_img *img;
  od_img_plane *iplane;
  unsigned char *ref_img_data;
  size_t data_sz;
  int frame_buf_width;
  int frame_buf_height;
  int plane_buf_width;
  int plane_buf_height;
  int reference_bytes;
  int reference_bits;
  int imgi;
  int pli;
  OD_ASSERT(nrefs == 4);
  info = &state->info;
  data_sz = 0;
  /*Reference bit depth is constant over the lifetime of od_state, and by
    extension, an encode or decode ctx.*/
  reference_bytes = state->full_precision_references ? 2 : 1;
  reference_bits = state->full_precision_references ? 8 + OD_COEFF_SHIFT : 8;
  /*TODO: Check for overflow before allocating.*/
  frame_buf_width = state->frame_width + (OD_BUFFER_PADDING << 1);
  frame_buf_height = state->frame_height + (OD_BUFFER_PADDING << 1);
  for (pli = 0; pli < info->nplanes; pli++) {
    /*Reserve space for this plane in nrefs reference images.*/
    plane_buf_width = frame_buf_width >> info->plane_info[pli].xdec;
    plane_buf_height = frame_buf_height >> info->plane_info[pli].ydec;
    data_sz += plane_buf_width*plane_buf_height*reference_bytes*nrefs;
  }
  state->ref_img_data = ref_img_data =
    (unsigned char *)od_aligned_malloc(data_sz, 32);
  if (OD_UNLIKELY(!ref_img_data)) {
    return OD_EFAULT;
  }
  /*Fill in the reference image structures.*/
  for (imgi = 0; imgi < nrefs; imgi++) {
    img = state->ref_imgs + imgi;
    img->nplanes = info->nplanes;
    img->width = state->frame_width;
    img->height = state->frame_height;
    for (pli = 0; pli < img->nplanes; pli++) {
      iplane = img->planes + pli;
      iplane->xdec = info->plane_info[pli].xdec;
      iplane->ydec = info->plane_info[pli].ydec;
      plane_buf_width = frame_buf_width >> iplane->xdec;
      plane_buf_height = frame_buf_height >> iplane->ydec;
      iplane->bitdepth = reference_bits;
      iplane->xstride = reference_bytes;
      iplane->ystride = plane_buf_width*reference_bytes;
      iplane->data = ref_img_data
       + (OD_BUFFER_PADDING >> iplane->xdec)*iplane->xstride
       + (OD_BUFFER_PADDING >> iplane->ydec)*iplane->ystride;
      ref_img_data += plane_buf_height*iplane->ystride;
    }
  }
  /*Mark all of the reference image buffers available.*/
  for (imgi = 0; imgi < nrefs; imgi++) state->ref_imgi[imgi] = -1;
  return OD_SUCCESS;
}

static int od_state_mvs_init(od_state *state) {
  state->mv_grid = (od_mv_grid_pt **)od_calloc_2d(state->nvmvbs + 1,
   state->nhmvbs + 1, sizeof(**state->mv_grid));
  if (OD_UNLIKELY(!state->mv_grid)) {
    return OD_EFAULT;
  }
  return OD_SUCCESS;
}

static void od_restore_fpu_c(void) {}

void od_restore_fpu(od_state *state) {
  (*state->opt_vtbl.restore_fpu)();
}

void od_state_opt_vtbl_init_c(od_state *state) {
  if (state->full_precision_references) {
    OD_ASSERT(0);
    /*Incoming:
      state->opt_vtbl.mc_predict1fmv = od_mc_predict1fmv16_c;
      state->opt_vtbl.mc_blend_full = od_mc_blend_full16_c;
      state->opt_vtbl.mc_blend_full_split = od_mc_blend_full_split16_c;
      state->opt_vtbl.mc_blend_multi = od_mc_blend_multi16_c;
      state->opt_vtbl.mc_blend_multi_split = od_mc_blend_multi_split16_c;*/
  }
  else {
    state->opt_vtbl.mc_predict1fmv = od_mc_predict1fmv8_c;
    state->opt_vtbl.mc_blend_full = od_mc_blend_full8_c;
    state->opt_vtbl.mc_blend_full_split = od_mc_blend_full_split8_c;
    state->opt_vtbl.mc_blend_full = od_mc_blend_multi8_c;
    state->opt_vtbl.mc_blend_full_split = od_mc_blend_multi_split8_c;
  }
  state->opt_vtbl.restore_fpu = od_restore_fpu_c;<commit_msg>Fix crash with assembly off introduced in d135749.<commit_after>/*Daala video codec
Copyright (c) 2006-2013 Daala project contributors.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "state.h"
#if defined(OD_X86ASM)
# include "x86/x86int.h"
#endif
#include "block_size.h"

/* OD_DC_RES[i] adjusts the quantization of DC for the ith plane.
   These values are based on manual tuning to optimize PSNR-HVS, while also
   attempting to keep a good visual balance between the relative resolution
   of luma, and chroma.
   FIXME: Tune this properly, see also OD_DEFAULT_QMS.*/
const od_coeff OD_DC_RES[3] = {17, 24, 17};

/* Scaling compensation for the Haar equivalent basis function. Left is
   for horizontal/vertical. Right is for diagonal. */
#if OD_DISABLE_FILTER || OD_DEBLOCKING
const od_coeff OD_DC_QM[OD_NBSIZES - 1][2] = {
  {16, 16}, {16, 16}, {16, 16}
};
#else
const od_coeff OD_DC_QM[OD_NBSIZES - 1][2] = {
  {21, 25}, {18, 20}, {17, 18}
};
#endif

/* Haar "quantization matrix" for each decomposition level (starting from LF).
   */
const int OD_HAAR_QM[2][OD_LOG_BSIZE_MAX] = {
  /* horizontal/vertical direction. */
  {16, 16, 16, 24, 32},
  /* "diagonal" direction. */
  {16, 16, 24, 32, 48},
};

void *od_aligned_malloc(size_t _sz,size_t _align) {
  unsigned char *p;
  if (_align - 1 > UCHAR_MAX || (_align&_align-1) || _sz > ~(size_t)0-_align)
    return NULL;
  p = (unsigned char *)malloc(_sz + _align);
  if (p != NULL) {
    int offs;
    offs = ((p-(unsigned char *)0) - 1 & _align - 1);
    p[offs] = offs;
    p += offs+1;
  }
  return p;
}

void od_aligned_free(void *_ptr) {
  unsigned char *p;
  p = (unsigned char *)_ptr;
  if (p != NULL) {
    int offs;
    offs = *--p;
    free(p - offs);
  }
}

void od_img_copy(od_img* dest, od_img* src) {
  int pli;
  OD_ASSERT(dest->width == src->width);
  OD_ASSERT(dest->height == src->height);
  OD_ASSERT(dest->nplanes == src->nplanes);
  for (pli = 0; pli < src->nplanes; pli++) {
    int width;
    int height;
    int row;
    width = dest->width >> dest->planes[pli].xdec;
    height = dest->height >> dest->planes[pli].ydec;
    for (row = 0; row < height; row++) {
      memcpy(dest->planes[pli].data + dest->planes[pli].ystride*row,
       src->planes[pli].data + src->planes[pli].ystride*row, width);
    }
  }
}

/*Initializes the buffers used for reference frames.
  These buffers are padded with 16 extra pixels on each side, to allow
   (relatively) unrestricted motion vectors without special casing reading
   outside the image boundary.
  If chroma is decimated in either direction, the padding is reduced by an
   appropriate factor on the appropriate sides.*/
static int od_state_ref_imgs_init(od_state *state, int nrefs) {
  daala_info *info;
  od_img *img;
  od_img_plane *iplane;
  unsigned char *ref_img_data;
  size_t data_sz;
  int frame_buf_width;
  int frame_buf_height;
  int plane_buf_width;
  int plane_buf_height;
  int reference_bytes;
  int reference_bits;
  int imgi;
  int pli;
  OD_ASSERT(nrefs == 4);
  info = &state->info;
  data_sz = 0;
  /*Reference bit depth is constant over the lifetime of od_state, and by
    extension, an encode or decode ctx.*/
  reference_bytes = state->full_precision_references ? 2 : 1;
  reference_bits = state->full_precision_references ? 8 + OD_COEFF_SHIFT : 8;
  /*TODO: Check for overflow before allocating.*/
  frame_buf_width = state->frame_width + (OD_BUFFER_PADDING << 1);
  frame_buf_height = state->frame_height + (OD_BUFFER_PADDING << 1);
  for (pli = 0; pli < info->nplanes; pli++) {
    /*Reserve space for this plane in nrefs reference images.*/
    plane_buf_width = frame_buf_width >> info->plane_info[pli].xdec;
    plane_buf_height = frame_buf_height >> info->plane_info[pli].ydec;
    data_sz += plane_buf_width*plane_buf_height*reference_bytes*nrefs;
  }
  state->ref_img_data = ref_img_data =
    (unsigned char *)od_aligned_malloc(data_sz, 32);
  if (OD_UNLIKELY(!ref_img_data)) {
    return OD_EFAULT;
  }
  /*Fill in the reference image structures.*/
  for (imgi = 0; imgi < nrefs; imgi++) {
    img = state->ref_imgs + imgi;
    img->nplanes = info->nplanes;
    img->width = state->frame_width;
    img->height = state->frame_height;
    for (pli = 0; pli < img->nplanes; pli++) {
      iplane = img->planes + pli;
      iplane->xdec = info->plane_info[pli].xdec;
      iplane->ydec = info->plane_info[pli].ydec;
      plane_buf_width = frame_buf_width >> iplane->xdec;
      plane_buf_height = frame_buf_height >> iplane->ydec;
      iplane->bitdepth = reference_bits;
      iplane->xstride = reference_bytes;
      iplane->ystride = plane_buf_width*reference_bytes;
      iplane->data = ref_img_data
       + (OD_BUFFER_PADDING >> iplane->xdec)*iplane->xstride
       + (OD_BUFFER_PADDING >> iplane->ydec)*iplane->ystride;
      ref_img_data += plane_buf_height*iplane->ystride;
    }
  }
  /*Mark all of the reference image buffers available.*/
  for (imgi = 0; imgi < nrefs; imgi++) state->ref_imgi[imgi] = -1;
  return OD_SUCCESS;
}

static int od_state_mvs_init(od_state *state) {
  state->mv_grid = (od_mv_grid_pt **)od_calloc_2d(state->nvmvbs + 1,
   state->nhmvbs + 1, sizeof(**state->mv_grid));
  if (OD_UNLIKELY(!state->mv_grid)) {
    return OD_EFAULT;
  }
  return OD_SUCCESS;
}

static void od_restore_fpu_c(void) {}

void od_restore_fpu(od_state *state) {
  (*state->opt_vtbl.restore_fpu)();
}

void od_state_opt_vtbl_init_c(od_state *state) {
  if (state->full_precision_references) {
    OD_ASSERT(0);
    /*Incoming:
      state->opt_vtbl.mc_predict1fmv = od_mc_predict1fmv16_c;
      state->opt_vtbl.mc_blend_full = od_mc_blend_full16_c;
      state->opt_vtbl.mc_blend_full_split = od_mc_blend_full_split16_c;
      state->opt_vtbl.mc_blend_multi = od_mc_blend_multi16_c;
      state->opt_vtbl.mc_blend_multi_split = od_mc_blend_multi_split16_c;*/
  }
  else {
    state->opt_vtbl.mc_predict1fmv = od_mc_predict1fmv8_c;
    state->opt_vtbl.mc_blend_full = od_mc_blend_full8_c;
    state->opt_vtbl.mc_blend_full_split = od_mc_blend_full_split8_c;
    state->opt_vtbl.mc_blend_multi = od_mc_blend_multi8_c;
    state->opt_vtbl.mc_blend_multi_split = od_mc_blend_multi_split8_c;
  }
  state->opt_vtbl.restore_fpu = od_restore_fpu_c;
{'fraction_non_alphanumeric': 0.11134662515731603, 'fraction_numerical': 0.016162151420812083, 'mean_word_length': 4.017613825191093, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 181, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4457201', 'n_tokens_mistral': 5682, 'n_tokens_neox': 5307, 'n_words': 1456}