summaryrefslogtreecommitdiff
path: root/common/impeg2_format_conv.c
blob: ec0bcfb61eab14eab84500eedd33c0403feabf9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/*****************************************************************************/
/*                                                                           */
/*  File Name         : impeg2_format_conv .c                                */
/*                                                                           */
/*  Description       : Contains functions needed to convert the images in   */
/*                      different color spaces to yuv 422i color space       */
/*                                                                           */
/*  List of Functions : YUV420toYUV420()                                      */
/*                      YUV420toYUV422I()                                    */
/*                      YUV420toYUV420SP_VU()                                */
/*                      YUV420toYUV420SP_UU()                                */
/*                                                                           */
/*  Issues / Problems : None                                                 */
/*                                                                           */
/*  Revision History  :                                                      */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         28 08 2007  Naveen Kumar T        Draft                           */
/*                                                                           */
/*****************************************************************************/
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/

/* System include files */

/* User include files */
#include <stdio.h>
#include <string.h>
#include "iv_datatypedef.h"
#include "iv.h"
#include "ithread.h"

#include "iv_datatypedef.h"
#include "impeg2_macros.h"
#include "impeg2_buf_mgr.h"
#include "impeg2_disp_mgr.h"
#include "impeg2_defs.h"
#include "impeg2_platform_macros.h"

#include "impeg2_job_queue.h"
#include "impeg2_format_conv.h"


/*****************************************************************************/
/*                                                                           */
/*  Function Name : impeg2_copy_frm_yuv420p()                                        */
/*                                                                           */
/*  Description   : This function performs conversion from YUV420 to         */
/*                  YUV422I color space.                                     */
/*                                                                           */
/*  Inputs        : pu1_src_y,       -   UWORD8 pointer to source y plane.   */
/*                  pu1_src_u,       -   UWORD8 pointer to source u plane.   */
/*                  pu1_src_v,       -   UWORD8 pointer to source v plane.   */
/*                  pu1_dst_y,       -   UWORD8 pointer to dest y plane.     */
/*                  pu1_dst_u,       -   UWORD8 pointer to dest u plane.     */
/*                  pu1_dst_v,       -   UWORD8 pointer to dest v plane.     */
/*                  u4_width,        -   Width of image.                     */
/*                  u4_height,       -   Height of image.                    */
/*                  u4_src_stride_y  -   Stride in pixels of source Y plane. */
/*                  u4_src_stride_u  -   Stride in pixels of source U plane. */
/*                  u4_src_stride_v  -   Stride in pixels of source V plane. */
/*                  u4_dst_stride_y  -   Stride in pixels of dest Y plane.   */
/*                  u4_dst_stride_u  -   Stride in pixels of dest U plane.   */
/*                  u4_dst_stride_v  -   Stride in pixels of dest V plane.   */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : One row is processed at a time. The one iteration of the */
/*                  code will rearrange pixels into YUV422 interleaved       */
/*                  format.                                                  */
/*                                                                           */
/*  Outputs       : None                                                     */
/*                                                                           */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         29 08 2007  Naveen Kumar T        Draft                           */
/*                                                                           */
/*****************************************************************************/
void impeg2_copy_frm_yuv420p(UWORD8 *pu1_src_y,
                             UWORD8 *pu1_src_u,
                             UWORD8 *pu1_src_v,
                             UWORD8 *pu1_dst_y,
                             UWORD8 *pu1_dst_u,
                             UWORD8 *pu1_dst_v,
                             UWORD32 u4_width,
                             UWORD32 u4_height,
                             UWORD32 u4_src_stride_y,
                             UWORD32 u4_src_stride_u,
                             UWORD32 u4_src_stride_v,
                             UWORD32 u4_dst_stride_y,
                             UWORD32 u4_dst_stride_u,
                             UWORD32 u4_dst_stride_v)
{
    WORD32 i4_cnt;
    WORD32  i4_y_height     = (WORD32) u4_height;
    WORD32  i4_uv_height    = u4_height >> 1;
    WORD32  i4_uv_width     = u4_width >> 1;

    for(i4_cnt = 0; i4_cnt < i4_y_height; i4_cnt++)
    {
        memcpy(pu1_dst_y, pu1_src_y, u4_width);
        pu1_dst_y += (u4_dst_stride_y);
        pu1_src_y += (u4_src_stride_y);
    }

    for(i4_cnt = 0; i4_cnt < i4_uv_height; i4_cnt++)
    {
        memcpy(pu1_dst_u, pu1_src_u, i4_uv_width);
        pu1_dst_u += (u4_dst_stride_u);
        pu1_src_u += (u4_src_stride_u);

    }

    for(i4_cnt = 0; i4_cnt < i4_uv_height; i4_cnt++)
    {
        memcpy(pu1_dst_v, pu1_src_v, i4_uv_width);
        pu1_dst_v += (u4_dst_stride_v);
        pu1_src_v += (u4_src_stride_v);

    }

}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : impeg2_fmt_conv_yuv420p_to_yuv422ile()                   */
/*                                                                           */
/*  Description   : This function performs conversion from YUV420 to         */
/*                  YUV422I color space.                                     */
/*                                                                           */
/*  Inputs        : pu1_y            -   UWORD8 pointer to y plane.          */
/*                  pu1_u            -   UWORD8 pointer to u plane.          */
/*                  pu1_v            -   UWORD8 pointer to u plane.          */
/*                  pu2_yuv422i      -   UWORD16 pointer to yuv422iimage.    */
/*                  u4_width         -   Width of the Y plane.               */
/*                  u4_height        -   Height of the Y plane.              */
/*                  u4_stride_y      -   Stride in pixels of Y plane.        */
/*                  u4_stride_u      -   Stride in pixels of U plane.        */
/*                  u4_stride_v      -   Stride in pixels of V plane.        */
/*                  u4_stride_yuv422i-   Stride in pixels of yuv422i image.  */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : One row is processed at a time. The one iteration of the */
/*                  code will rearrange pixels into YUV422 interleaved       */
/*                  format.                                                  */
/*                                                                           */
/*  Outputs       : None                                                     */
/*                                                                           */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         29 08 2007  Naveen Kumar T        Draft                           */
/*                                                                           */
/*****************************************************************************/

