summaryrefslogtreecommitdiff
path: root/msmcobalt/mm-video-v4l2/vidc/vdec/src/omx_swvdec_utils.cpp
blob: 56c3f30524c92c9909a7f453bcddfc3d9f93ad34 (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
/**
 * @copyright
 *
 *   Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *   * Neither the name of The Linux Foundation nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE DISCLAIMED.
 *   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
 *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 *   DAMAGE.
 *
 * @file
 *
 *   omx_swvdec_utils.cpp
 *
 * @brief
 *
 *   OMX software video decoder utility functions source.
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>

#include <cutils/properties.h>

#include "omx_swvdec_utils.h"

#define OMX_SWVDEC_LOGLEVEL_DEFAULT 2 ///< default OMX SwVdec loglevel

unsigned int g_omx_swvdec_logmask = (1 << OMX_SWVDEC_LOGLEVEL_DEFAULT) - 1;
                              ///< global OMX SwVdec logmask variable definition

/**
 * @brief Initialize OMX SwVdec log level & mask.
 */
void omx_swvdec_log_init()
{
    int omx_swvdec_loglevel = OMX_SWVDEC_LOGLEVEL_DEFAULT;

    char property_value[PROPERTY_VALUE_MAX] = {0};

    if (property_get("omx_swvdec.log.level", property_value, NULL))
    {
        omx_swvdec_loglevel = atoi(property_value);

        if (omx_swvdec_loglevel > 3)
        {
            omx_swvdec_loglevel = 3;
        }

        if (omx_swvdec_loglevel < 0)
        {
            omx_swvdec_loglevel = 0;
        }

        OMX_SWVDEC_LOG_HIGH(
            "omx_swvdec.log.level: %d; %s",
            omx_swvdec_loglevel,
            (omx_swvdec_loglevel == 3) ? "error, high, & low logs" :
            ((omx_swvdec_loglevel == 2) ? "error & high logs" :
             ((omx_swvdec_loglevel == 1) ? "error logs" :
              "no logs")));
    }

    g_omx_swvdec_logmask = (unsigned int) ((1 << omx_swvdec_loglevel) - 1);
}

/**
 * @brief OMX SwVdec queue constructor.
 */
omx_swvdec_queue::omx_swvdec_queue()
{
    pthread_mutex_init(&m_mutex, NULL);
}

/**
 * @brief OMX SwVdec queue destructor.
 */
omx_swvdec_queue::~omx_swvdec_queue()
{
    pthread_mutex_destroy(&m_mutex);
}

/**
 * @brief Push event to queue.
 *
 * @param[in] p_event_info: Pointer to event information structure.
 */
void omx_swvdec_queue::push(OMX_SWVDEC_EVENT_INFO *p_event_info)
{
    pthread_mutex_lock(&m_mutex);

    m_queue.push(*p_event_info);

    pthread_mutex_unlock(&m_mutex);
}

/**
 * @brief Pop event from queue.
 *
 * @param[in,out] p_event_info: Pointer to event information structure.
 *
 * @retval  true if pop successful
 * @retval false if pop unsuccessful
 */
bool omx_swvdec_queue::pop(OMX_SWVDEC_EVENT_INFO *p_event_info)
{
    bool retval = true;

    pthread_mutex_lock(&m_mutex);

    if (m_queue.empty())
    {
        retval = false;
    }
    else
    {
        *p_event_info = m_queue.front();

        m_queue.pop();
    }

    pthread_mutex_unlock(&m_mutex);

    return retval;
}

/**
 * @brief OMX SwVdec diagnostics class constructor.
 */
omx_swvdec_diag::omx_swvdec_diag():
    m_dump_ip(0),
    m_dump_op(0),
    m_filename_ip(NULL),
    m_filename_op(NULL),
    m_file_ip(NULL),
    m_file_op(NULL)
{
    time_t time_raw;

    struct tm *time_info;

    char time_string[16];

    char filename_ip[PROPERTY_VALUE_MAX];
    char filename_op[PROPERTY_VALUE_MAX];

    char property_value[PROPERTY_VALUE_MAX] = {0};

    time_raw = time(NULL);

    time_info = localtime(&time_raw);

    if (time_info != NULL)
    {
        // time string: "YYYYmmddTHHMMSS"
        strftime(time_string, sizeof(time_string), "%Y%m%dT%H%M%S", time_info);
    }
    else
    {
        // time string: "19700101T000000"
        snprintf(time_string, sizeof(time_string), "19700101T000000");
    }

    // default ip filename: "/data/misc/media/omx_swvdec_YYYYmmddTHHMMSS_ip.bin"
    snprintf(filename_ip,
             sizeof(filename_ip),
             "%s/omx_swvdec_%s_ip.bin",
             DIAG_FILE_PATH,
             time_string);

    // default op filename: "/data/misc/media/omx_swvdec_YYYYmmddTHHMMSS_op.yuv"
    snprintf(filename_op,
             sizeof(filename_op),
             "%s/omx_swvdec_%s_op.yuv",
             DIAG_FILE_PATH,
             time_string);

    if (property_get("omx_swvdec.dump.ip", property_value, NULL))
    {
        m_dump_ip = atoi(property_value);

        OMX_SWVDEC_LOG_HIGH("omx_swvdec.dump.ip: %d", m_dump_ip);
    }

    if (property_get("omx_swvdec.dump.op", property_value, NULL))
    {
        m_dump_op = atoi(property_value);

        OMX_SWVDEC_LOG_HIGH("omx_swvdec.dump.op: %d", m_dump_op);
    }

    if (m_dump_ip && property_get("omx_swvdec.filename.ip",
                                  property_value,
                                  filename_ip))
    {
        m_filename_ip =
            (char *) malloc((strlen(property_value) + 1) * sizeof(char));

        if (m_filename_ip == NULL)
        {
            OMX_SWVDEC_LOG_ERROR("failed to allocate %zu bytes for "
                                 "input filename string",
                                 (strlen(property_value) + 1) * sizeof(char));
        }
        else
        {
            strlcpy(m_filename_ip, property_value, strlen(m_filename_ip));

            OMX_SWVDEC_LOG_HIGH("omx_swvdec.filename.ip: %s", m_filename_ip);

            if ((m_file_ip = fopen(m_filename_ip, "wb")) == NULL)
            {
                OMX_SWVDEC_LOG_ERROR("cannot open input file '%s'",
                                     m_filename_ip);
            }
        }
    }

    if (m_dump_op && property_get("omx_swvdec.filename.op",
                                  property_value,
                                  filename_op))
    {
        m_filename_op =
            (char *) malloc((strlen(property_value) + 1) * sizeof(char));

        if (m_filename_op == NULL)
        {
            OMX_SWVDEC_LOG_ERROR("failed to allocate %zu bytes for "
                                 "output filename string",
                                 (strlen(property_value) + 1) * sizeof(char));
        }
        else
        {
            strlcpy(m_filename_op, property_value, strlen(m_filename_op));

            OMX_SWVDEC_LOG_HIGH("omx_swvdec.filename.op: %s", m_filename_op);

            if ((m_file_op = fopen(m_filename_op, "wb")) == NULL)
            {
                OMX_SWVDEC_LOG_ERROR("cannot open output file '%s'",
                                     m_filename_op);
            }
        }
    }
}

/**
 * @brief OMX SwVdec diagnostics class destructor.
 */
omx_swvdec_diag::~omx_swvdec_diag()
{
    if (m_file_op)
    {
        fclose(m_file_op);
        m_file_op = NULL;
    }

    if (m_file_ip)
    {
        fclose(m_file_ip);
        m_file_ip = NULL;
    }

    if (m_filename_op)
    {
        free(m_filename_op);
        m_filename_op = NULL;
    }

    if (m_filename_ip)
    {
        free(m_filename_ip);
        m_filename_ip = NULL;
    }
}

/**
 * @brief Dump input bitstream to file.
 *
 * @param[in] p_buffer:      Pointer to input bitstream buffer.
 * @param[in] filled_length: Bitstream buffer's filled length.
 */
void omx_swvdec_diag::dump_ip(unsigned char *p_buffer,
                              unsigned int   filled_length)
{
    if (m_dump_ip && (m_file_ip != NULL))
    {
        fwrite(p_buffer, sizeof(unsigned char), filled_length, m_file_ip);
    }
}

/**
 * @brief Dump output YUV to file.
 *
 * @param[in] p_buffer:  Pointer to output YUV buffer.
 * @param[in] width:     Frame width.
 * @param[in] height:    Frame height.
 * @param[in] stride:    Frame stride.
 * @param[in] scanlines: Frame scanlines.
 */
void omx_swvdec_diag::dump_op(unsigned char *p_buffer,
                              unsigned int   width,
                              unsigned int   height,
                              unsigned int   stride,
                              unsigned int   scanlines)
{
    if (m_dump_op && (m_file_op != NULL))
    {
        unsigned char *p_buffer_y;
        unsigned char *p_buffer_uv;

        unsigned int ii;

        p_buffer_y  = p_buffer;
        p_buffer_uv = p_buffer + (stride * scanlines);

        for (ii = 0; ii < height; ii++)
        {
            fwrite(p_buffer_y, sizeof(unsigned char), width, m_file_op);

            p_buffer_y += stride;
        }

        for (ii = 0; ii < (height / 2); ii++)
        {
            fwrite(p_buffer_uv, sizeof(unsigned char), width, m_file_op);

            p_buffer_uv += stride;
        }
    }
}