comparison libmpcodecs/vd_zrmjpeg.c @ 11386:229079491864

addition of special image formats for Zoran MJPEG, and vd_zrmjpeg.c the zoran passthrough filter for the new vo_zr2.
author rik
date Tue, 04 Nov 2003 19:58:37 +0000
parents
children 90b4a1345b96
comparison
equal deleted inserted replaced
11385:cd0088334aea 11386:229079491864
1 /*
2 * Copyright (C) 2003 Rik Snel <snel@phys.uu.nl>, license GPL v2
3 * - based on vd_mpegpes.c by A'rpi (C) 2002-2003
4 * - guess_mjpeg_type code stolen from lav_io.c (C) 2000 Rainer Johanni
5 * <Rainer@Johanni.de> from the mjpegtools package
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "config.h"
11 #include "mp_msg.h"
12
13 /* some convenient #define's, is this portable enough? */
14 #define VERBOSE(...) mp_msg(MSGT_DECVIDEO, MSGL_V, "vd_zrmjpeg: " __VA_ARGS__)
15 #define ERROR(...) mp_msg(MSGT_DECVIDEO, MSGL_ERR, "vd_zrmjpeg: " __VA_ARGS__)
16 #define WARNING(...) mp_msg(MSGT_DECVIDEO, MSGL_WARN, \
17 "vd_zrmjpeg: " __VA_ARGS__)
18
19 #include "vd_internal.h"
20
21 static vd_info_t info =
22 {
23 "Zoran MJPEG Video passthrough",
24 "zrmjpeg",
25 "Rik Snel <snel@phys.uu.nl>",
26 "Rik Snel <snel@phys.uu.nl>",
27 "for hw decoders (DC10(+)/buz/lml33)"
28 };
29
30 LIBVD_EXTERN(zrmjpeg)
31
32 #include "libvo/video_out.h"
33
34 typedef struct {
35 int vo_inited;
36 unsigned int preferred_csp;
37 } vd_zrmjpeg_ctx_t;
38
39 static int query_format(sh_video_t *sh, unsigned int format) {
40 vd_zrmjpeg_ctx_t *ctx = sh->context;
41 if (format == ctx->preferred_csp) return CONTROL_TRUE;
42 return CONTROL_FALSE;
43 }
44
45 // to set/get/query special features/parameters
46 static int control(sh_video_t *sh, int cmd, void* arg, ...) {
47 switch (cmd) {
48 case VDCTRL_QUERY_FORMAT:
49 return query_format(sh, *((unsigned int*)arg));
50 }
51 return CONTROL_UNKNOWN;
52 }
53
54 // init driver
55 static int init(sh_video_t *sh) {
56 vd_zrmjpeg_ctx_t *ctx;
57
58 VERBOSE("init called\n");
59 ctx = malloc(sizeof(*ctx));
60 if (!ctx) return 0;
61 memset(ctx, 0, sizeof(*ctx));
62 sh->context = ctx;
63
64 /* defer init of vo until the first frame is known */
65 return 1;
66 #if 0
67 return mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h, IMGFMT_ZRMJPEGIT);
68 #endif
69 }
70
71 // uninit driver
72 static void uninit(sh_video_t *sh) {
73 free(sh->context);
74 }
75
76 /* parts directly stolen from scan_jpg() and lav_open_input_file */
77 static int get_int2(unsigned char *buf) {
78 return buf[0]*256 + buf[1];
79 }
80
81 #define M_SOF0 0xC0
82 #define M_SOF1 0xC1
83 #define M_DHT 0xC4
84 #define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
85 #define M_EOI 0xD9 /* End Of Image (end of datastream) */
86 #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
87 #define M_DQT 0xDB
88 #define M_APP0 0xE0
89 #define M_APP1 0xE1
90 /* returns 0 in case of failure */
91 static unsigned int guess_mjpeg_type(unsigned char *data, unsigned int size,
92 int d_height) {
93 unsigned int p;
94 int marker, length, height, ncomps, i, hf[3], vf[3];
95 unsigned int app0 = 0, header = 0;
96
97 /* The initial marker must be SIO */
98 if (size < 2) {
99 ERROR("JPEG data too short (%d bytes)\n", size);
100 return 0;
101 }
102
103 if (data[0] != 0xFF || data[1] != M_SOI) {
104 ERROR("JPEG data must start with FFD8, but doesn't\n");
105 return 0;
106 }
107
108 p = 2; /* pointer within jpeg data */
109
110 while (p < size) {
111 /* search 0xFF */
112 while(data[p] != 0xFF) {
113 p++;
114 if (p >= size) return 0;
115 }
116
117 /* get marker code, skip duplicate FF's */
118 while(data[p] == 0xFF) {
119 p++;
120 if (p >= size) return 0;
121 }
122
123 marker = data[p++];
124
125 /* marker may have an associated length */
126 if (p <= size - 2) length = get_int2(data+p);
127 else length = 0;
128
129 switch (marker) {
130 case M_SOF0:
131 case M_SOF1:
132 header = p-2;
133 VERBOSE("found offset of header %u\n",
134 header);
135 break;
136 case M_SOS:
137 size = 0;
138 continue;
139 case M_APP0:
140 app0 = p-2;
141 VERBOSE("found offset of APP0 %u\n",
142 app0);
143 break;
144 }
145
146 /* these markers shouldn't have parameters,
147 * i.e. we don't need to skip anaything */
148 if (marker == 0 || marker == 1 ||
149 (marker >= 0xd0 && marker < 0xd8))
150 continue;
151
152 if (p + length <= size) p += length;
153 else {
154 ERROR("input JPEG too short, data missing\n");
155 return 0;
156 }
157 }
158
159 if (!header) {
160 ERROR("JPEG header (with resolution and sampling factors) not found\n");
161 return 0;
162 }
163
164 if (data[header + 9] != 3) {
165 ERROR("JPEG has wrong number of components\n");
166 return 0;
167 }
168
169 /* get the horizontal and vertical sample factors */
170 for (i = 0; i < 3; i++) {
171 hf[i] = data[header + 10 + 3*i + 1]>>4;
172 vf[i] = data[header + 10 + 3*i + 1]&0x0F;
173 }
174
175 if (hf[0] != 2 || hf[1] != 1 || hf[2] != 1 ||
176 vf[0] != 1 || vf[1] != 1 || vf[2] != 1) {
177 ERROR("JPEG has wrong internal image format\n");
178 } else VERBOSE("JPEG has colorspace YUV422 with minimal sampling factors (good)\n");
179
180 height = get_int2(data + header + 5);
181 if (height == d_height) {
182 VERBOSE("data is non interlaced\n");
183 return IMGFMT_ZRMJPEGNI;
184 }
185
186 if (2*height != d_height) {
187 ERROR("something very inconsistent happened\n");
188 return 0;
189 }
190
191
192 if (app0 && get_int2(data + app0 + 2) >= 5 &&
193 strncasecmp((char*)(data + app0 + 4), "AVI1", 4) == 0) {
194 if (data[app0+8] == 1) {
195 VERBOSE("data is interlaced, APP0: top-first (1)\n");
196 return IMGFMT_ZRMJPEGIT;
197 } else {
198 VERBOSE("data is interlaced, APP0: bottom-first (%d)\n",
199 data[app0+8]);
200 return IMGFMT_ZRMJPEGIB;
201 }
202 } else {
203 VERBOSE("data is interlaced, no (valid) APP0 marker, "
204 "guessing top-first\n");
205 return IMGFMT_ZRMJPEGIT;
206 }
207
208
209 return 0;
210 }
211
212 // decode a frame
213 static mp_image_t* decode(sh_video_t *sh, void* data, int len, int flags) {
214 mp_image_t* mpi;
215 vd_zrmjpeg_ctx_t *ctx = sh->context;
216
217 if (!ctx->vo_inited) {
218 ctx->preferred_csp = guess_mjpeg_type(data, len, sh->disp_h);
219 if (ctx->preferred_csp == 0) return NULL;
220 mpcodecs_config_vo(sh, sh->disp_w, sh->disp_h,
221 ctx->preferred_csp);
222 ctx->vo_inited = 1;
223 }
224
225 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0,
226 sh->disp_w, sh->disp_h);
227 /* abuse of mpi */
228 mpi->planes[0]=(uint8_t*)data;
229 mpi->planes[1]=(uint8_t*)len;
230 return mpi;
231 }