-/* This function calculates vector differences using mean square
- * error (MSE). */
-
-static int encode_vector(SVQ1Context *s, unsigned char *vector,
- unsigned int level, int threshold)
-{
- int i, j, k;
- int mean;
- signed short work_vector[256];
- int best_codebook;
- int best_score;
- int multistage_codebooks[6];
- int number_of_stages = 0;
- int8_t *current_codebook;
- int mse;
- int diff;
- int ret;
-
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " ** recursive entry point: encoding level %d vector at threshold %d\n",
- level, threshold);
-#endif
- if (level > 5) {
- av_log(s->avctx, AV_LOG_INFO, " help! level %d > 5\n", level);
- return 0;
- }
-
-#ifdef DEBUG_SVQ1
-for (i = 0; i < level_sizes[level]; i++)
- av_log(s->avctx, AV_LOG_INFO, " %02X", vector[i]);
-av_log(s->avctx, AV_LOG_INFO, "\n");
-#endif
-
- /* calculate the mean */
- mean = 0;
- for (i = 0; i < level_sizes[level]; i++)
- mean += vector[i];
- mean >>= level_log2_sizes[level];
-
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " vector mean = 0x%02X\n", mean);
-#endif
-
- /* remove the mean from the vector and compute the resulting MSE */
- mse = 0;
- for (i = 0; i < level_sizes[level]; i++) {
- work_vector[i] = (signed short)vector[i] - mean;
- mse += (work_vector[i] * work_vector[i]);
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " %d", work_vector[i]);
-#endif
- }
- mse >>= level_log2_sizes[level];
-
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, "\n MSE = %d\n", mse);
-#endif
-
- if (mse < threshold) {
-
-#ifdef DEBUG_SVQ1
- av_log(s->avctx, AV_LOG_INFO, " mean-only encoding found for level %d vector, mean = %d\n",
- level, mean);
-#endif
-
- /* indicate that this is the end of the subdivisions */
- if (level > 0)
- put_bits(&s->pb, 1, 0);
-
- /* index 1 in the table indicates mean-only encoding */
- put_bits(&s->pb, svq1_intra_multistage_vlc[level][1][1],
- svq1_intra_multistage_vlc[level][1][0]);
- put_bits(&s->pb, svq1_intra_mean_vlc[mean][1],
- svq1_intra_mean_vlc[mean][0]);
-
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " mean-only L%d, VLC = (0x%X, %d), mean = %d (0x%X, %d)\n",
- level,
- svq1_intra_multistage_vlc[level][1 + number_of_stages][0],
- svq1_intra_multistage_vlc[level][1 + number_of_stages][1],
- mean,
- svq1_intra_mean_vlc[mean][0],
- svq1_intra_mean_vlc[mean][1]);
-#endif
-
- ret = 0;
-
- } else {
-
- if (level <= 3) {
-
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " multistage VQ search...\n");
-#endif
- /* conduct multistage VQ search, for each stage... */
- for (i = 0; i < 6; i++) {
-
- best_codebook = 0;
- best_score = 0x7FFFFFFF;
- /* for each codebook in stage */
- for (j = 0; j < 16; j++) {
-
- mse = 0;
- current_codebook =
- &svq1_intra_codebooks[level]
- [i * level_sizes[level] * 16 + j * level_sizes[level]];
- /* calculate the MSE for this vector */
- for (k = 0; k < level_sizes[level]; k++) {
- diff = work_vector[k] - current_codebook[k];
- mse += (diff * diff);
- }
- mse >>= level_log2_sizes[level];
-
- /* lowest score so far? */
- if (mse < best_score) {
- best_score = mse;
- best_codebook = j;
- }
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " after %d, %d, best codebook is %d with a score of %d (score was %d)\n",
- i, j, best_codebook, best_score, mse);
-#endif
- }
-
- /* apply the winning codebook to the work vector and check if
- * the vector meets the quality threshold */
- mse = 0;
- current_codebook =
- &svq1_intra_codebooks[level]
- [i * level_sizes[level] * 16 + j * level_sizes[level]];
- multistage_codebooks[number_of_stages++] = best_codebook;
- for (j = 0; j < level_sizes[level]; j++) {
- work_vector[j] = work_vector[j] - current_codebook[j];
- mse += (work_vector[j] * work_vector[j]);
- }
- mse >>= level_log2_sizes[level];
-
- /* do not go forward with the rest of the search if an acceptable
- * codebook combination has been found */
- if (mse < threshold)
- break;
- }
- }
-
- if ((mse < threshold) || (level == 0)) {
-#ifdef DEBUG_SVQ1
- av_log(s->avctx, AV_LOG_INFO, " level %d VQ encoding found using mean %d and codebooks", level, mean);
- for (i = 0; i < number_of_stages; i++)
- av_log(s->avctx, AV_LOG_INFO, " %d", multistage_codebooks[i]);
- av_log(s->avctx, AV_LOG_INFO, "\n");
-#endif
-
- /* indicate that this is the end of the subdivisions */
- if (level > 0)
- put_bits(&s->pb, 1, 0);
-
- /* output the encoding */
- put_bits(&s->pb,
- svq1_intra_multistage_vlc[level][1 + number_of_stages][1],
- svq1_intra_multistage_vlc[level][1 + number_of_stages][0]);
- put_bits(&s->pb, svq1_intra_mean_vlc[mean][1],
- svq1_intra_mean_vlc[mean][0]);
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, " L%d: multistage = %d (0x%X, %d), mean = %d (0x%X, %d), codebooks = ",
- level,
- number_of_stages,
- svq1_intra_multistage_vlc[level][1 + number_of_stages][0],
- svq1_intra_multistage_vlc[level][1 + number_of_stages][1],
- mean,
- svq1_intra_mean_vlc[mean][0],
- svq1_intra_mean_vlc[mean][1]);
-#endif
-
- for (i = 0; i < number_of_stages; i++)
-{
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, "%d ", multistage_codebooks[i]);
-#endif
- put_bits(&s->pb, 4, multistage_codebooks[i]);
-}
-#ifdef DEBUG_SVQ1
-av_log(s->avctx, AV_LOG_INFO, "\n");
-#endif
-
- ret = 0;
-
- } else {
-
- /* output a subdivision bit to the encoded stream and signal to
- * the calling function that this vector could not be
- * coded at the requested threshold and needs to be subdivided */
- put_bits(&s->pb, 1, 1);
- ret = 1;
- }
- }
-
- return ret;
-}