| 90 | } |
| 91 | |
| 92 | avifResult avifSampleTransformRecipeToExpression(avifSampleTransformRecipe recipe, avifSampleTransformExpression * expression) |
| 93 | { |
| 94 | // Postfix (or Reverse Polish) notation. Brackets to highlight sub-expressions. |
| 95 | |
| 96 | if (recipe == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_8B_8B) { |
| 97 | // reference_count is two: two 8-bit input images. |
| 98 | // (base_sample << 8) | hidden_sample |
| 99 | // Note: base_sample is encoded losslessly. hidden_sample is encoded lossily or losslessly. |
| 100 | AVIF_CHECKERR(avifArrayCreate(expression, sizeof(avifSampleTransformToken), 5), AVIF_RESULT_OUT_OF_MEMORY); |
| 101 | |
| 102 | { |
| 103 | // The base image represents the 8 most significant bits of the reconstructed, bit-depth-extended output image. |
| 104 | // Left shift the base image (which is also the primary item, or the auxiliary alpha item of the primary item) |
| 105 | // by 8 bits. This is equivalent to multiplying by 2^8. |
| 106 | AVIF_ASSERT_OR_RETURN(avifPushConstant(expression, 256)); |
| 107 | AVIF_ASSERT_OR_RETURN(avifPushInputImageItem(expression, 1)); |
| 108 | AVIF_ASSERT_OR_RETURN(avifPushOperator(expression, AVIF_SAMPLE_TRANSFORM_PRODUCT)); |
| 109 | } |
| 110 | { |
| 111 | // The second image represents the 8 least significant bits of the reconstructed, bit-depth-extended output image. |
| 112 | AVIF_ASSERT_OR_RETURN(avifPushInputImageItem(expression, 2)); |
| 113 | } |
| 114 | AVIF_ASSERT_OR_RETURN(avifPushOperator(expression, AVIF_SAMPLE_TRANSFORM_OR)); |
| 115 | return AVIF_RESULT_OK; |
| 116 | } |
| 117 | |
| 118 | if (recipe == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_4B) { |
| 119 | // reference_count is two: one 12-bit input image and one 8-bit input image (because AV1 does not support 4-bit samples). |
| 120 | // (base_sample << 4) | (hidden_sample >> 4) |
| 121 | // Note: base_sample is encoded losslessly. hidden_sample is encoded lossily or losslessly. |
| 122 | AVIF_CHECKERR(avifArrayCreate(expression, sizeof(avifSampleTransformToken), 7), AVIF_RESULT_OUT_OF_MEMORY); |
| 123 | |
| 124 | { |
| 125 | // The base image represents the 12 most significant bits of the reconstructed, bit-depth-extended output image. |
| 126 | // Left shift the base image (which is also the primary item, or the auxiliary alpha item of the primary item) |
| 127 | // by 4 bits. This is equivalent to multiplying by 2^4. |
| 128 | AVIF_ASSERT_OR_RETURN(avifPushConstant(expression, 16)); |
| 129 | AVIF_ASSERT_OR_RETURN(avifPushInputImageItem(expression, 1)); |
| 130 | AVIF_ASSERT_OR_RETURN(avifPushOperator(expression, AVIF_SAMPLE_TRANSFORM_PRODUCT)); |
| 131 | } |
| 132 | { |
| 133 | // The second image represents the 4 least significant bits of the reconstructed, bit-depth-extended output image. |
| 134 | AVIF_ASSERT_OR_RETURN(avifPushInputImageItem(expression, 2)); |
| 135 | AVIF_ASSERT_OR_RETURN(avifPushConstant(expression, 16)); |
| 136 | AVIF_ASSERT_OR_RETURN(avifPushOperator(expression, AVIF_SAMPLE_TRANSFORM_QUOTIENT)); |
| 137 | } |
| 138 | AVIF_ASSERT_OR_RETURN(avifPushOperator(expression, AVIF_SAMPLE_TRANSFORM_SUM)); |
| 139 | return AVIF_RESULT_OK; |
| 140 | } |
| 141 | |
| 142 | if (recipe == AVIF_SAMPLE_TRANSFORM_BIT_DEPTH_EXTENSION_12B_8B_OVERLAP_4B) { |
| 143 | // reference_count is two: one 12-bit input image and one 8-bit input image. |
| 144 | // (base_sample << 4) + hidden_sample |
| 145 | // Note: Both base_sample and hidden_sample are encoded lossily or losslessly. hidden_sample overlaps |
| 146 | // with base_sample by 4 bits to alleviate the loss caused by the quantization of base_sample. |
| 147 | AVIF_CHECKERR(avifArrayCreate(expression, sizeof(avifSampleTransformToken), 7), AVIF_RESULT_OUT_OF_MEMORY); |
| 148 | |
| 149 | // The base image represents the 12 most significant bits of the reconstructed, bit-depth-extended output image. |