void impeg2_fmt_conv_yuv420p_to_yuv422ile(register UWORD8 *pu1_y,
                     register UWORD8 *pu1_u,
                     register UWORD8 *pu1_v,
                     void *pv_yuv422i,
                     UWORD32 u4_width,
                     UWORD32 u4_height,
                     UWORD32 u4_stride_y,
                     UWORD32 u4_stride_u,
                     UWORD32 u4_stride_v,
                     UWORD32 u4_stride_yuv422i)
{
    /* Declare local variables */
    register WORD16  i,j;
    register UWORD16 u2_offset1,u2_offset2,u2_offset3,u2_offset_yuv422i;
    register UWORD8  u1_y1,u1_uv;
    register UWORD32 u4_pixel;
    register UWORD16 u2_width_cnt;
    register UWORD32 *pu4_yuv422i;

    UWORD8 u1_flag;             /* This flag is used to indicate wether the row is even or odd */

    u1_flag=0x0;                /* Intialize it with 0 indicating odd row */

    /* Calculate the offsets necessary to make input and output buffers to point next row */
    u2_offset1       = u4_stride_y - u4_width;
    u2_offset2       = u4_stride_u - ((u4_width + 1) >> 1);
    u2_offset3       = u4_stride_v - ((u4_width + 1) >> 1);
    u2_offset_yuv422i = (u4_stride_yuv422i >> 1) -((u4_width + 1) >> 1);

    /* Type cast the output pointer to UWORD32 */
    pu4_yuv422i      = (UWORD32 *)pv_yuv422i;

    /* Calculate the loop counter for inner loop */
    u2_width_cnt     = u4_width >> 1;

    /* Run the loop for height of input buffer */
    for(i = u4_height; i > 0; i--)
    {
        /* Run the loop for width/2 */
        for(j = u2_width_cnt; j > 0; j--)
        {
            /* Store the value in output buffer in the order U0Y0V0Y1U2Y2V2Y3.... */
            /* Load Y0 */
            u1_y1          = *pu1_y++;
            /* Load Y1 */
            u4_pixel       = *pu1_y++;
            /* Load V0 */
            u1_uv          = *pu1_v++;
            u4_pixel       = (u4_pixel << 8) + u1_uv;
            /* Load U0 */
            u1_uv          = *pu1_u++;
            u4_pixel       = (u4_pixel << 8) + u1_y1;
            u4_pixel       = (u4_pixel << 8) + u1_uv;
            *pu4_yuv422i++ = u4_pixel;
        }
        /* Incase of width is odd number take care of last pixel */
        if(u4_width & 0x1)
        {
            /* Store the value in output buffer in the order U0Y0V0Y1U2Y2V2Y3.... */
            /* Load Y0 */
            u1_y1          = *pu1_y++;
            /* Load V0 */
            u1_uv          = *pu1_v++;
            /* Take Y0 as Y1 */
            u4_pixel       = u1_y1;
            u4_pixel       = (u4_pixel << 8) + u1_uv;
            /* Load U0 */
            u1_uv          = *pu1_u++;
            u4_pixel       = (u4_pixel << 8) + u1_y1;
            u4_pixel       = (u4_pixel << 8) + u1_uv;
            *pu4_yuv422i++ = u4_pixel;
        }
        /* Make the pointers to buffer to point to next row */
        pu1_y = pu1_y       + u2_offset1;
        if(!u1_flag)
        {
            /* Restore the pointers of u and v buffer back so that the row of pixels are also  */
            /* Processed with same row of u and values again */
            pu1_u = pu1_u - ((u4_width + 1) >> 1);
            pu1_v = pu1_v - ((u4_width + 1) >> 1);
        }
        else
        {
            /* Adjust the u and v buffer pointers so that they will point to next row */
            pu1_u = pu1_u + u2_offset2;
            pu1_v = pu1_v + u2_offset3;
        }

        /* Adjust the output buffer pointer for next row */
        pu4_yuv422i = pu4_yuv422i + u2_offset_yuv422i;
        /* Toggle the flag to convert between odd and even row */
        u1_flag= u1_flag ^ 0x1;
    }
}




