1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/stat.h>
35
36 #include "output/muxer.h"
37
38 typedef struct MuxerPriv {
39 FILE *f;
40 int first;
41 unsigned fps[2];
42 } Y4m2OutputContext;
43
y4m2_open(Y4m2OutputContext * const c,const char * const file,const Dav1dPictureParameters * p,const unsigned fps[2])44 static int y4m2_open(Y4m2OutputContext *const c, const char *const file,
45 const Dav1dPictureParameters *p, const unsigned fps[2])
46 {
47 if (!strcmp(file, "-")) {
48 c->f = stdout;
49 } else if (!(c->f = fopen(file, "wb"))) {
50 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
51 return -1;
52 }
53
54 c->first = 1;
55 c->fps[0] = fps[0];
56 c->fps[1] = fps[1];
57
58 return 0;
59 }
60
write_header(Y4m2OutputContext * const c,const Dav1dPicture * const p)61 static int write_header(Y4m2OutputContext *const c, const Dav1dPicture *const p) {
62 static const char *const ss_names[][3] = {
63 [DAV1D_PIXEL_LAYOUT_I400] = { "mono", "mono10", "mono12" },
64 [DAV1D_PIXEL_LAYOUT_I420] = { NULL, "420p10", "420p12" },
65 [DAV1D_PIXEL_LAYOUT_I422] = { "422", "422p10", "422p12" },
66 [DAV1D_PIXEL_LAYOUT_I444] = { "444", "444p10", "444p12" }
67 };
68
69 static const char *const chr_names_8bpc_i420[] = {
70 [DAV1D_CHR_UNKNOWN] = "420jpeg",
71 [DAV1D_CHR_VERTICAL] = "420mpeg2",
72 [DAV1D_CHR_COLOCATED] = "420"
73 };
74
75 const char *const ss_name =
76 p->p.layout == DAV1D_PIXEL_LAYOUT_I420 && p->p.bpc == 8 ?
77 chr_names_8bpc_i420[p->seq_hdr->chr > 2 ? DAV1D_CHR_UNKNOWN : p->seq_hdr->chr] :
78 ss_names[p->p.layout][p->seq_hdr->hbd];
79
80 fprintf(c->f, "YUV4MPEG2 W%d H%d F%d:%d Ip C%s\n",
81 p->p.w, p->p.h, c->fps[0], c->fps[1], ss_name);
82
83 return 0;
84 }
85
y4m2_write(Y4m2OutputContext * const c,Dav1dPicture * const p)86 static int y4m2_write(Y4m2OutputContext *const c, Dav1dPicture *const p) {
87 if (c->first) {
88 c->first = 0;
89 const int res = write_header(c, p);
90 if (res < 0) return res;
91 }
92 fprintf(c->f, "FRAME\n");
93
94 uint8_t *ptr;
95 const int hbd = p->p.bpc > 8;
96
97 ptr = p->data[0];
98 for (int y = 0; y < p->p.h; y++) {
99 if (fwrite(ptr, p->p.w << hbd, 1, c->f) != 1)
100 goto error;
101 ptr += p->stride[0];
102 }
103
104 if (p->p.layout != DAV1D_PIXEL_LAYOUT_I400) {
105 // u/v
106 const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
107 const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
108 const int cw = (p->p.w + ss_hor) >> ss_hor;
109 const int ch = (p->p.h + ss_ver) >> ss_ver;
110 for (int pl = 1; pl <= 2; pl++) {
111 ptr = p->data[pl];
112 for (int y = 0; y < ch; y++) {
113 if (fwrite(ptr, cw << hbd, 1, c->f) != 1)
114 goto error;
115 ptr += p->stride[1];
116 }
117 }
118 }
119
120 dav1d_picture_unref(p);
121 return 0;
122
123 error:
124 dav1d_picture_unref(p);
125 fprintf(stderr, "Failed to write frame data: %s\n", strerror(errno));
126 return -1;
127 }
128
y4m2_close(Y4m2OutputContext * const c)129 static void y4m2_close(Y4m2OutputContext *const c) {
130 if (c->f != stdout)
131 fclose(c->f);
132 }
133
134 const Muxer y4m2_muxer = {
135 .priv_data_size = sizeof(Y4m2OutputContext),
136 .name = "yuv4mpeg2",
137 .extension = "y4m",
138 .write_header = y4m2_open,
139 .write_picture = y4m2_write,
140 .write_trailer = y4m2_close,
141 };
142