annotate h261enc.c @ 5510:37bf17e052fb libavcodec

use get_unary from bitstream.h
author alex
date Wed, 08 Aug 2007 22:36:12 +0000
parents 65b1bfb3a74c
children a4104482ceef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
1 /*
5057
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
2 * H261 encoder
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
4 * Copyright (c) 2004 Maarten Daniels
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
5 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3740
diff changeset
6 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3740
diff changeset
7 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3740
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3740
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
12 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3740
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
16 * Lesser General Public License for more details.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
17 *
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3740
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2967
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
21 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
22
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
23 /**
5057
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
24 * @file h261enc.c
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
25 * H.261 encoder.
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
26 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
27
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
28 #include "dsputil.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
29 #include "avcodec.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
30 #include "mpegvideo.h"
5158
65b1bfb3a74c move definition of MB_TYPE_H261_FIL to h261.h so h261.c doesn't
mru
parents: 5057
diff changeset
31 #include "h261.h"
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
32 #include "h261data.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
33
5057
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
34 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
4668
1f1a0e67b961 kill av_mallocz_static() calls in init_rl()
michael
parents: 4206
diff changeset
35
5057
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
36 static void h261_encode_block(H261Context * h, DCTELEM * block,
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
37 int n);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
38
4206
be94e97ecb6e throw error if unsupported resolution is used with h261
bcoudurier
parents: 3947
diff changeset
39 int ff_h261_get_picture_format(int width, int height){
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
40 // QCIF
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
41 if (width == 176 && height == 144)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
42 return 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
43 // CIF
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
44 else if (width == 352 && height == 288)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
45 return 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
46 // ERROR
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
47 else
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
48 return -1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
49 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
50
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
51 void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
52 H261Context * h = (H261Context *) s;
2328
7b5353eb0dd8 cleanup
michael
parents: 2327
diff changeset
53 int format, temp_ref;
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
54
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
55 align_put_bits(&s->pb);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
56
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
57 /* Update the pointer to last GOB */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
58 s->ptr_lastgob = pbBufPtr(&s->pb);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
59
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
60 put_bits(&s->pb, 20, 0x10); /* PSC */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
61
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
62 temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
2637
ef44d24680d1 switch to native time bases
michael
parents: 2632
diff changeset
63 (1001 * (int64_t)s->avctx->time_base.den); //FIXME maybe this should use a timestamp
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
64 put_bits(&s->pb, 5, temp_ref & 0x1f); /* TemporalReference */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
65
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
66 put_bits(&s->pb, 1, 0); /* split screen off */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
67 put_bits(&s->pb, 1, 0); /* camera off */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
68 put_bits(&s->pb, 1, 0); /* freeze picture release off */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
69
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
70 format = ff_h261_get_picture_format(s->width, s->height);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
71
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
72 put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
73
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
74 put_bits(&s->pb, 1, 0); /* still image mode */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
75 put_bits(&s->pb, 1, 0); /* reserved */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
76
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
77 put_bits(&s->pb, 1, 0); /* no PEI */
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
78 if(format == 0)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
79 h->gob_number = -1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
80 else
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
81 h->gob_number = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
82 h->current_mba = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
83 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
84
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
85 /**
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
86 * Encodes a group of blocks header.
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
87 */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
88 static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
89 H261Context * h = (H261Context *)s;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
90 if(ff_h261_get_picture_format(s->width, s->height) == 0){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
91 h->gob_number+=2; // QCIF
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
92 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
93 else{
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
94 h->gob_number++; // CIF
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
95 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
96 put_bits(&s->pb, 16, 1); /* GBSC */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
97 put_bits(&s->pb, 4, h->gob_number); /* GN */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
98 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
99 put_bits(&s->pb, 1, 0); /* no GEI */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
100 h->current_mba = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
101 h->previous_mba = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
102 h->current_mv_x=0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
103 h->current_mv_y=0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
104 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
105
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
106 void ff_h261_reorder_mb_index(MpegEncContext* s){
2331
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
107 int index= s->mb_x + s->mb_y*s->mb_width;
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
108
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
109 if(index % 33 == 0)
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
110 h261_encode_gob_header(s,0);
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
111
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
112 /* for CIF the GOB's are fragmented in the middle of a scanline
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
113 that's why we need to adjust the x and y index of the macroblocks */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
114 if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
2331
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
115 s->mb_x = index % 11 ; index /= 11;
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
116 s->mb_y = index % 3 ; index /= 3;
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
117 s->mb_x+= 11*(index % 2); index /= 2;
5690ce009ebd simplify ff_h261_reorder_mb_index
michael
parents: 2330
diff changeset
118 s->mb_y+= 3*index;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
119
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
120 ff_init_block_index(s);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
121 ff_update_block_index(s);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
122 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
123 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
124
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
125 static void h261_encode_motion(H261Context * h, int val){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
126 MpegEncContext * const s = &h->s;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
127 int sign, code;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
128 if(val==0){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
129 code = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
130 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
131 }
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
132 else{
2345
ada3891b859d H261 fixing and cleaning:
michael
parents: 2332
diff changeset
133 if(val > 15)
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
134 val -=32;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
135 if(val < -16)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
136 val+=32;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
137 sign = val < 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
138 code = sign ? -val : val;
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
139 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
140 put_bits(&s->pb,1,sign);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
141 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
142 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
143
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
144 static inline int get_cbp(MpegEncContext * s,
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
145 DCTELEM block[6][64])
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
146 {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
147 int i, cbp;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
148 cbp= 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
149 for (i = 0; i < 6; i++) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
150 if (s->block_last_index[i] >= 0)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
151 cbp |= 1 << (5 - i);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
152 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
153 return cbp;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
154 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
155 void ff_h261_encode_mb(MpegEncContext * s,
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
156 DCTELEM block[6][64],
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
157 int motion_x, int motion_y)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
158 {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
159 H261Context * h = (H261Context *)s;
2329
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
160 int mvd, mv_diff_x, mv_diff_y, i, cbp;
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
161 cbp = 63; // avoid warning
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
162 mvd = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
163
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
164 h->current_mba++;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
165 h->mtype = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
166
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
167 if (!s->mb_intra){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
168 /* compute cbp */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
169 cbp= get_cbp(s, block);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
170
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
171 /* mvd indicates if this block is motion compensated */
2329
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
172 mvd = motion_x | motion_y;
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
173
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
174 if((cbp | mvd | s->dquant ) == 0) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
175 /* skip macroblock */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
176 s->skip_count++;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
177 h->current_mv_x=0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
178 h->current_mv_y=0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
179 return;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
180 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
181 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
182
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
183 /* MB is not skipped, encode MBA */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
184 put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
185
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
186 /* calculate MTYPE */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
187 if(!s->mb_intra){
2329
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
188 h->mtype++;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
189
2329
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
190 if(mvd || s->loop_filter)
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
191 h->mtype+=3;
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
192 if(s->loop_filter)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
193 h->mtype+=3;
2329
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
194 if(cbp || s->dquant)
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
195 h->mtype++;
bae9c6de090b fix and cleanup h261 mb_type selection
michael
parents: 2328
diff changeset
196 assert(h->mtype > 1);
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
197 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
198
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
199 if(s->dquant)
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
200 h->mtype++;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
201
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
202 put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
203
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
204 h->mtype = h261_mtype_map[h->mtype];
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
205
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
206 if(IS_QUANT(h->mtype)){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
207 ff_set_qscale(s,s->qscale+s->dquant);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
208 put_bits(&s->pb, 5, s->qscale);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
209 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
210
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
211 if(IS_16X16(h->mtype)){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
212 mv_diff_x = (motion_x >> 1) - h->current_mv_x;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
213 mv_diff_y = (motion_y >> 1) - h->current_mv_y;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
214 h->current_mv_x = (motion_x >> 1);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
215 h->current_mv_y = (motion_y >> 1);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
216 h261_encode_motion(h,mv_diff_x);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
217 h261_encode_motion(h,mv_diff_y);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
218 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
219
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
220 h->previous_mba = h->current_mba;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
221
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
222 if(HAS_CBP(h->mtype)){
3317
e2a44d47dae8 cbp must be >0 here or we are in troubble
michael
parents: 3177
diff changeset
223 assert(cbp>0);
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
224 put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
225 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
226 for(i=0; i<6; i++) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
227 /* encode each block */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
228 h261_encode_block(h, block[i], i);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
229 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
230
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
231 if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
232 h->current_mv_x=0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
233 h->current_mv_y=0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
234 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
235 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
236
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
237 void ff_h261_encode_init(MpegEncContext *s){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
238 static int done = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
239
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
240 if (!done) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
241 done = 1;
5057
5b63f62602fa Split the h261.c file into separate files for the encoder, decoder and common stuff.
takis
parents: 4962
diff changeset
242 init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
243 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
244
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
245 s->min_qcoeff= -127;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
246 s->max_qcoeff= 127;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
247 s->y_dc_scale_table=
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
248 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
249 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
250
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
251
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
252 /**
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
253 * encodes a 8x8 block.
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
254 * @param block the 8x8 block
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
255 * @param n block index (0-3 are luma, 4-5 are chroma)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
256 */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
257 static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
258 MpegEncContext * const s = &h->s;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
259 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
260 RLTable *rl;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
261
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
262 rl = &h261_rl_tcoeff;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
263 if (s->mb_intra) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
264 /* DC coef */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
265 level = block[0];
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
266 /* 255 cannot be represented, so we clamp */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
267 if (level > 254) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
268 level = 254;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
269 block[0] = 254;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
270 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
271 /* 0 cannot be represented also */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
272 else if (level < 1) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
273 level = 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
274 block[0] = 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
275 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
276 if (level == 128)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
277 put_bits(&s->pb, 8, 0xff);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
278 else
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
279 put_bits(&s->pb, 8, level);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
280 i = 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
281 } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
282 //special case
2332
cb3f0954e982 cleanup
michael
parents: 2331
diff changeset
283 put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
284 i = 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
285 } else {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
286 i = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
287 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2792
diff changeset
288
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
289 /* AC coefs */
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
290 last_index = s->block_last_index[n];
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
291 last_non_zero = i - 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
292 for (; i <= last_index; i++) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
293 j = s->intra_scantable.permutated[i];
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
294 level = block[j];
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
295 if (level) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
296 run = i - last_non_zero - 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
297 last = (i == last_index);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
298 sign = 0;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
299 slevel = level;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
300 if (level < 0) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
301 sign = 1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
302 level = -level;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
303 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
304 code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
305 if(run==0 && level < 16)
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
306 code+=1;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
307 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
308 if (code == rl->n) {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
309 put_bits(&s->pb, 6, run);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
310 assert(slevel != 0);
2332
cb3f0954e982 cleanup
michael
parents: 2331
diff changeset
311 assert(level <= 127);
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
312 put_bits(&s->pb, 8, slevel & 0xff);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
313 } else {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
314 put_bits(&s->pb, 1, sign);
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
315 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
316 last_non_zero = i;
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
317 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
318 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
319 if(last_index > -1){
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
320 put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
321 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
322 }
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
323
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
324 AVCodec h261_encoder = {
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
325 "h261",
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
326 CODEC_TYPE_VIDEO,
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
327 CODEC_ID_H261,
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
328 sizeof(H261Context),
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
329 MPV_encode_init,
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
330 MPV_encode_picture,
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
331 MPV_encode_end,
3740
0082fb8c77b6 set supported pixel formats for a few encoders (fixes crashes with grayscale)
michael
parents: 3666
diff changeset
332 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
2327
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
333 };
5e5cf598a48b H.261 encoder by (Maarten Daniels <maarten dot daniels at luc dot ac dot be>)
michael
parents: 2291
diff changeset
334