void impeg2_fmt_conv_yuv420p_to_yuv420sp_vu(UWORD8 *pu1_y, UWORD8 *pu1_u, UWORD8 *pu1_v,
                                     UWORD8 *pu1_dest_y, UWORD8 *pu1_dest_uv,
                                     UWORD32 u4_height,  UWORD32 u4_width,UWORD32 u4_stridey,
                                     UWORD32 u4_strideu, UWORD32 u4_stridev,
                                     UWORD32 u4_dest_stride_y, UWORD32 u4_dest_stride_uv,
                                     UWORD32 u4_convert_uv_only
                                     )

{


    UWORD8 *pu1_src,*pu1_dst;
    UWORD8 *pu1_src_u, *pu1_src_v;
    UWORD16 i;
    UWORD32 u2_width_uv;

    UWORD32 u4_dest_inc_y=0, u4_dest_inc_uv=0;


    /* Copy Y buffer */
    pu1_dst = (UWORD8 *)pu1_dest_y;
    pu1_src = (UWORD8 *)pu1_y;

    u4_dest_inc_y =    u4_dest_stride_y;
    u4_dest_inc_uv =   u4_dest_stride_uv;

    if(0 == u4_convert_uv_only)
    {
        for(i = 0; i < u4_height; i++)
        {
            memcpy((void *)pu1_dst,(void *)pu1_src, u4_width);
            pu1_dst += u4_dest_inc_y;
            pu1_src += u4_stridey;
        }
    }

    /* Interleave Cb and Cr buffers */
    pu1_src_u = pu1_u;
    pu1_src_v = pu1_v;
    pu1_dst = pu1_dest_uv ;

    u4_height = (u4_height + 1) >> 1;
    u2_width_uv = (u4_width + 1) >> 1;
    for(i = 0; i < u4_height ; i++)
    {
        UWORD32 j;
        for(j = 0; j < u2_width_uv; j++)
        {
            *pu1_dst++ = *pu1_src_v++;
            *pu1_dst++ = *pu1_src_u++;

        }

        pu1_dst += u4_dest_inc_uv - u4_width;
        pu1_src_u  += u4_strideu - u2_width_uv;
        pu1_src_v  += u4_stridev - u2_width_uv;
    }
}

void impeg2_fmt_conv_yuv420p_to_yuv420sp_uv(UWORD8 *pu1_y, UWORD8 *pu1_u, UWORD8 *pu1_v,
                                     UWORD8 *pu1_dest_y, UWORD8 *pu1_dest_uv,
                                     UWORD32 u4_height,  UWORD32 u4_width,UWORD32 u4_stridey,
                                     UWORD32 u4_strideu, UWORD32 u4_stridev,
                                     UWORD32 u4_dest_stride_y, UWORD32 u4_dest_stride_uv,
                                     UWORD32 u4_convert_uv_only)

{


    UWORD8 *pu1_src,*pu1_dst;
    UWORD8 *pu1_src_u, *pu1_src_v;
    UWORD16 i;
    UWORD32 u2_width_uv;

    UWORD32 u4_dest_inc_y=0, u4_dest_inc_uv=0;


    /* Copy Y buffer */
    pu1_dst = (UWORD8 *)pu1_dest_y;
    pu1_src = (UWORD8 *)pu1_y;

    u4_dest_inc_y =    u4_dest_stride_y;
    u4_dest_inc_uv =   u4_dest_stride_uv;

    if(0 == u4_convert_uv_only)
    {
        for(i = 0; i < u4_height; i++)
        {
            memcpy((void *)pu1_dst,(void *)pu1_src, u4_width);
            pu1_dst += u4_dest_inc_y;
            pu1_src += u4_stridey;
        }
    }

    /* Interleave Cb and Cr buffers */
    pu1_src_u = pu1_u;
    pu1_src_v = pu1_v;
    pu1_dst = pu1_dest_uv ;

    u4_height = (u4_height + 1) >> 1;
    u2_width_uv = (u4_width + 1) >> 1;
    for(i = 0; i < u4_height ; i++)
    {
        UWORD32 j;
        for(j = 0; j < u2_width_uv; j++)
        {
            *pu1_dst++ = *pu1_src_u++;
            *pu1_dst++ = *pu1_src_v++;
        }

        pu1_dst += u4_dest_inc_uv - u4_width;
        pu1_src_u  += u4_strideu - u2_width_uv;
        pu1_src_v  += u4_stridev - u2_width_uv;
    }

}