Skip to content

Commit

Permalink
Fixed the packed copyouts to be more robust against weird formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Yahweasel committed Nov 9, 2023
1 parent 57a73dd commit 11dccc0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void AVFrame_sample_aspect_ratio_s(AVFrame *a, int n, int d) {

/* AVPixFmtDescriptor */
#define B(type, field) A(AVPixFmtDescriptor, type, field)
B(uint64_t, flags)
B(uint8_t, nb_components)
B(uint8_t, log2_chroma_h)
B(uint8_t, log2_chroma_w)
Expand Down
1 change: 1 addition & 0 deletions funcs.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
"width"
]],
["AVPixFmtDescriptor", [
"flags",
"log2_chroma_h",
"log2_chroma_w",
"nb_components"
Expand Down
30 changes: 25 additions & 5 deletions post.in.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,19 +1625,29 @@ var ff_copyout_frame_video_width = Module.ff_copyout_frame_video = function(fram
*/
/// @types ff_frame_video_packed_size@sync(frame: number): @promise@Frame@
var ff_frame_video_packed_size = Module.ff_frame_video_packed_size = function(frame) {
// FIXME: duplication
var width = AVFrame_width(frame);
var height = AVFrame_height(frame);
var format = AVFrame_format(frame);
var desc = av_pix_fmt_desc_get(format);

// VERY simple bpp, assuming all components are 8-bit
var bpp = 1;
if (!(AVPixFmtDescriptor_flags(desc) & 0x10) /* planar */)
bpp *= AVPixFmtDescriptor_nb_components(desc);

var dataSz = 0;
for (var i = 0; i < 8 /* AV_NUM_DATA_POINTERS */; i++) {
var linesize = AVFrame_linesize_a(frame, i);
if (!linesize)
break;
var w = width * bpp;
var h = height;
if (i === 1 || i === 2)
if (i === 1 || i === 2) {
w >>= AVPixFmtDescriptor_log2_chroma_w(desc);
h >>= AVPixFmtDescriptor_log2_chroma_h(desc);
dataSz += linesize * h;
}
dataSz += w * h;
}

return dataSz;
Expand All @@ -1646,26 +1656,36 @@ var ff_frame_video_packed_size = Module.ff_frame_video_packed_size = function(fr
/* Copy out just the packed data from this frame, into the given buffer. Used
* internally. */
var ff_copyout_frame_data_packed = Module.ff_copyout_frame_data_packed = function(data, frame) {
var width = AVFrame_width(frame);
var height = AVFrame_height(frame);
var format = AVFrame_format(frame);
var desc = av_pix_fmt_desc_get(format);

// VERY simple bpp, assuming all components are 8-bit
var bpp = 1;
if (!(AVPixFmtDescriptor_flags(desc) & 0x10) /* planar */)
bpp *= AVPixFmtDescriptor_nb_components(desc);

// Copy it out
var dIdx = 0;
for (var i = 0; i < 8 /* AV_NUM_DATA_POINTERS */; i++) {
var linesize = AVFrame_linesize_a(frame, i);
if (!linesize)
break;
var inData = AVFrame_data_a(frame, i);
var w = width * bpp;
var h = height;
if (i === 1 || i === 2)
if (i === 1 || i === 2) {
w >>= AVPixFmtDescriptor_log2_chroma_w(desc);
h >>= AVPixFmtDescriptor_log2_chroma_h(desc);
}
for (var y = 0; y < h; y++) {
var line = inData + y * linesize;
data.set(
Module.HEAPU8.subarray(inData + y * linesize, inData + (y + 1) * linesize),
Module.HEAPU8.subarray(line, line + w),
dIdx
);
dIdx += linesize;
dIdx += w;
}
}
};
Expand Down

0 comments on commit 11dccc0

Please sign in to comment.