xref: /aosp_15_r20/external/libdav1d/tools/output/yuv.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 <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "output/muxer.h"
36 
37 typedef struct MuxerPriv {
38     FILE *f;
39 } YuvOutputContext;
40 
yuv_open(YuvOutputContext * const c,const char * const file,const Dav1dPictureParameters * const p,const unsigned fps[2])41 static int yuv_open(YuvOutputContext *const c, const char *const file,
42                     const Dav1dPictureParameters *const p,
43                     const unsigned fps[2])
44 {
45     if (!strcmp(file, "-")) {
46         c->f = stdout;
47     } else if (!(c->f = fopen(file, "wb"))) {
48         fprintf(stderr, "Failed to open %s: %s\n", file, strerror(errno));
49         return -1;
50     }
51 
52     return 0;
53 }
54 
yuv_write(YuvOutputContext * const c,Dav1dPicture * const p)55 static int yuv_write(YuvOutputContext *const c, Dav1dPicture *const p) {
56     uint8_t *ptr;
57     const int hbd = p->p.bpc > 8;
58 
59     ptr = p->data[0];
60     for (int y = 0; y < p->p.h; y++) {
61         if (fwrite(ptr, p->p.w << hbd, 1, c->f) != 1)
62             goto error;
63         ptr += p->stride[0];
64     }
65 
66     if (p->p.layout != DAV1D_PIXEL_LAYOUT_I400) {
67         // u/v
68         const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
69         const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
70         const int cw = (p->p.w + ss_hor) >> ss_hor;
71         const int ch = (p->p.h + ss_ver) >> ss_ver;
72         for (int pl = 1; pl <= 2; pl++) {
73             ptr = p->data[pl];
74             for (int y = 0; y < ch; y++) {
75                 if (fwrite(ptr, cw << hbd, 1, c->f) != 1)
76                     goto error;
77                 ptr += p->stride[1];
78             }
79         }
80     }
81 
82     dav1d_picture_unref(p);
83     return 0;
84 
85 error:
86     dav1d_picture_unref(p);
87     fprintf(stderr, "Failed to write frame data: %s\n", strerror(errno));
88     return -1;
89 }
90 
yuv_close(YuvOutputContext * const c)91 static void yuv_close(YuvOutputContext *const c) {
92     if (c->f != stdout)
93         fclose(c->f);
94 }
95 
96 const Muxer yuv_muxer = {
97     .priv_data_size = sizeof(YuvOutputContext),
98     .name = "yuv",
99     .extension = "yuv",
100     .write_header = yuv_open,
101     .write_picture = yuv_write,
102     .write_trailer = yuv_close,
103 };
104