Tizen 2.0 Release
[sdk/emulator/qemu.git] / tizen / src / hw / maru_codec.h
1 /*
2  * Virtual Codec device
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact:
7  *  Kitae Kim <kt920.kim@samsung.com>
8  *  SeokYeon Hwang <syeon.hwang@samsung.com>
9  *  DongKyun Yun <dk77.yun@samsung.com>
10  *  YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  *
29  */
30
31 #include <stdio.h>
32 #include <pthread.h>
33 #include <sys/types.h>
34 #include "hw.h"
35 #include "kvm.h"
36 #include "pci.h"
37 #include "pci_ids.h"
38 #include "tizen/src/debug_ch.h"
39 #include "maru_pci_ids.h"
40
41 #include <libavformat/avformat.h>
42
43 #define CODEC_MAX_CONTEXT   10
44
45 /*
46  *  Codec Device Structures
47  */
48
49 typedef struct _SVCodecParam {
50     uint32_t        apiIndex;
51     uint32_t        ctxIndex;
52     uint32_t        in_args[20];
53     uint32_t        ret_args;
54     uint32_t        mmapOffset;
55         uint32_t                fileIndex;
56 } SVCodecParam;
57
58 typedef struct _SVCodecContext {
59     AVCodecContext          *pAVCtx;
60     AVFrame                 *pFrame;
61     AVCodecParserContext    *pParserCtx;
62     uint8_t                 *pParserBuffer;
63     bool                    bParser;
64     bool                    bUsed;
65         uint32_t                                nFileValue;
66 } SVCodecContext;
67
68 typedef struct _SVCodecState {
69     PCIDevice           dev;
70     SVCodecContext      ctxArr[CODEC_MAX_CONTEXT];
71     SVCodecParam        codecParam;
72     pthread_mutex_t     codec_mutex;
73
74     int                 mmioIndex;
75     uint32_t            mem_addr;
76     uint32_t            mmio_addr;
77
78     uint8_t*            vaddr;
79     MemoryRegion        vram;
80     MemoryRegion        mmio;
81 } SVCodecState;
82
83 enum {
84     CODEC_API_INDEX         = 0x00,
85     CODEC_IN_PARAM          = 0x04,
86     CODEC_RETURN_VALUE      = 0x08,
87     CODEC_CONTEXT_INDEX     = 0x0c,
88     CODEC_MMAP_OFFSET       = 0x10,
89     CODEC_FILE_INDEX        = 0x14,
90     CODEC_CLOSED            = 0x18,
91 };
92
93 enum {
94     EMUL_AV_REGISTER_ALL = 1,
95     EMUL_AVCODEC_ALLOC_CONTEXT,
96     EMUL_AVCODEC_ALLOC_FRAME,
97     EMUL_AVCODEC_OPEN,
98     EMUL_AVCODEC_CLOSE,
99     EMUL_AV_FREE_CONTEXT,
100     EMUL_AV_FREE_FRAME,
101     EMUL_AV_FREE_PALCTRL,
102     EMUL_AV_FREE_EXTRADATA,
103     EMUL_AVCODEC_FLUSH_BUFFERS,
104     EMUL_AVCODEC_DECODE_VIDEO,
105     EMUL_AVCODEC_ENCODE_VIDEO,
106     EMUL_AVCODEC_DECODE_AUDIO,
107     EMUL_AVCODEC_ENCODE_AUDIO,
108     EMUL_AV_PICTURE_COPY,
109     EMUL_AV_PARSER_INIT,
110     EMUL_AV_PARSER_PARSE,
111     EMUL_AV_PARSER_CLOSE,
112 };
113
114
115 /*
116  *  Codec Device APIs
117  */
118 int codec_init (PCIBus *bus);
119
120 uint64_t codec_read (void *opaque, target_phys_addr_t addr, unsigned size);
121
122 void codec_write (void *opaque, target_phys_addr_t addr, uint64_t value, unsigned size);
123
124 static int codec_operate(uint32_t apiIndex, uint32_t ctxIndex, SVCodecState *state);
125
126 /*
127  *  Codec Helper APIs
128  */
129 static void qemu_parser_init (SVCodecState *s, int ctxIndex);
130
131 static void qemu_restore_context (AVCodecContext *dst, AVCodecContext *src);
132
133 /*
134  *  FFMPEG APIs
135  */
136
137 static void qemu_av_register_all (void);
138
139 static int qemu_avcodec_open (SVCodecState *s, int ctxIndex);
140
141 static int qemu_avcodec_close (SVCodecState *s, int ctxIndex);
142
143 static void qemu_avcodec_alloc_context (SVCodecState *s);
144
145 static void qemu_avcodec_alloc_frame (SVCodecState *s);
146
147 static void qemu_av_free_context (SVCodecState* s, int ctxIndex);
148
149 static void qemu_av_free_picture (SVCodecState* s, int ctxIndex);
150
151 static void qemu_av_free_palctrl (SVCodecState* s, int ctxIndex);
152
153 static void qemu_av_free_extradata (SVCodecState* s, int ctxIndex);
154
155 static void qemu_avcodec_flush_buffers (SVCodecState *s, int ctxIndex);
156
157 static int qemu_avcodec_decode_video (SVCodecState *s, int ctxIndex);
158
159 static int qemu_avcodec_encode_video (SVCodecState *s, int ctxIndex);
160
161 static int qemu_avcodec_decode_audio (SVCodecState *s, int ctxIndex);
162
163 static int qemu_avcodec_encode_audio (SVCodecState *s, int ctxIndex);
164
165 static void qemu_av_picture_copy (SVCodecState *s, int ctxIndex);
166
167 static void qemu_av_parser_init (SVCodecState *s, int ctxIndex);
168
169 static int qemu_av_parser_parse (SVCodecState *s, int ctxIndex);
170
171 static void qemu_av_parser_close (SVCodecState *s, int ctxIndex);
172
173 static int qemu_avcodec_get_buffer (AVCodecContext *context, AVFrame *picture);
174
175 static void qemu_avcodec_release_buffer (AVCodecContext *context, AVFrame *picture);