1 /*****************************************************************************
2  * video_output.h
3  *****************************************************************************
4  * Copyright (C) 2012-2015 L-SMASH Works project
5  *
6  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  *****************************************************************************/
20 
21 /* This file is available under an ISC license. */
22 
23 #define REPEAT_CONTROL_CACHE_NUM 2
24 
25 #define LW_FRAME_PROP_CHANGE_FLAG_WIDTH        (1<<0)
26 #define LW_FRAME_PROP_CHANGE_FLAG_HEIGHT       (1<<1)
27 #define LW_FRAME_PROP_CHANGE_FLAG_PIXEL_FORMAT (1<<2)
28 #define LW_FRAME_PROP_CHANGE_FLAG_COLORSPACE   (1<<3)
29 #define LW_FRAME_PROP_CHANGE_FLAG_YUV_RANGE    (1<<4)
30 
31 typedef struct
32 {
33     int                scaler_flags;
34     int                frame_prop_change_flags;
35     int                input_width;
36     int                input_height;
37     enum AVPixelFormat input_pixel_format;
38     enum AVPixelFormat output_pixel_format;
39     enum AVColorSpace  input_colorspace;
40     int                input_yuv_range;
41     struct SwsContext *sws_ctx;
42 } lw_video_scaler_handler_t;
43 
44 typedef struct
45 {
46     uint32_t top;
47     uint32_t bottom;
48 } lw_video_frame_order_t;
49 
50 typedef struct
51 {
52     lw_video_scaler_handler_t scaler;
53     int                       output_width;
54     int                       output_height;
55     /* VFR->CFR conversion */
56     int                       vfr2cfr;
57     uint32_t                  cfr_num;
58     uint32_t                  cfr_den;
59     /* Repeat control */
60     int                       repeat_control;
61     int64_t                   repeat_correction_ts;
62     uint32_t                  frame_count;
63     uint32_t                  frame_order_count;
64     lw_video_frame_order_t   *frame_order_list;
65     AVFrame                  *frame_cache_buffers[REPEAT_CONTROL_CACHE_NUM];
66     uint32_t                  frame_cache_numbers[REPEAT_CONTROL_CACHE_NUM];
67     /* Application private extension */
68     void                     *private_handler;
69     void (*free_private_handler)( void *private_handler );
70 } lw_video_output_handler_t;
71 
72 int avoid_yuv_scale_conversion( enum AVPixelFormat *pixel_format );
73 
74 void setup_video_rendering
75 (
76     lw_video_output_handler_t *vohp,
77     int                        scaler_flags,
78     int                        width,
79     int                        height,
80     enum AVPixelFormat         output_pixel_format,
81     struct AVCodecContext     *ctx,
82     int (*dr_get_buffer)( struct AVCodecContext *, AVFrame *, int )
83 );
84 
85 /* Return 0 if no update.
86  * Return 1 if any update.
87  * Retunr a negative value otherwise. */
88 int update_scaler_configuration_if_needed
89 (
90     lw_video_scaler_handler_t *vshp,
91     lw_log_handler_t          *lhp,
92     const AVFrame             *av_frame
93 );
94 
95 void lw_cleanup_video_output_handler
96 (
97     lw_video_output_handler_t *vohp
98 );
99