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