Skip to content

Commit faff66e

Browse files
ojw28icbaker
authored andcommitted
Use &id_ as buffer_private_data for libgav1.
This avoids the issue of whether it is defined behaviour to cast an arbitrary int (or even intptr_t) value to a void* pointer. This is the original approach used before commit 0915998. PiperOrigin-RevId: 295552115
1 parent 4e4a87f commit faff66e

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

extensions/av1/src/main/jni/gav1_jni.cc

+17-17
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#endif // CPU_FEATURES_COMPILED_ANY_ARM_NEON
2828
#include <jni.h>
2929

30-
#include <cassert>
31-
#include <climits>
3230
#include <cstdint>
3331
#include <cstring>
3432
#include <mutex> // NOLINT
@@ -134,6 +132,12 @@ class JniFrameBuffer {
134132
}
135133
}
136134

135+
// Not copyable or movable.
136+
JniFrameBuffer(const JniFrameBuffer&) = delete;
137+
JniFrameBuffer(JniFrameBuffer&&) = delete;
138+
JniFrameBuffer& operator=(const JniFrameBuffer&) = delete;
139+
JniFrameBuffer& operator=(JniFrameBuffer&&) = delete;
140+
137141
void SetFrameData(const libgav1::DecoderBuffer& decoder_buffer) {
138142
for (int plane_index = kPlaneY; plane_index < decoder_buffer.NumPlanes();
139143
plane_index++) {
@@ -162,7 +166,7 @@ class JniFrameBuffer {
162166
bool InUse() const { return reference_count_ != 0; }
163167

164168
uint8_t* RawBuffer(int plane_index) const { return raw_buffer_[plane_index]; }
165-
int Id() const { return id_; }
169+
void* BufferPrivateData() const { return const_cast<int*>(&id_); }
166170

167171
// Attempts to reallocate data planes if the existing ones don't have enough
168172
// capacity. Returns true if the allocation was successful or wasn't needed,
@@ -359,19 +363,17 @@ int Libgav1GetFrameBuffer(void* callback_private_data, int bitdepth,
359363
uint8_t* const v_buffer =
360364
(info.uv_buffer_size != 0) ? jni_buffer->RawBuffer(2) : nullptr;
361365

362-
status = libgav1::SetFrameBuffer(&info, y_buffer, u_buffer, v_buffer,
363-
reinterpret_cast<void*>(jni_buffer->Id()),
364-
frame_buffer);
366+
status =
367+
libgav1::SetFrameBuffer(&info, y_buffer, u_buffer, v_buffer,
368+
jni_buffer->BufferPrivateData(), frame_buffer);
365369
return (status == kLibgav1StatusOk) ? 0 : -1;
366370
}
367371

368372
void Libgav1ReleaseFrameBuffer(void* callback_private_data,
369373
void* buffer_private_data) {
370374
JniContext* const context = static_cast<JniContext*>(callback_private_data);
371-
const intptr_t buffer_id = reinterpret_cast<intptr_t>(buffer_private_data);
372-
assert(buffer_id <= INT_MAX);
373-
context->jni_status_code =
374-
context->buffer_manager.ReleaseBuffer(static_cast<int>(buffer_id));
375+
const int buffer_id = *static_cast<const int*>(buffer_private_data);
376+
context->jni_status_code = context->buffer_manager.ReleaseBuffer(buffer_id);
375377
if (context->jni_status_code != kJniStatusOk) {
376378
LOGE("%s", GetJniErrorMessage(context->jni_status_code));
377379
}
@@ -655,12 +657,11 @@ DECODER_FUNC(jint, gav1GetFrame, jlong jContext, jobject jOutputBuffer,
655657
return kStatusError;
656658
}
657659

658-
const intptr_t buffer_id =
659-
reinterpret_cast<intptr_t>(decoder_buffer->buffer_private_data);
660-
assert(buffer_id <= INT_MAX);
661-
context->buffer_manager.AddBufferReference(static_cast<int>(buffer_id));
660+
const int buffer_id =
661+
*static_cast<const int*>(decoder_buffer->buffer_private_data);
662+
context->buffer_manager.AddBufferReference(buffer_id);
662663
JniFrameBuffer* const jni_buffer =
663-
context->buffer_manager.GetBuffer(static_cast<int>(buffer_id));
664+
context->buffer_manager.GetBuffer(buffer_id);
664665
jni_buffer->SetFrameData(*decoder_buffer);
665666
env->CallVoidMethod(jOutputBuffer, context->init_for_private_frame_method,
666667
decoder_buffer->displayed_width[kPlaneY],
@@ -669,8 +670,7 @@ DECODER_FUNC(jint, gav1GetFrame, jlong jContext, jobject jOutputBuffer,
669670
// Exception is thrown in Java when returning from the native call.
670671
return kStatusError;
671672
}
672-
env->SetIntField(jOutputBuffer, context->decoder_private_field,
673-
static_cast<int>(buffer_id));
673+
env->SetIntField(jOutputBuffer, context->decoder_private_field, buffer_id);
674674
}
675675

676676
return kStatusOk;

0 commit comments

Comments
 (0)