1/*
2 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22* @file
23* function definitions common to libschroedingerdec.c and libschroedingerenc.c
24*/
25
26#include "libdirac_libschro.h"
27#include "libschroedinger.h"
28
29/**
30* Schroedinger video preset table. Ensure that this tables matches up correctly
31* with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
32*/
33static const SchroVideoFormatEnum ff_schro_video_formats[]={
34    SCHRO_VIDEO_FORMAT_CUSTOM     ,
35    SCHRO_VIDEO_FORMAT_QSIF       ,
36    SCHRO_VIDEO_FORMAT_QCIF       ,
37    SCHRO_VIDEO_FORMAT_SIF        ,
38    SCHRO_VIDEO_FORMAT_CIF        ,
39    SCHRO_VIDEO_FORMAT_4SIF       ,
40    SCHRO_VIDEO_FORMAT_4CIF       ,
41    SCHRO_VIDEO_FORMAT_SD480I_60  ,
42    SCHRO_VIDEO_FORMAT_SD576I_50  ,
43    SCHRO_VIDEO_FORMAT_HD720P_60  ,
44    SCHRO_VIDEO_FORMAT_HD720P_50  ,
45    SCHRO_VIDEO_FORMAT_HD1080I_60 ,
46    SCHRO_VIDEO_FORMAT_HD1080I_50 ,
47    SCHRO_VIDEO_FORMAT_HD1080P_60 ,
48    SCHRO_VIDEO_FORMAT_HD1080P_50 ,
49    SCHRO_VIDEO_FORMAT_DC2K_24    ,
50    SCHRO_VIDEO_FORMAT_DC4K_24    ,
51};
52
53SchroVideoFormatEnum ff_get_schro_video_format_preset(AVCodecContext *avccontext)
54{
55    unsigned int num_formats = sizeof(ff_schro_video_formats) /
56                               sizeof(ff_schro_video_formats[0]);
57
58    unsigned int idx = ff_dirac_schro_get_video_format_idx (avccontext);
59
60    return (idx < num_formats) ? ff_schro_video_formats[idx] :
61                                 SCHRO_VIDEO_FORMAT_CUSTOM;
62}
63
64int ff_get_schro_frame_format (SchroChromaFormat schro_pix_fmt,
65                               SchroFrameFormat  *schro_frame_fmt)
66{
67    unsigned int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
68                               sizeof(ffmpeg_schro_pixel_format_map[0]);
69
70    int idx;
71
72    for (idx = 0; idx < num_formats; ++idx) {
73        if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
74            *schro_frame_fmt = ffmpeg_schro_pixel_format_map[idx].schro_frame_fmt;
75            return 0;
76        }
77    }
78    return -1;
79}
80
81static void FreeSchroFrame(SchroFrame *frame, void *priv)
82{
83    AVPicture *p_pic = priv;
84
85    if (!p_pic)
86        return;
87
88    avpicture_free(p_pic);
89    av_freep(&p_pic);
90}
91
92SchroFrame *ff_create_schro_frame(AVCodecContext *avccontext,
93                                  SchroFrameFormat schro_frame_fmt)
94{
95    AVPicture *p_pic;
96    SchroFrame *p_frame;
97    int y_width, uv_width;
98    int y_height, uv_height;
99    int i;
100
101    y_width   = avccontext->width;
102    y_height  = avccontext->height;
103    uv_width  = y_width  >> (SCHRO_FRAME_FORMAT_H_SHIFT(schro_frame_fmt));
104    uv_height = y_height >> (SCHRO_FRAME_FORMAT_V_SHIFT(schro_frame_fmt));
105
106    p_pic = av_mallocz(sizeof(AVPicture));
107    avpicture_alloc(p_pic, avccontext->pix_fmt, y_width, y_height);
108
109    p_frame         = schro_frame_new();
110    p_frame->format = schro_frame_fmt;
111    p_frame->width  = y_width;
112    p_frame->height = y_height;
113    schro_frame_set_free_callback(p_frame, FreeSchroFrame, (void *)p_pic);
114
115    for (i = 0; i < 3; ++i) {
116        p_frame->components[i].width  = i ? uv_width : y_width;
117        p_frame->components[i].stride = p_pic->linesize[i];
118        p_frame->components[i].height = i ? uv_height : y_height;
119        p_frame->components[i].length =
120                 p_frame->components[i].stride * p_frame->components[i].height;
121        p_frame->components[i].data   = p_pic->data[i];
122
123        if (i) {
124            p_frame->components[i].v_shift =
125                SCHRO_FRAME_FORMAT_V_SHIFT(p_frame->format);
126            p_frame->components[i].h_shift =
127                SCHRO_FRAME_FORMAT_H_SHIFT(p_frame->format);
128        }
129    }
130
131    return p_frame;
132}
133