]> git.sesse.net Git - vlc/blob - modules/codec/omxil/qcom.c
mediacodec: Support playing back VP8 as well
[vlc] / modules / codec / omxil / qcom.c
1 /*****************************************************************************
2  * qcom.c : pixel format translation for Qualcomm tiled nv12
3  *****************************************************************************
4  * Copyright © 2012 Rafaël Carré
5  *
6  * Authors: Rafaël Carré <funman@videolanorg>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_picture.h>
28
29 #include <string.h>
30 #include <stdint.h>
31
32 #include "qcom.h"
33
34
35 /*
36  * The format is called QOMX_COLOR_FormatYUV420PackedSemiPlanar64x32Tile2m8ka.
37  * First wtf: why call it YUV420? It is NV12 (interleaved U&V).
38  * Second wtf: why this format at all?
39  */
40 #define TILE_WIDTH 64
41 #define TILE_HEIGHT 32
42 #define TILE_SIZE (TILE_WIDTH * TILE_HEIGHT)
43
44 /* get frame tile coordinate. XXX: nothing to be understood here, don't try. */
45 static size_t tile_pos(size_t x, size_t y, size_t w, size_t h)
46 {
47     size_t flim = x + (y & ~1) * w;
48
49     if (y & 1) {
50         flim += (x & ~3) + 2;
51     } else if ((h & 1) == 0 || y != (h - 1)) {
52         flim += (x + 2) & ~3;
53     }
54
55     return flim;
56 }
57
58 void qcom_convert(const uint8_t *src, picture_t *pic)
59 {
60     size_t width = pic->format.i_width;
61     size_t pitch = pic->p[0].i_pitch;
62     size_t height = pic->format.i_height;
63
64     const size_t tile_w = (width - 1) / TILE_WIDTH + 1;
65     const size_t tile_w_align = (tile_w + 1) & ~1;
66
67     const size_t tile_h_luma = (height - 1) / TILE_HEIGHT + 1;
68     const size_t tile_h_chroma = (height / 2 - 1) / TILE_HEIGHT + 1;
69
70     size_t luma_size = tile_w_align * tile_h_luma * TILE_SIZE;
71
72 #define TILE_GROUP_SIZE (4 * TILE_SIZE)
73     if((luma_size % TILE_GROUP_SIZE) != 0)
74         luma_size = (((luma_size - 1) / TILE_GROUP_SIZE) + 1) * TILE_GROUP_SIZE;
75
76     for(size_t y = 0; y < tile_h_luma; y++) {
77         size_t row_width = width;
78         for(size_t x = 0; x < tile_w; x++) {
79             /* luma source pointer for this tile */
80             const uint8_t *src_luma  = src
81                 + tile_pos(x, y,tile_w_align, tile_h_luma) * TILE_SIZE;
82
83             /* chroma source pointer for this tile */
84             const uint8_t *src_chroma = src + luma_size
85                 + tile_pos(x, y/2, tile_w_align, tile_h_chroma) * TILE_SIZE;
86             if (y & 1)
87                 src_chroma += TILE_SIZE/2;
88
89             /* account for right columns */
90             size_t tile_width = row_width;
91             if (tile_width > TILE_WIDTH)
92                 tile_width = TILE_WIDTH;
93
94             /* account for bottom rows */
95             size_t tile_height = height;
96             if (tile_height > TILE_HEIGHT)
97                 tile_height = TILE_HEIGHT;
98
99             /* dest luma memory index for this tile */
100             size_t luma_idx = y * TILE_HEIGHT * pitch + x * TILE_WIDTH;
101
102             /* dest chroma memory index for this tile */
103             /* XXX: remove divisions */
104             size_t chroma_idx = (luma_idx / pitch) * pitch/2 + (luma_idx % pitch);
105
106             tile_height /= 2; // we copy 2 luma lines at once
107             while (tile_height--) {
108                 memcpy(&pic->p[0].p_pixels[luma_idx], src_luma, tile_width);
109                 src_luma += TILE_WIDTH;
110                 luma_idx += pitch;
111
112                 memcpy(&pic->p[0].p_pixels[luma_idx], src_luma, tile_width);
113                 src_luma += TILE_WIDTH;
114                 luma_idx += pitch;
115
116                 memcpy(&pic->p[1].p_pixels[chroma_idx], src_chroma, tile_width);
117                 src_chroma += TILE_WIDTH;
118                 chroma_idx += pitch;
119             }
120             row_width -= TILE_WIDTH;
121         }
122         height -= TILE_HEIGHT;
123     }
124 }