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 <inttypes.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/stat.h>
36
37 #include "output/muxer.h"
38
39 typedef struct MuxerPriv {
40 FILE *f;
41 int first;
42 unsigned fps[2];
43 } Y4m2OutputContext;
44
y4m2_open(Y4m2OutputContext * const c,const char * const file,const Dav1dPictureParameters * p,const unsigned fps[2])45 static int y4m2_open(Y4m2OutputContext *const c, const char *const file,
46 const Dav1dPictureParameters *p, const unsigned fps[2])
47 {
48 if (!strcmp(file, "-")) {
49 c->f = stdout;
50 } else if (!(c->f = fopen(file, "wb"))) {
51 fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
52 return -1;
53 }
54
55 c->first = 1;
56 c->fps[0] = fps[0];
57 c->fps[1] = fps[1];
58
59 return 0;
60 }
61
write_header(Y4m2OutputContext * const c,const Dav1dPicture * const p)62 static int write_header(Y4m2OutputContext *const c, const Dav1dPicture *const p) {
63 static const char *const ss_names[][3] = {
64 [DAV1D_PIXEL_LAYOUT_I400] = { "mono", "mono10", "mono12" },
65 [DAV1D_PIXEL_LAYOUT_I420] = { NULL, "420p10", "420p12" },
66 [DAV1D_PIXEL_LAYOUT_I422] = { "422", "422p10", "422p12" },
67 [DAV1D_PIXEL_LAYOUT_I444] = { "444", "444p10", "444p12" }
68 };
69
70 static const char *const chr_names_8bpc_i420[] = {
71 [DAV1D_CHR_UNKNOWN] = "420jpeg",
72 [DAV1D_CHR_VERTICAL] = "420mpeg2",
73 [DAV1D_CHR_COLOCATED] = "420"
74 };
75
76 const char *const ss_name =
77 p->p.layout == DAV1D_PIXEL_LAYOUT_I420 && p->p.bpc == 8 ?
78 chr_names_8bpc_i420[p->seq_hdr->chr > 2 ? DAV1D_CHR_UNKNOWN : p->seq_hdr->chr] :
79 ss_names[p->p.layout][p->seq_hdr->hbd];
80
81 const unsigned fw = p->p.w;
82 const unsigned fh = p->p.h;
83 uint64_t aw = (uint64_t)fh * p->frame_hdr->render_width;
84 uint64_t ah = (uint64_t)fw * p->frame_hdr->render_height;
85 uint64_t gcd = ah;
86 for (uint64_t a = aw, b; (b = a % gcd); a = gcd, gcd = b);
87 aw /= gcd;
88 ah /= gcd;
89
90 fprintf(c->f, "YUV4MPEG2 W%u H%u F%u:%u Ip A%"PRIu64":%"PRIu64" C%s\n",
91 fw, fh, c->fps[0], c->fps[1], aw, ah, ss_name);
92
93 return 0;
94 }
95
y4m2_write(Y4m2OutputContext * const c,Dav1dPicture * const p)96 static int y4m2_write(Y4m2OutputContext *const c, Dav1dPicture *const p) {
97 if (c->first) {
98 c->first = 0;
99 const int res = write_header(c, p);
100 if (res < 0) return res;
101 }
102 fprintf(c->f, "FRAME\n");
103
104 uint8_t *ptr;
105 const int hbd = p->p.bpc > 8;
106
107 ptr = p->data[0];
108 for (int y = 0; y < p->p.h; y++) {
109 if (fwrite(ptr, p->p.w << hbd, 1, c->f) != 1)
110 goto error;
111 ptr += p->stride[0];
112 }
113
114 if (p->p.layout != DAV1D_PIXEL_LAYOUT_I400) {
115 // u/v
116 const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
117 const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
118 const int cw = (p->p.w + ss_hor) >> ss_hor;
119 const int ch = (p->p.h + ss_ver) >> ss_ver;
120 for (int pl = 1; pl <= 2; pl++) {
121 ptr = p->data[pl];
122 for (int y = 0; y < ch; y++) {
123 if (fwrite(ptr, cw << hbd, 1, c->f) != 1)
124 goto error;
125 ptr += p->stride[1];
126 }
127 }
128 }
129
130 dav1d_picture_unref(p);
131 return 0;
132
133 error:
134 dav1d_picture_unref(p);
135 fprintf(stderr, "Failed to write frame data: %s\n", strerror(errno));
136 return -1;
137 }
138
y4m2_close(Y4m2OutputContext * const c)139 static void y4m2_close(Y4m2OutputContext *const c) {
140 if (c->f != stdout)
141 fclose(c->f);
142 }
143
144 const Muxer y4m2_muxer = {
145 .priv_data_size = sizeof(Y4m2OutputContext),
146 .name = "yuv4mpeg2",
147 .extension = "y4m",
148 .write_header = y4m2_open,
149 .write_picture = y4m2_write,
150 .write_trailer = y4m2_close,
151 };
152