]> git.sesse.net Git - vlc/blob - modules/video_output/yuv.c
kva: handle rotated movies
[vlc] / modules / video_output / yuv.c
1 /*****************************************************************************
2  * yuv.c : yuv video output
3  *****************************************************************************
4  * Copyright (C) 2008, M2X BV
5  * $Id$
6  *
7  * Authors: Jean-Paul Saman <jpsaman@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout_display.h>
35 #include <vlc_picture_pool.h>
36 #include <vlc_fs.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define YUV_FILE_TEXT N_("device, fifo or filename")
42 #define YUV_FILE_LONGTEXT N_("device, fifo or filename to write yuv frames too.")
43
44 #define CHROMA_TEXT N_("Chroma used")
45 #define CHROMA_LONGTEXT N_(\
46     "Force use of a specific chroma for output. Default is I420.")
47
48 #define YUV4MPEG2_TEXT N_("YUV4MPEG2 header (default disabled)")
49 #define YUV4MPEG2_LONGTEXT N_("The YUV4MPEG2 header is compatible " \
50     "with mplayer yuv video output and requires YV12/I420 fourcc. By default "\
51     "vlc writes the fourcc of the picture frame into the output destination.")
52
53 #define CFG_PREFIX "yuv-"
54
55 static int  Open (vlc_object_t *);
56 static void Close(vlc_object_t *);
57
58 vlc_module_begin()
59     set_shortname(N_("YUV output"))
60     set_description(N_("YUV video output"))
61     set_category(CAT_VIDEO)
62     set_subcategory(SUBCAT_VIDEO_VOUT)
63     set_capability("vout display", 0)
64
65     add_string(CFG_PREFIX "file", "stream.yuv",
66                 YUV_FILE_TEXT, YUV_FILE_LONGTEXT, false)
67     add_string(CFG_PREFIX "chroma", NULL,
68                 CHROMA_TEXT, CHROMA_LONGTEXT, true)
69     add_bool  (CFG_PREFIX "yuv4mpeg2", false,
70                 YUV4MPEG2_TEXT, YUV4MPEG2_LONGTEXT, true)
71
72     set_callbacks(Open, Close)
73 vlc_module_end()
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static const char *const ppsz_vout_options[] = {
79     "file", "chroma", "yuv4mpeg2", NULL
80 };
81
82 /* */
83 static picture_pool_t *Pool  (vout_display_t *, unsigned);
84 static void           Display(vout_display_t *, picture_t *, subpicture_t *subpicture);
85 static int            Control(vout_display_t *, int, va_list);
86
87 /*****************************************************************************
88  * vout_display_sys_t: video output descriptor
89  *****************************************************************************/
90 struct vout_display_sys_t {
91     FILE *f;
92     bool  is_first;
93     bool  is_yuv4mpeg2;
94
95     picture_pool_t *pool;
96 };
97
98 /* */
99 static int Open(vlc_object_t *object)
100 {
101     vout_display_t *vd = (vout_display_t *)object;
102     vout_display_sys_t *sys;
103
104     /* Allocate instance and initialize some members */
105     vd->sys = sys = malloc(sizeof(*sys));
106     if (!sys)
107         return VLC_ENOMEM;
108
109     sys->is_first = false;
110     sys->is_yuv4mpeg2 = var_InheritBool(vd, CFG_PREFIX "yuv4mpeg2");
111     sys->pool = NULL;
112
113     /* */
114     char *psz_fcc = var_InheritString(vd, CFG_PREFIX "chroma");
115     const vlc_fourcc_t requested_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES,
116                                                                         psz_fcc);
117     free(psz_fcc);
118
119     const vlc_fourcc_t chroma = requested_chroma ? requested_chroma :
120                                                    VLC_CODEC_I420;
121     if (sys->is_yuv4mpeg2) {
122         switch (chroma) {
123         case VLC_CODEC_YV12:
124         case VLC_CODEC_I420:
125         case VLC_CODEC_J420:
126             break;
127         default:
128             msg_Err(vd, "YUV4MPEG2 mode needs chroma YV12 not %4.4s as requested",
129                     (char *)&chroma);
130             free(sys);
131             return VLC_EGENERIC;
132         }
133     }
134     msg_Dbg(vd, "Using chroma %4.4s", (char *)&chroma);
135
136     /* */
137     char *name = var_InheritString(vd, CFG_PREFIX "file");
138     if (!name) {
139         msg_Err(vd, "Empty file name");
140         free(sys);
141         return VLC_EGENERIC;
142     }
143     sys->f = vlc_fopen(name, "wb");
144
145     if (!sys->f) {
146         msg_Err(vd, "Failed to open %s", name);
147         free(name);
148         free(sys);
149         return VLC_EGENERIC;
150     }
151     msg_Dbg(vd, "Writing data to %s", name);
152     free(name);
153
154     /* */
155     video_format_t fmt;
156     video_format_ApplyRotation(&fmt, &vd->fmt);
157     fmt.i_chroma = chroma;
158     video_format_FixRgb(&fmt);
159
160     /* */
161     vout_display_info_t info = vd->info;
162     info.has_hide_mouse = true;
163
164     /* */
165     vd->fmt     = fmt;
166     vd->info    = info;
167     vd->pool    = Pool;
168     vd->prepare = NULL;
169     vd->display = Display;
170     vd->control = Control;
171     vd->manage  = NULL;
172
173     vout_display_SendEventFullscreen(vd, false);
174     return VLC_SUCCESS;
175 }
176
177 /* */
178 static void Close(vlc_object_t *object)
179 {
180     vout_display_t *vd = (vout_display_t *)object;
181     vout_display_sys_t *sys = vd->sys;
182
183     if (sys->pool)
184         picture_pool_Delete(sys->pool);
185     fclose(sys->f);
186     free(sys);
187 }
188
189 /*****************************************************************************
190  *
191  *****************************************************************************/
192 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
193 {
194     vout_display_sys_t *sys = vd->sys;
195     if (!sys->pool)
196         sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
197     return sys->pool;
198 }
199
200 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
201 {
202     vout_display_sys_t *sys = vd->sys;
203
204     /* */
205     video_format_t fmt = vd->fmt;
206
207     if (ORIENT_IS_SWAP(vd->source.orientation))
208     {
209         fmt.i_sar_num = vd->source.i_sar_den;
210         fmt.i_sar_den = vd->source.i_sar_num;
211     }
212     else
213     {
214         fmt.i_sar_num = vd->source.i_sar_num;
215         fmt.i_sar_den = vd->source.i_sar_den;
216     }
217
218     /* */
219     char type;
220     if (picture->b_progressive)
221         type = 'p';
222     else if (picture->b_top_field_first)
223         type = 't';
224     else
225         type = 'b';
226
227     if (type != 'p') {
228         msg_Warn(vd, "Received a non progressive frame, "
229                      "it will be written as progressive.");
230         type = 'p';
231     }
232
233     /* */
234     if (!sys->is_first) {
235         const char *header;
236         char buffer[5];
237         if (sys->is_yuv4mpeg2) {
238             /* MPlayer compatible header, unfortunately it doesn't tell you
239              * the exact fourcc used. */
240             header = "YUV4MPEG2";
241         } else {
242             snprintf(buffer, sizeof(buffer), "%4.4s", 
243                      (const char*)&fmt.i_chroma);
244             header = buffer;
245         }
246
247         fprintf(sys->f, "%s W%d H%d F%d:%d I%c A%d:%d\n",
248                 header,
249                 fmt.i_visible_width, fmt.i_visible_height,
250                 fmt.i_frame_rate, fmt.i_frame_rate_base,
251                 type,
252                 fmt.i_sar_num, fmt.i_sar_den);
253         sys->is_first = true;
254     }
255
256     /* */
257     fprintf(sys->f, "FRAME\n");
258     for (int i = 0; i < picture->i_planes; i++) {
259         const plane_t *plane = &picture->p[i];
260         const uint8_t *pixels = plane->p_pixels;
261
262         pixels += (vd->fmt.i_x_offset * plane->i_visible_pitch)
263                   / vd->fmt.i_visible_height;
264
265         for( int y = 0; y < plane->i_visible_lines; y++) {
266             const size_t written = fwrite(pixels, 1, plane->i_visible_pitch,
267                                           sys->f);
268             if (written != (size_t)plane->i_visible_pitch)
269                 msg_Warn(vd, "only %zd of %d bytes written",
270                          written, plane->i_visible_pitch);
271
272             pixels += plane->i_pitch;
273         }
274     }
275     fflush(sys->f);
276
277     /* */
278     picture_Release(picture);
279     VLC_UNUSED(subpicture);
280 }
281
282 static int Control(vout_display_t *vd, int query, va_list args)
283 {
284     VLC_UNUSED(vd);
285     switch (query) {
286     case VOUT_DISPLAY_CHANGE_FULLSCREEN: {
287         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
288         if (cfg->is_fullscreen)
289             return VLC_EGENERIC;
290         return VLC_SUCCESS;
291     }
292     default:
293         return VLC_EGENERIC;
294     }
295 }
296