summaryrefslogtreecommitdiff
path: root/frame.c
blob: 2e008ed5c28d3b02c2a4ed8841802d2ac0f8de91 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (C) 2023 Mikhail Burakov. This file is part of receiver.
 *
 * receiver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * receiver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with receiver.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "frame.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "util.h"

struct Frame* FrameCreate(uint32_t width, uint32_t height, uint32_t fourcc,
                          size_t nplanes, const struct FramePlane* planes) {
  struct AUTO(Frame)* frame = malloc(sizeof(struct Frame));
  if (!frame) {
    LOG("Failed to allocate frame (%s)", strerror(errno));
    return NULL;
  }
  *frame = (struct Frame){
      .width = width,
      .height = height,
      .fourcc = fourcc,
      .nplanes = nplanes,
      .planes[0] = {.dmabuf_fd = -1, .pitch = 0, .offset = 0, .modifier = 0},
      .planes[1] = {.dmabuf_fd = -1, .pitch = 0, .offset = 0, .modifier = 0},
      .planes[2] = {.dmabuf_fd = -1, .pitch = 0, .offset = 0, .modifier = 0},
      .planes[3] = {.dmabuf_fd = -1, .pitch = 0, .offset = 0, .modifier = 0},
  };

  for (size_t i = 0; i < nplanes; i++) {
    frame->planes[i] = (struct FramePlane){
        .dmabuf_fd = dup(planes[i].dmabuf_fd),
        .pitch = planes[i].pitch,
        .offset = planes[i].offset,
        .modifier = planes[i].modifier,
    };
    if (frame->planes[i].dmabuf_fd == -1) {
      LOG("Failed to dup dmabuf fd (%s)", strerror(errno));
      return NULL;
    }
  }

  return RELEASE(frame);
}

void FrameDestroy(struct Frame** frame) {
  if (!frame || !*frame) return;
  for (size_t i = (*frame)->nplanes; i; i--) {
    if ((*frame)->planes[i].dmabuf_fd != -1)
      close((*frame)->planes[i].dmabuf_fd);
  }
}