xref: /aosp_15_r20/external/libdav1d/tools/output/y4m2.c (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
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 
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     const unsigned fw = p->p.w;
81     const unsigned fh = p->p.h;
82     uint64_t aw = (uint64_t)fh * p->frame_hdr->render_width;
83     uint64_t ah = (uint64_t)fw * p->frame_hdr->render_height;
84     uint64_t gcd = ah;
85     for (uint64_t a = aw, b; (b = a % gcd); a = gcd, gcd = b);
86     aw /= gcd;
87     ah /= gcd;
88 
89     fprintf(c->f, "YUV4MPEG2 W%u H%u F%u:%u Ip A%"PRIu64":%"PRIu64" C%s\n",
90             fw, fh, c->fps[0], c->fps[1], aw, ah, ss_name);
91 
92     return 0;
93 }
94 
y4m2_write(Y4m2OutputContext * const c,Dav1dPicture * const p)95 static int y4m2_write(Y4m2OutputContext *const c, Dav1dPicture *const p) {
96     if (c->first) {
97         c->first = 0;
98         const int res = write_header(c, p);
99         if (res < 0) return res;
100     }
101     fprintf(c->f, "FRAME\n");
102 
103     uint8_t *ptr;
104     const int hbd = p->p.bpc > 8;
105 
106     ptr = p->data[0];
107     for (int y = 0; y < p->p.h; y++) {
108         if (fwrite(ptr, p->p.w << hbd, 1, c->f) != 1)
109             goto error;
110         ptr += p->stride[0];
111     }
112 
113     if (p->p.layout != DAV1D_PIXEL_LAYOUT_I400) {
114         // u/v
115         const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
116         const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
117         const int cw = (p->p.w + ss_hor) >> ss_hor;
118         const int ch = (p->p.h + ss_ver) >> ss_ver;
119         for (int pl = 1; pl <= 2; pl++) {
120             ptr = p->data[pl];
121             for (int y = 0; y < ch; y++) {
122                 if (fwrite(ptr, cw << hbd, 1, c->f) != 1)
123                     goto error;
124                 ptr += p->stride[1];
125             }
126         }
127     }
128 
129     dav1d_picture_unref(p);
130     return 0;
131 
132 error:
133     dav1d_picture_unref(p);
134     fprintf(stderr, "Failed to write frame data: %s\n", strerror(errno));
135     return -1;
136 }
137 
y4m2_close(Y4m2OutputContext * const c)138 static void y4m2_close(Y4m2OutputContext *const c) {
139     if (c->f != stdout)
140         fclose(c->f);
141 }
142 
143 const Muxer y4m2_muxer = {
144     .priv_data_size = sizeof(Y4m2OutputContext),
145     .name = "yuv4mpeg2",
146     .extension = "y4m",
147     .write_header = y4m2_open,
148     .write_picture = y4m2_write,
149     .write_trailer = y4m2_close,
150 };
151