| 1 | #include <iostream>
|
|---|
| 2 |
|
|---|
| 3 | extern "C" {
|
|---|
| 4 | #include <libswscale/swscale.h>
|
|---|
| 5 | }
|
|---|
| 6 |
|
|---|
| 7 | int main() {
|
|---|
| 8 | constexpr int width = 1080;
|
|---|
| 9 | constexpr int height = 1920;
|
|---|
| 10 | constexpr AVPixelFormat pix_fmt = AV_PIX_FMT_YUVJ420P;
|
|---|
| 11 | constexpr int initial_value = 123;
|
|---|
| 12 |
|
|---|
| 13 | AVFrame* frame = av_frame_alloc();
|
|---|
| 14 | if (!frame) {
|
|---|
| 15 | return 1;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | frame->width = width;
|
|---|
| 19 | frame->height = height;
|
|---|
| 20 | frame->format = pix_fmt;
|
|---|
| 21 | int ret = av_frame_get_buffer(frame, 0);
|
|---|
| 22 |
|
|---|
| 23 | uint8_t *output_buffer = new uint8_t[width * height * 3];// + 8];
|
|---|
| 24 | // for (int i=0; i<8; i++) {
|
|---|
| 25 | // output_buffer[width * height * 3 + i] = initial_value;
|
|---|
| 26 | // }
|
|---|
| 27 | const int output_stride[] = { width * 3 };
|
|---|
| 28 |
|
|---|
| 29 | SwsContext *sws_ctx_ = sws_getContext(width,
|
|---|
| 30 | height,
|
|---|
| 31 | pix_fmt,
|
|---|
| 32 | width,
|
|---|
| 33 | height,
|
|---|
| 34 | AV_PIX_FMT_BGR24,
|
|---|
| 35 | 0,
|
|---|
| 36 | nullptr,
|
|---|
| 37 | nullptr,
|
|---|
| 38 | nullptr);
|
|---|
| 39 |
|
|---|
| 40 | sws_scale(sws_ctx_, frame->data, frame->linesize, 0, height, &output_buffer, output_stride);
|
|---|
| 41 |
|
|---|
| 42 | // checking if memory was modified beyond buffer
|
|---|
| 43 | // for (int i=0; i<8; i++) {
|
|---|
| 44 | // if (output_buffer[width * height * 3 + i] != initial_value) {
|
|---|
| 45 | // std::cerr << "Memory was modified " << i << " bytes beyond buffer: " << (int)output_buffer[width * height * 3 + i] << std::endl;
|
|---|
| 46 | // }
|
|---|
| 47 | // }
|
|---|
| 48 |
|
|---|
| 49 | delete[] output_buffer;
|
|---|
| 50 | av_frame_free(&frame);
|
|---|
| 51 | sws_freeContext(sws_ctx_);
|
|---|
| 52 | }
|
|---|