-
Notifications
You must be signed in to change notification settings - Fork 4
/
m420.c
39 lines (31 loc) · 1.13 KB
/
m420.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Copyright 2015, Anton Leontiev <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0
*/
#include <stdint.h>
#include <error.h>
#include <libavformat/avformat.h>
void yuv420_to_m420(AVFrame *frame) {
unsigned const linesize = frame->linesize[0], height = frame->height;
uint8_t *temp = malloc(linesize * height * 3 / 2);
if (!temp) error(EXIT_FAILURE, 0, "Can not allocate memory for convertion buffer");
// Luma
for (size_t i = 0, j = 0; i < height; i += 2, j += 3) {
memcpy(temp + j * linesize, &frame->data[0][i * linesize], 2 * linesize);
}
// Chroma
for (size_t i = 0, j = 2; i < height / 2; i++, j += 3) {
uint8_t *const out = &temp[j * linesize];
uint8_t *const incb = &frame->data[1][i * linesize / 2];
uint8_t *const incr = &frame->data[2][i * linesize / 2];
for (size_t k = 0; k < linesize / 2; k++) {
out[2 * k] = incb[k];
out[2 * k + 1] = incr[k];
}
}
memcpy(frame->data[0], temp, linesize * height);
memcpy(frame->data[1], temp + linesize * height, linesize * height / 4);
memcpy(frame->data[2], temp + linesize * height + linesize * height / 4, linesize * height / 4);
free(temp);
}