annotate h264enc.c @ 7929:5b81784c5fe6 libavcodec

Simplify: avoid duplication backward_filter()
author vitor
date Fri, 26 Sep 2008 18:42:06 +0000
parents de080c5c0960
children 9915843e67e2
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 *
7803
de080c5c0960 license header consistency cosmetics
diego
parents: 6763
diff changeset
4 * This file is part of FFmpeg.
de080c5c0960 license header consistency cosmetics
diego
parents: 6763
diff changeset
5 *
4291
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
6 * FFmpeg is free software; you can redistribute it and/or
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
7 * 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
8 * License as published by the Free Software Foundation; either
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
9 * 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
10 *
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
11 * 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
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
14 * Lesser General Public License for more details.
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
15 *
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
16 * 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
17 * 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
18 * 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
19 */
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
20
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
21
6763
f7cbb7733146 Use full path for #includes from another directory.
diego
parents: 6524
diff changeset
22 #include "libavutil/common.h"
4291
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
23 #include "bitstream.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
24 #include "mpegvideo.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
25 #include "h264data.h"
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
26
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
27 /**
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
28 * Write out the provided data into a NAL unit.
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
29 * @param nal_ref_idc NAL reference IDC
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
30 * @param nal_unit_type NAL unit payload type
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
31 * @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
32 * @param destsize the length of the dst array
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
33 * @param b2 the data which should be escaped
6524
01647ac078a7 typo: occured --> occurred
diego
parents: 4291
diff changeset
34 * @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
35 */
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
36 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
37 PutBitContext *b2)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
38 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
39 PutBitContext b;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
40 int i, destpos, rbsplen, escape_count;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
41 uint8_t *rbsp;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
42
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
43 if (nal_unit_type != NAL_END_STREAM)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
44 put_bits(b2,1,1); // rbsp_stop_bit
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
45
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
46 // Align b2 on a byte boundary
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
47 align_put_bits(b2);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
48 rbsplen = put_bits_count(b2)/8;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
49 flush_put_bits(b2);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
50 rbsp = b2->buf;
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 init_put_bits(&b,dest,*destsize);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
53
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
54 put_bits(&b,16,0);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
55 put_bits(&b,16,0x01);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
56
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
57 put_bits(&b,1,0); // forbidden zero bit
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
58 put_bits(&b,2,nal_ref_idc); // nal_ref_idc
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
59 put_bits(&b,5,nal_unit_type); // nal_unit_type
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 flush_put_bits(&b);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
62
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
63 destpos = 5;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
64 escape_count= 0;
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 for (i=0; i<rbsplen; i+=2)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
67 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
68 if (rbsp[i]) continue;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
69 if (i>0 && rbsp[i-1]==0)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
70 i--;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
71 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
72 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
73 escape_count++;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
74 i+=2;
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 }
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(escape_count==0)
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 if(dest+destpos != rbsp)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
81 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
82 memcpy(dest+destpos, rbsp, rbsplen);
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
83 *destsize -= (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 return dest+rbsplen+destpos;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
86 }
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 if(rbsplen + escape_count + 1> *destsize)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
89 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
90 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
91 return NULL;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
92 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
93
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
94 // this should be damn rare (hopefully)
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
95 for (i = 0 ; i < rbsplen ; i++)
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 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
98 {
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
99 dest[destpos++] = rbsp[i++];
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
100 dest[destpos++] = rbsp[i];
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
101 dest[destpos++] = 0x03; // emulation prevention byte
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
102 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
103 else
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
104 dest[destpos++] = rbsp[i];
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
105 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
106 *destsize -= destpos;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
107 return dest+destpos;
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
108 }
0c090df061f8 Adding function which enables writing H.264 NAL units.
takis
parents:
diff changeset
109