annotate libvo/jpeg_enc.c @ 5567:cfa4a2c0366e

Lots of updates and bugfixes to the tdfxfb driver
author mark
date Thu, 11 Apr 2002 21:43:09 +0000
parents 7a4ea9beebc0
children 34b27da59eaf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
1 /* Straightforward (to be) optimized JPEG encoder for the YUV422 format
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
2 * based on mjpeg code from ffmpeg.
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
3 *
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
4 * Copyright (c) 2002, Rik Snel
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
5 * Parts from ffmpeg Copyright (c) 2000, 2001 Gerard Lantau
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
6 *
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
10 * (at your option) any later version.
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
11 *
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
15 * GNU General Public License for more details.
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
16 *
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
20 *
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
21 * For an excellent introduction to the JPEG format, see:
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
22 * http://www.ece.purdue.edu/~bouman/grad-labs/lab8/pdf/lab.pdf
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
23 */
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
24
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
25
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
26
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
27 #include <sys/types.h>
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
28 #include <stdio.h>
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
29 #include <stdlib.h>
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
30 #include "config.h"
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
31 #ifdef USE_FASTMEMCPY
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
32 #include "fastmemcpy.h"
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
33 #endif
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
34 #include "../mp_msg.h"
4683
fba4289cdb41 we are now able to use the new bitwriter code in libavcodec
rik
parents: 4651
diff changeset
35 /* We need this #define because we need ../libavcodec/common.h to #define
fba4289cdb41 we are now able to use the new bitwriter code in libavcodec
rik
parents: 4651
diff changeset
36 * be2me_32, otherwise the linker will complain that it doesn't exist */
fba4289cdb41 we are now able to use the new bitwriter code in libavcodec
rik
parents: 4651
diff changeset
37 #define HAVE_AV_CONFIG_H
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
38 #include "../libavcodec/avcodec.h"
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
39 #include "../libavcodec/dsputil.h"
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
40 #include "../libavcodec/mpegvideo.h"
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
41
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
42 #include "jpeg_enc.h"
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
43
5391
7a4ea9beebc0 use avcodec_inited instead of av_fdct to see if avcodec was initialized by another part of mplayer
rik
parents: 4683
diff changeset
44 extern int avcodec_inited;
7a4ea9beebc0 use avcodec_inited instead of av_fdct to see if avcodec was initialized by another part of mplayer
rik
parents: 4683
diff changeset
45
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
46 /* zr_mjpeg_encode_mb needs access to these tables for the black & white
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
47 * option */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
48 typedef struct MJpegContext {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
49 UINT8 huff_size_dc_luminance[12];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
50 UINT16 huff_code_dc_luminance[12];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
51 UINT8 huff_size_dc_chrominance[12];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
52 UINT16 huff_code_dc_chrominance[12];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
53
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
54 UINT8 huff_size_ac_luminance[256];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
55 UINT16 huff_code_ac_luminance[256];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
56 UINT8 huff_size_ac_chrominance[256];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
57 UINT16 huff_code_ac_chrominance[256];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
58 } MJpegContext;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
59
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
60
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
61 /* A very important function pointer */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
62 extern int (*dct_quantize)(MpegEncContext *s,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
63 DCTELEM *block, int n, int qscale);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
64
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
65
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
66 /* Begin excessive code duplication ************************************/
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
67 /* Code coming from mpegvideo.c and mjpeg.c in ../libavcodec ***********/
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
68
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
69 static const unsigned short aanscales[64] = {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
70 /* precomputed values scaled up by 14 bits */
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
71 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
72 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
73 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
74 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
75 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
76 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
77 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
78 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
79 };
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
80
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
81 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
82 {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
83 int i;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
84
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
85 if (av_fdct == jpeg_fdct_ifast) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
86 for(i=0;i<64;i++) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
87 /* 16 <= qscale * quant_matrix[i] <= 7905 */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
88 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
89 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
90 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
91
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
92 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
93 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
94 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
95 } else {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
96 for(i=0;i<64;i++) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
97 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
98 So 16 <= qscale * quant_matrix[i] <= 7905
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
99 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
100 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
101 */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
102 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
103 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
104 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
105 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
106 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
107
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
108 static inline void encode_dc(MpegEncContext *s, int val,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
109 UINT8 *huff_size, UINT16 *huff_code)
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
110 {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
111 int mant, nbits;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
112
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
113 if (val == 0) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
114 jput_bits(&s->pb, huff_size[0], huff_code[0]);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
115 } else {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
116 mant = val;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
117 if (val < 0) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
118 val = -val;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
119 mant--;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
120 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
121
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
122 /* compute the log (XXX: optimize) */
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
123 nbits = 0;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
124 while (val != 0) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
125 val = val >> 1;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
126 nbits++;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
127 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
128
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
129 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
130
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
131 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
132 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
133 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
134
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
135 static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
136 {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
137 int mant, nbits, code, i, j;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
138 int component, dc, run, last_index, val;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
139 MJpegContext *m = s->mjpeg_ctx;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
140 UINT8 *huff_size_ac;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
141 UINT16 *huff_code_ac;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
142
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
143 /* DC coef */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
144 component = (n <= 3 ? 0 : n - 4 + 1);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
145 dc = block[0]; /* overflow is impossible */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
146 val = dc - s->last_dc[component];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
147 if (n < 4) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
148 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
149 huff_size_ac = m->huff_size_ac_luminance;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
150 huff_code_ac = m->huff_code_ac_luminance;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
151 } else {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
152 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
153 huff_size_ac = m->huff_size_ac_chrominance;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
154 huff_code_ac = m->huff_code_ac_chrominance;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
155 }
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
156 s->last_dc[component] = dc;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
157
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
158 /* AC coefs */
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
159
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
160 run = 0;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
161 last_index = s->block_last_index[n];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
162 for(i=1;i<=last_index;i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
163 j = zigzag_direct[i];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
164 val = block[j];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
165 if (val == 0) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
166 run++;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
167 } else {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
168 while (run >= 16) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
169 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
170 run -= 16;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
171 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
172 mant = val;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
173 if (val < 0) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
174 val = -val;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
175 mant--;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
176 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
177
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
178 /* compute the log (XXX: optimize) */
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
179 nbits = 0;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
180 while (val != 0) {
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
181 val = val >> 1;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
182 nbits++;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
183 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
184 code = (run << 4) | nbits;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
185
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
186 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
187
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
188 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
189 run = 0;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
190 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
191 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
192
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
193 /* output EOB only if not already 64 values */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
194 if (last_index < 63 || run != 0)
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
195 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
196 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
197
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
198 /* End excessive code duplication **************************************/
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
199
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
200 /* this function is a reproduction of the one in mjpeg, it includes two
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
201 * changes, it allows for black&white encoding (it skips the U and V
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
202 * macroblocks and it outputs the huffman code for 'no change' (dc) and
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
203 * 'all zero' (ac)) and it takes 4 macroblocks (422) instead of 6 (420) */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
204 static void zr_mjpeg_encode_mb(jpeg_enc_t *j) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
205
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
206 MJpegContext *m = j->s->mjpeg_ctx;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
207
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
208 encode_block(j->s, j->s->block[0], 0);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
209 encode_block(j->s, j->s->block[1], 1);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
210 if (j->bw) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
211 /* U */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
212 jput_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
213 m->huff_code_dc_chrominance[0]);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
214 jput_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
215 m->huff_code_ac_chrominance[0]);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
216 /* V */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
217 jput_bits(&j->s->pb, m->huff_size_dc_chrominance[0],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
218 m->huff_code_dc_chrominance[0]);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
219 jput_bits(&j->s->pb, m->huff_size_ac_chrominance[0],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
220 m->huff_code_ac_chrominance[0]);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
221 } else {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
222 /* we trick encode_block here so that it uses
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
223 * chrominance huffman tables instead of luminance ones
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
224 * (see the effect of second argument of encode_block) */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
225 encode_block(j->s, j->s->block[2], 4);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
226 encode_block(j->s, j->s->block[3], 5);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
227 }
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
228 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
229
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
230 /* this function can take all kinds of YUV colorspaces
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
231 * YV12, YVYU, UYVY. The necesary parameters must be set up by the caller
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
232 * y_ps means "y pixel size", y_rs means "y row size".
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
233 * For YUYV, for example, is u_buf = y_buf + 1, v_buf = y_buf + 3,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
234 * y_ps = 2, u_ps = 4, v_ps = 4, y_rs = u_rs = v_rs.
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
235 *
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
236 * The actual buffers must be passed with mjpeg_encode_frame, this is
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
237 * to make it possible to call encode on the buffer provided by the
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
238 * codec in draw_frame.
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
239 *
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
240 * The data is straightened out at the moment it is put in DCT
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
241 * blocks, there are therefore no spurious memcopies involved */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
242 /* Notice that w must be a multiple of 16 and h must be a multiple of 8 */
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
243 /* We produce YUV422 jpegs, the colors must be subsampled horizontally,
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
244 * if the colors are also subsampled vertically, then this function
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
245 * performs cheap upsampling (better solution will be: a DCT that is
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
246 * optimized in the case that every two rows are the same) */
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
247 /* cu = 0 means 'No cheap upsampling'
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
248 * cu = 1 means 'perform cheap upsampling' */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
249 /* The encoder doesn't know anything about interlacing, the halve height
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
250 * needs to be passed and the double rowstride. Which field gets encoded
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
251 * is decided by what buffers are passed to mjpeg_encode_frame */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
252 jpeg_enc_t *jpeg_enc_init(int w, int h, int y_psize, int y_rsize,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
253 int u_psize, int u_rsize, int v_psize, int v_rsize,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
254 int cu, int q, int b) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
255 jpeg_enc_t *j;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
256 int i = 0;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
257 mp_msg(MSGT_VO, MSGL_V, "JPEnc init: %dx%d %d %d %d %d %d %d\n",
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
258 w, h, y_psize, y_rsize, u_psize,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
259 u_rsize, v_psize, v_rsize);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
260
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
261 j = malloc(sizeof(jpeg_enc_t));
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
262 if (j == NULL) return NULL;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
263
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
264 j->s = malloc(sizeof(MpegEncContext));
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
265 if (j->s == NULL) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
266 free(j);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
267 return NULL;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
268 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
269
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
270 /* info on how to access the pixels */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
271 j->y_ps = y_psize;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
272 j->u_ps = u_psize;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
273 j->v_ps = v_psize;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
274 j->y_rs = y_rsize;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
275 j->u_rs = u_rsize;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
276 j->v_rs = v_rsize;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
277
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
278 j->s->width = w;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
279 j->s->height = h;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
280 j->s->qscale = q;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
281
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
282 j->s->out_format = FMT_MJPEG;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
283 j->s->intra_only = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
284 j->s->encoding = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
285 j->s->pict_type = I_TYPE;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
286 j->s->y_dc_scale = 8;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
287 j->s->c_dc_scale = 8;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
288
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
289 j->s->mjpeg_write_tables = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
290 j->s->mjpeg_vsample[0] = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
291 j->s->mjpeg_vsample[1] = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
292 j->s->mjpeg_vsample[2] = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
293 j->s->mjpeg_hsample[0] = 2;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
294 j->s->mjpeg_hsample[1] = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
295 j->s->mjpeg_hsample[2] = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
296
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
297 j->cheap_upsample = cu;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
298 j->bw = b;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
299
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
300 /* if libavcodec is used by the decoder then we must not
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
301 * initialize again, but if it is not initialized then we must
5391
7a4ea9beebc0 use avcodec_inited instead of av_fdct to see if avcodec was initialized by another part of mplayer
rik
parents: 4683
diff changeset
302 * initialize it here. */
7a4ea9beebc0 use avcodec_inited instead of av_fdct to see if avcodec was initialized by another part of mplayer
rik
parents: 4683
diff changeset
303 if (!avcodec_inited) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
304 /* we need to initialize libavcodec */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
305 avcodec_init();
5391
7a4ea9beebc0 use avcodec_inited instead of av_fdct to see if avcodec was initialized by another part of mplayer
rik
parents: 4683
diff changeset
306 avcodec_register_all();
7a4ea9beebc0 use avcodec_inited instead of av_fdct to see if avcodec was initialized by another part of mplayer
rik
parents: 4683
diff changeset
307 avcodec_inited=1;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
308 }
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
309
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
310 if (mjpeg_init(j->s) < 0) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
311 free(j->s);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
312 free(j);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
313 return NULL;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
314 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
315
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
316 if (MPV_common_init(j->s) < 0) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
317 free(j->s);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
318 free(j);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
319 return NULL;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
320 }
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
321
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
322 /* correct the value for sc->mb_height */
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
323 j->s->mb_height = j->s->height/8;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
324 j->s->mb_intra = 1;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
325
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
326 j->s->intra_matrix[0] = default_intra_matrix[0];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
327 for (i = 1; i < 64; i++)
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
328 j->s->intra_matrix[i] =
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
329 (default_intra_matrix[i]*j->s->qscale) >> 3;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
330 convert_matrix(j->s->q_intra_matrix, j->s->q_intra_matrix16,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
331 j->s->intra_matrix, 8);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
332 return j;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
333 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
334
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
335 int jpeg_enc_frame(jpeg_enc_t *j, unsigned char *y_data,
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
336 unsigned char *u_data, unsigned char *v_data, char *bufr) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
337 int i, k, mb_x, mb_y;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
338 short int *dest;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
339 unsigned char *source;
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
340 /* initialize the buffer */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
341
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
342 init_put_bits(&j->s->pb, bufr, 1024*256, NULL, NULL);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
343
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
344 mjpeg_picture_header(j->s);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
345
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
346 j->s->last_dc[0] = 128;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
347 j->s->last_dc[1] = 128;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
348 j->s->last_dc[2] = 128;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
349
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
350 for (mb_y = 0; mb_y < j->s->mb_height; mb_y++) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
351 for (mb_x = 0; mb_x < j->s->mb_width; mb_x++) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
352 /* conversion 8 to 16 bit and filling of blocks
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
353 * must be mmx optimized */
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
354 /* fill 2 Y macroblocks and one U and one V */
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
355 source = mb_y * 8 * j->y_rs +
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
356 16 * j->y_ps * mb_x + y_data;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
357 dest = j->s->block[0];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
358 for (i = 0; i < 8; i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
359 for (k = 0; k < 8; k++) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
360 dest[k] = source[k*j->y_ps];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
361 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
362 dest += 8;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
363 source += j->y_rs;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
364 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
365 source = mb_y * 8 * j->y_rs +
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
366 (16*mb_x + 8)*j->y_ps + y_data;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
367 dest = j->s->block[1];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
368 for (i = 0; i < 8; i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
369 for (k = 0; k < 8; k++) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
370 dest[k] = source[k*j->y_ps];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
371 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
372 dest += 8;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
373 source += j->y_rs;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
374 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
375 if (!j->bw && j->cheap_upsample) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
376 source = mb_y*4*j->u_rs +
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
377 8*mb_x*j->u_ps + u_data;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
378 dest = j->s->block[2];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
379 for (i = 0; i < 4; i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
380 for (k = 0; k < 8; k++) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
381 dest[k] = source[k*j->u_ps];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
382 dest[k+8] = source[k*j->u_ps];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
383 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
384 dest += 16;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
385 source += j->u_rs;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
386 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
387 source = mb_y*4*j->v_rs +
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
388 8*mb_x*j->v_ps + v_data;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
389 dest = j->s->block[3];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
390 for (i = 0; i < 4; i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
391 for (k = 0; k < 8; k++) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
392 dest[k] = source[k*j->v_ps];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
393 dest[k+8] = source[k*j->v_ps];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
394 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
395 dest += 16;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
396 source += j->u_rs;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
397 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
398 } else if (!j->bw && !j->cheap_upsample) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
399 source = mb_y*8*j->u_rs +
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
400 8*mb_x*j->u_ps + u_data;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
401 dest = j->s->block[2];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
402 for (i = 0; i < 8; i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
403 for (k = 0; k < 8; k++)
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
404 dest[k] = source[k*j->u_ps];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
405 dest += 8;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
406 source += j->u_rs;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
407 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
408 source = mb_y*8*j->v_rs +
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
409 8*mb_x*j->v_ps + v_data;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
410 dest = j->s->block[3];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
411 for (i = 0; i < 8; i++) {
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
412 for (k = 0; k < 8; k++)
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
413 dest[k] = source[k*j->v_ps];
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
414 dest += 8;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
415 source += j->u_rs;
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
416 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
417 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
418 emms_c(); /* is this really needed? */
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
419
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
420 j->s->block_last_index[0] =
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
421 dct_quantize(j->s, j->s->block[0],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
422 0, j->s->qscale);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
423 j->s->block_last_index[1] =
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
424 dct_quantize(j->s, j->s->block[1],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
425 1, j->s->qscale);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
426
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
427 if (!j->bw) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
428 j->s->block_last_index[4] =
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
429 dct_quantize(j->s, j->s->block[2],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
430 4, j->s->qscale);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
431 j->s->block_last_index[5] =
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
432 dct_quantize(j->s, j->s->block[3],
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
433 5, j->s->qscale);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
434 }
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
435 zr_mjpeg_encode_mb(j);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
436 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
437 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
438 emms_c();
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
439 mjpeg_picture_trailer(j->s);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
440 flush_put_bits(&j->s->pb);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
441
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
442 if (j->s->mjpeg_write_tables == 1)
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
443 j->s->mjpeg_write_tables = 0;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
444
4683
fba4289cdb41 we are now able to use the new bitwriter code in libavcodec
rik
parents: 4651
diff changeset
445 return pbBufPtr(&(j->s->pb)) - j->s->pb.buf;
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
446 }
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
447
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
448 void jpeg_enc_uninit(jpeg_enc_t *j) {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
449 mjpeg_close(j->s);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
450 free(j->s);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
451 free(j);
4347
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
452 }
d036a9992baf fast 4:2:2 jpeg encoder, based on libavcodec. - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
453
4651
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
454 #if 0
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
455
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
456 #define W 32
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
457 #define H 32
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
458
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
459 int quant_store[MBR+1][MBC+1];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
460 unsigned char buf[W*H*3/2];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
461 char code[256*1024];
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
462
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
463
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
464 main() {
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
465 int i, size;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
466 FILE *fp;
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
467
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
468 memset(buf, 0, W*H);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
469 memset(buf+W*H, 255, W*H/4);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
470 memset(buf+5*W*H/4, 0, W*H/4);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
471 mjpeg_encoder_init(W, H, 1, W, 1, W/2, 1, W/2, 1, 1, 0);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
472
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
473 size = mjpeg_encode_frame(buf, buf+W*H, buf+5*W*H/4, code);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
474 fp = fopen("test.jpg", "w");
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
475 fwrite(code, 1, size, fp);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
476 fclose(fp);
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
477 }
49f2bc22f880 mjpeg encoder cleanup - patch by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents: 4347
diff changeset
478 #endif