ConvertToARGB: compute buffer offsets in ptrdiff_t

Also validate crop_x, crop_y, crop_width, crop_height and make sure the
crop region stays inside the source rectangle.

Change-Id: I68748e14b21307b262d8b283147bce5ace8108d2
This commit is contained in:
Wan-Teh Chang 2026-06-05 15:34:35 -07:00
parent ccd415101d
commit d7512da57b

View File

@ -11,6 +11,7 @@
#include "libyuv/convert_argb.h" #include "libyuv/convert_argb.h"
#include <limits.h> #include <limits.h>
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -50,10 +51,19 @@ int ConvertToARGB(const uint8_t* sample,
int crop_height, int crop_height,
enum RotationMode rotation, enum RotationMode rotation,
uint32_t fourcc) { uint32_t fourcc) {
if (src_height == INT_MIN || crop_height == INT_MIN) {
return -1;
}
int abs_src_height = (src_height < 0) ? -src_height : src_height;
int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height;
if (dst_argb == NULL || sample == NULL || src_width <= 0 || if (dst_argb == NULL || sample == NULL || src_width <= 0 ||
src_width > INT_MAX / 4 || crop_width <= 0 || crop_width > INT_MAX / 4 || src_width > INT_MAX / 4 || crop_width <= 0 || crop_width > INT_MAX / 4 ||
src_height == 0 || src_height == INT_MIN || crop_height == 0 || src_height == 0 || crop_height == 0 || crop_x < 0 || crop_y < 0 ||
crop_height == INT_MIN) { crop_width > src_width || crop_x > src_width - crop_width ||
abs_crop_height > abs_src_height ||
crop_y > abs_src_height - abs_crop_height) {
return -1; return -1;
} }
@ -61,8 +71,6 @@ int ConvertToARGB(const uint8_t* sample,
int aligned_src_width = (src_width + 1) & ~1; int aligned_src_width = (src_width + 1) & ~1;
const uint8_t* src; const uint8_t* src;
const uint8_t* src_uv; const uint8_t* src_uv;
int abs_src_height = (src_height < 0) ? -src_height : src_height;
int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height;
int r = 0; int r = 0;
// One pass rotation is available for some formats. For the rest, convert // One pass rotation is available for some formats. For the rest, convert
@ -75,7 +83,7 @@ int ConvertToARGB(const uint8_t* sample,
uint8_t* dest_argb = dst_argb; uint8_t* dest_argb = dst_argb;
int dest_dst_stride_argb = dst_stride_argb; int dest_dst_stride_argb = dst_stride_argb;
uint8_t* rotate_buffer = NULL; uint8_t* rotate_buffer = NULL;
int abs_crop_height = (crop_height < 0) ? -crop_height : crop_height; int inv_crop_height = (crop_height < 0) ? -crop_height : crop_height;
if (src_height < 0) { if (src_height < 0) {
inv_crop_height = -inv_crop_height; inv_crop_height = -inv_crop_height;
@ -98,95 +106,97 @@ int ConvertToARGB(const uint8_t* sample,
switch (format) { switch (format) {
// Single plane formats // Single plane formats
case FOURCC_YUY2: case FOURCC_YUY2:
src = sample + (aligned_src_width * crop_y + crop_x) * 2; src = sample + ((ptrdiff_t)aligned_src_width * crop_y + crop_x) * 2;
r = YUY2ToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb, r = YUY2ToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb,
crop_width, inv_crop_height); crop_width, inv_crop_height);
break; break;
case FOURCC_UYVY: case FOURCC_UYVY:
src = sample + (aligned_src_width * crop_y + crop_x) * 2; src = sample + ((ptrdiff_t)aligned_src_width * crop_y + crop_x) * 2;
r = UYVYToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb, r = UYVYToARGB(src, aligned_src_width * 2, dst_argb, dst_stride_argb,
crop_width, inv_crop_height); crop_width, inv_crop_height);
break; break;
case FOURCC_24BG: case FOURCC_24BG:
src = sample + (src_width * crop_y + crop_x) * 3; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 3;
r = RGB24ToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width, r = RGB24ToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_RAW: case FOURCC_RAW:
src = sample + (src_width * crop_y + crop_x) * 3; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 3;
r = RAWToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width, r = RAWToARGB(src, src_width * 3, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_ARGB: case FOURCC_ARGB:
if (!need_buf && !rotation) { if (!need_buf && !rotation) {
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = ARGBToARGB(src, src_width * 4, dst_argb, dst_stride_argb, r = ARGBToARGB(src, src_width * 4, dst_argb, dst_stride_argb,
crop_width, inv_crop_height); crop_width, inv_crop_height);
} }
break; break;
case FOURCC_BGRA: case FOURCC_BGRA:
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = BGRAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width, r = BGRAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_ABGR: case FOURCC_ABGR:
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = ABGRToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width, r = ABGRToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_RGBA: case FOURCC_RGBA:
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = RGBAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width, r = RGBAToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_AR30: case FOURCC_AR30:
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = AR30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width, r = AR30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_AB30: case FOURCC_AB30:
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = AB30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width, r = AB30ToARGB(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_RGBP: case FOURCC_RGBP:
src = sample + (src_width * crop_y + crop_x) * 2; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 2;
r = RGB565ToARGB(src, src_width * 2, dst_argb, dst_stride_argb, r = RGB565ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
crop_width, inv_crop_height); crop_width, inv_crop_height);
break; break;
case FOURCC_RGBO: case FOURCC_RGBO:
src = sample + (src_width * crop_y + crop_x) * 2; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 2;
r = ARGB1555ToARGB(src, src_width * 2, dst_argb, dst_stride_argb, r = ARGB1555ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
crop_width, inv_crop_height); crop_width, inv_crop_height);
break; break;
case FOURCC_R444: case FOURCC_R444:
src = sample + (src_width * crop_y + crop_x) * 2; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 2;
r = ARGB4444ToARGB(src, src_width * 2, dst_argb, dst_stride_argb, r = ARGB4444ToARGB(src, src_width * 2, dst_argb, dst_stride_argb,
crop_width, inv_crop_height); crop_width, inv_crop_height);
break; break;
case FOURCC_I400: case FOURCC_I400:
src = sample + src_width * crop_y + crop_x; src = sample + (ptrdiff_t)src_width * crop_y + crop_x;
r = I400ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width, r = I400ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
case FOURCC_J400: case FOURCC_J400:
src = sample + src_width * crop_y + crop_x; src = sample + (ptrdiff_t)src_width * crop_y + crop_x;
r = J400ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width, r = J400ToARGB(src, src_width, dst_argb, dst_stride_argb, crop_width,
inv_crop_height); inv_crop_height);
break; break;
// Biplanar formats // Biplanar formats
case FOURCC_NV12: case FOURCC_NV12:
src = sample + (src_width * crop_y + crop_x); src = sample + ((ptrdiff_t)src_width * crop_y + crop_x);
src_uv = src_uv = sample +
sample + aligned_src_width * (abs_src_height + crop_y / 2) + crop_x; aligned_src_width * ((ptrdiff_t)abs_src_height + crop_y / 2) +
crop_x;
r = NV12ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb, r = NV12ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb,
dst_stride_argb, crop_width, inv_crop_height); dst_stride_argb, crop_width, inv_crop_height);
break; break;
case FOURCC_NV21: case FOURCC_NV21:
src = sample + (src_width * crop_y + crop_x); src = sample + ((ptrdiff_t)src_width * crop_y + crop_x);
src_uv = src_uv = sample +
sample + aligned_src_width * (abs_src_height + crop_y / 2) + crop_x; aligned_src_width * ((ptrdiff_t)abs_src_height + crop_y / 2) +
crop_x;
// Call NV12 but with u and v parameters swapped. // Call NV12 but with u and v parameters swapped.
r = NV21ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb, r = NV21ToARGB(src, src_width, src_uv, aligned_src_width, dst_argb,
dst_stride_argb, crop_width, inv_crop_height); dst_stride_argb, crop_width, inv_crop_height);
@ -194,21 +204,21 @@ int ConvertToARGB(const uint8_t* sample,
// Triplanar formats // Triplanar formats
case FOURCC_I420: case FOURCC_I420:
case FOURCC_YV12: { case FOURCC_YV12: {
const uint8_t* src_y = sample + (src_width * crop_y + crop_x); const uint8_t* src_y = sample + ((ptrdiff_t)src_width * crop_y + crop_x);
const uint8_t* src_u; const uint8_t* src_u;
const uint8_t* src_v; const uint8_t* src_v;
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
int halfheight = (abs_src_height + 1) / 2; int halfheight = (abs_src_height + 1) / 2;
if (format == FOURCC_YV12) { if (format == FOURCC_YV12) {
src_v = sample + src_width * abs_src_height + src_v = sample + (ptrdiff_t)src_width * abs_src_height +
(halfwidth * crop_y + crop_x) / 2; ((ptrdiff_t)halfwidth * crop_y + crop_x) / 2;
src_u = sample + src_width * abs_src_height + src_u = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (halfheight + crop_y / 2) + crop_x / 2; halfwidth * ((ptrdiff_t)halfheight + crop_y / 2) + crop_x / 2;
} else { } else {
src_u = sample + src_width * abs_src_height + src_u = sample + (ptrdiff_t)src_width * abs_src_height +
(halfwidth * crop_y + crop_x) / 2; ((ptrdiff_t)halfwidth * crop_y + crop_x) / 2;
src_v = sample + src_width * abs_src_height + src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (halfheight + crop_y / 2) + crop_x / 2; halfwidth * ((ptrdiff_t)halfheight + crop_y / 2) + crop_x / 2;
} }
r = I420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = I420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
@ -218,11 +228,12 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_J420: { case FOURCC_J420: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
int halfheight = (abs_src_height + 1) / 2; int halfheight = (abs_src_height + 1) / 2;
const uint8_t* src_y = sample + (src_width * crop_y + crop_x); const uint8_t* src_y = sample + ((ptrdiff_t)src_width * crop_y + crop_x);
const uint8_t* src_u = sample + src_width * abs_src_height + const uint8_t* src_u = sample + (ptrdiff_t)src_width * abs_src_height +
(halfwidth * crop_y + crop_x) / 2; ((ptrdiff_t)halfwidth * crop_y + crop_x) / 2;
const uint8_t* src_v = sample + src_width * abs_src_height + const uint8_t* src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (halfheight + crop_y / 2) + crop_x / 2; halfwidth * ((ptrdiff_t)halfheight + crop_y / 2) +
crop_x / 2;
r = J420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = J420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -231,11 +242,12 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_H420: { case FOURCC_H420: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
int halfheight = (abs_src_height + 1) / 2; int halfheight = (abs_src_height + 1) / 2;
const uint8_t* src_y = sample + (src_width * crop_y + crop_x); const uint8_t* src_y = sample + ((ptrdiff_t)src_width * crop_y + crop_x);
const uint8_t* src_u = sample + src_width * abs_src_height + const uint8_t* src_u = sample + (ptrdiff_t)src_width * abs_src_height +
(halfwidth * crop_y + crop_x) / 2; ((ptrdiff_t)halfwidth * crop_y + crop_x) / 2;
const uint8_t* src_v = sample + src_width * abs_src_height + const uint8_t* src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (halfheight + crop_y / 2) + crop_x / 2; halfwidth * ((ptrdiff_t)halfheight + crop_y / 2) +
crop_x / 2;
r = H420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = H420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -244,11 +256,12 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_U420: { case FOURCC_U420: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
int halfheight = (abs_src_height + 1) / 2; int halfheight = (abs_src_height + 1) / 2;
const uint8_t* src_y = sample + (src_width * crop_y + crop_x); const uint8_t* src_y = sample + ((ptrdiff_t)src_width * crop_y + crop_x);
const uint8_t* src_u = sample + src_width * abs_src_height + const uint8_t* src_u = sample + (ptrdiff_t)src_width * abs_src_height +
(halfwidth * crop_y + crop_x) / 2; ((ptrdiff_t)halfwidth * crop_y + crop_x) / 2;
const uint8_t* src_v = sample + src_width * abs_src_height + const uint8_t* src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (halfheight + crop_y / 2) + crop_x / 2; halfwidth * ((ptrdiff_t)halfheight + crop_y / 2) +
crop_x / 2;
r = U420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = U420ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -257,19 +270,19 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_I422: case FOURCC_I422:
case FOURCC_YV16: { case FOURCC_YV16: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u; const uint8_t* src_u;
const uint8_t* src_v; const uint8_t* src_v;
if (format == FOURCC_YV16) { if (format == FOURCC_YV16) {
src_v = sample + src_width * abs_src_height + halfwidth * crop_y + src_v = sample + (ptrdiff_t)src_width * abs_src_height +
crop_x / 2; (ptrdiff_t)halfwidth * crop_y + crop_x / 2;
src_u = sample + src_width * abs_src_height + src_u = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (abs_src_height + crop_y) + crop_x / 2; halfwidth * ((ptrdiff_t)abs_src_height + crop_y) + crop_x / 2;
} else { } else {
src_u = sample + src_width * abs_src_height + halfwidth * crop_y + src_u = sample + (ptrdiff_t)src_width * abs_src_height +
crop_x / 2; (ptrdiff_t)halfwidth * crop_y + crop_x / 2;
src_v = sample + src_width * abs_src_height + src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (abs_src_height + crop_y) + crop_x / 2; halfwidth * ((ptrdiff_t)abs_src_height + crop_y) + crop_x / 2;
} }
r = I422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = I422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
@ -278,11 +291,12 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_J422: { case FOURCC_J422: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u = const uint8_t* src_u = sample + (ptrdiff_t)src_width * abs_src_height +
sample + src_width * abs_src_height + halfwidth * crop_y + crop_x / 2; (ptrdiff_t)halfwidth * crop_y + crop_x / 2;
const uint8_t* src_v = sample + src_width * abs_src_height + const uint8_t* src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (abs_src_height + crop_y) + crop_x / 2; halfwidth * ((ptrdiff_t)abs_src_height + crop_y) +
crop_x / 2;
r = J422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = J422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -290,11 +304,12 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_H422: { case FOURCC_H422: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u = const uint8_t* src_u = sample + (ptrdiff_t)src_width * abs_src_height +
sample + src_width * abs_src_height + halfwidth * crop_y + crop_x / 2; (ptrdiff_t)halfwidth * crop_y + crop_x / 2;
const uint8_t* src_v = sample + src_width * abs_src_height + const uint8_t* src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (abs_src_height + crop_y) + crop_x / 2; halfwidth * ((ptrdiff_t)abs_src_height + crop_y) +
crop_x / 2;
r = H422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = H422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -302,11 +317,12 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_U422: { case FOURCC_U422: {
int halfwidth = (src_width + 1) / 2; int halfwidth = (src_width + 1) / 2;
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u = const uint8_t* src_u = sample + (ptrdiff_t)src_width * abs_src_height +
sample + src_width * abs_src_height + halfwidth * crop_y + crop_x / 2; (ptrdiff_t)halfwidth * crop_y + crop_x / 2;
const uint8_t* src_v = sample + src_width * abs_src_height + const uint8_t* src_v = sample + (ptrdiff_t)src_width * abs_src_height +
halfwidth * (abs_src_height + crop_y) + crop_x / 2; halfwidth * ((ptrdiff_t)abs_src_height + crop_y) +
crop_x / 2;
r = H422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth, r = H422ToARGB(src_y, src_width, src_u, halfwidth, src_v, halfwidth,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -314,15 +330,19 @@ int ConvertToARGB(const uint8_t* sample,
case FOURCC_I444: case FOURCC_I444:
case FOURCC_YV24: { case FOURCC_YV24: {
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u; const uint8_t* src_u;
const uint8_t* src_v; const uint8_t* src_v;
if (format == FOURCC_YV24) { if (format == FOURCC_YV24) {
src_v = sample + src_width * (abs_src_height + crop_y) + crop_x; src_v =
src_u = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; sample + src_width * ((ptrdiff_t)abs_src_height + crop_y) + crop_x;
src_u = sample + src_width * ((ptrdiff_t)abs_src_height * 2 + crop_y) +
crop_x;
} else { } else {
src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; src_u =
src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; sample + src_width * ((ptrdiff_t)abs_src_height + crop_y) + crop_x;
src_v = sample + src_width * ((ptrdiff_t)abs_src_height * 2 + crop_y) +
crop_x;
} }
r = I444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width, r = I444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
@ -330,33 +350,36 @@ int ConvertToARGB(const uint8_t* sample,
} }
case FOURCC_J444: { case FOURCC_J444: {
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u; const uint8_t* src_u =
const uint8_t* src_v; sample + src_width * ((ptrdiff_t)abs_src_height + crop_y) + crop_x;
src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; const uint8_t* src_v =
src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; sample + src_width * ((ptrdiff_t)abs_src_height * 2 + crop_y) +
crop_x;
r = J444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width, r = J444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
} }
case FOURCC_H444: { case FOURCC_H444: {
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u; const uint8_t* src_u =
const uint8_t* src_v; sample + src_width * ((ptrdiff_t)abs_src_height + crop_y) + crop_x;
src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; const uint8_t* src_v =
src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; sample + src_width * ((ptrdiff_t)abs_src_height * 2 + crop_y) +
crop_x;
r = H444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width, r = H444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
} }
case FOURCC_U444: { case FOURCC_U444: {
const uint8_t* src_y = sample + src_width * crop_y + crop_x; const uint8_t* src_y = sample + (ptrdiff_t)src_width * crop_y + crop_x;
const uint8_t* src_u; const uint8_t* src_u =
const uint8_t* src_v; sample + src_width * ((ptrdiff_t)abs_src_height + crop_y) + crop_x;
src_u = sample + src_width * (abs_src_height + crop_y) + crop_x; const uint8_t* src_v =
src_v = sample + src_width * (abs_src_height * 2 + crop_y) + crop_x; sample + src_width * ((ptrdiff_t)abs_src_height * 2 + crop_y) +
crop_x;
r = U444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width, r = U444ToARGB(src_y, src_width, src_u, src_width, src_v, src_width,
dst_argb, dst_stride_argb, crop_width, inv_crop_height); dst_argb, dst_stride_argb, crop_width, inv_crop_height);
break; break;
@ -379,7 +402,7 @@ int ConvertToARGB(const uint8_t* sample,
} }
free(rotate_buffer); free(rotate_buffer);
} else if (rotation) { } else if (rotation) {
src = sample + (src_width * crop_y + crop_x) * 4; src = sample + ((ptrdiff_t)src_width * crop_y + crop_x) * 4;
r = ARGBRotate(src, src_width * 4, dst_argb, dst_stride_argb, crop_width, r = ARGBRotate(src, src_width * 4, dst_argb, dst_stride_argb, crop_width,
inv_crop_height, rotation); inv_crop_height, rotation);
} }