annotate h264enc.c @ 6624:2dc587201e38 libavcodec

Change k limiting code, i think the code was buggy. If you have ALAC files TEST them! Mine produce the same md5 but the new code is not identical if limiting does happen.
author michael
date Thu, 17 Apr 2008 03:00:08 +0000
parents 01647ac078a7
children f7cbb7733146
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4291
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
1 /*
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
2 * H.264 encoder
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
3 *
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
4 * FFmpeg is free software; you can redistribute it and/or
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
5 * modify it under the terms of the GNU Lesser General Public
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
6 * License as published by the Free Software Foundation; either
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
7 * version 2.1 of the License, or (at your option) any later version.
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
8 *
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
9 * FFmpeg is distributed in the hope that it will be useful,
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
12 * Lesser General Public License for more details.
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
13 *
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
14 * You should have received a copy of the GNU Lesser General Public
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
15 * License along with FFmpeg; if not, write to the Free Software
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
17 */
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
18
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
19
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
20 #include "common.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
21 #include "bitstream.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
22 #include "mpegvideo.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
23 #include "h264data.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
24
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
25 /**
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
26 * Write out the provided data into a NAL unit.
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
27 * @param nal_ref_idc NAL reference IDC
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
28 * @param nal_unit_type NAL unit payload type
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
29 * @param dest the target buffer, dst+1 == src is allowed as a special case
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
30 * @param destsize the length of the dst array
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
31 * @param b2 the data which should be escaped
6524
01647ac078a7 typo: occured --> occurred
diego
parents: 4291
diff changeset
32 * @returns pointer to current position in the output buffer or NULL if an error occurred
4291
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
33 */
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
34 static uint8_t *h264_write_nal_unit(int nal_ref_idc, int nal_unit_type, uint8_t *dest, int *destsize,
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
35 PutBitContext *b2)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
36 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
37 PutBitContext b;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
38 int i, destpos, rbsplen, escape_count;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
39 uint8_t *rbsp;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
40
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
41 if (nal_unit_type != NAL_END_STREAM)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
42 put_bits(b2,1,1); // rbsp_stop_bit
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
43
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
44 // Align b2 on a byte boundary
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
45 align_put_bits(b2);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
46 rbsplen = put_bits_count(b2)/8;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
47 flush_put_bits(b2);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
48 rbsp = b2->buf;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
49
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
50 init_put_bits(&b,dest,*destsize);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
51
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
52 put_bits(&b,16,0);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
53 put_bits(&b,16,0x01);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
54
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
55 put_bits(&b,1,0); // forbidden zero bit
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
56 put_bits(&b,2,nal_ref_idc); // nal_ref_idc
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
57 put_bits(&b,5,nal_unit_type); // nal_unit_type
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
58
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
59 flush_put_bits(&b);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
60
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
61 destpos = 5;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
62 escape_count= 0;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
63
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
64 for (i=0; i<rbsplen; i+=2)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
65 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
66 if (rbsp[i]) continue;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
67 if (i>0 && rbsp[i-1]==0)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
68 i--;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
69 if (i+2<rbsplen && rbsp[i+1]==0 && rbsp[i+2]<=3)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
70 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
71 escape_count++;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
72 i+=2;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
73 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
74 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
75
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
76 if(escape_count==0)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
77 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
78 if(dest+destpos != rbsp)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
79 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
80 memcpy(dest+destpos, rbsp, rbsplen);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
81 *destsize -= (rbsplen+destpos);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
82 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
83 return dest+rbsplen+destpos;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
84 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
85
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
86 if(rbsplen + escape_count + 1> *destsize)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
87 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
88 av_log(NULL, AV_LOG_ERROR, "Destination buffer too small!\n");
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
89 return NULL;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
90 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
91
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
92 // this should be damn rare (hopefully)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
93 for (i = 0 ; i < rbsplen ; i++)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
94 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
95 if (i + 2 < rbsplen && (rbsp[i] == 0 && rbsp[i+1] == 0 && rbsp[i+2] < 4))
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
96 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
97 dest[destpos++] = rbsp[i++];
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
98 dest[destpos++] = rbsp[i];
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
99 dest[destpos++] = 0x03; // emulation prevention byte
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
100 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
101 else
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
102 dest[destpos++] = rbsp[i];
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
103 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
104 *destsize -= destpos;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
105 return dest+destpos;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
106 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
107