OSDN Git Service

flvenc: use avcodec_get_name to report unsupported codecs.
[coroid/ffmpeg_saccubus.git] / libavformat / avc.c
1 /*
2  * AVC helper functions for muxers
3  * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "avio.h"
25 #include "avc.h"
26
27 static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
28 {
29     const uint8_t *a = p + 4 - ((intptr_t)p & 3);
30
31     for (end -= 3; p < a && p < end; p++) {
32         if (p[0] == 0 && p[1] == 0 && p[2] == 1)
33             return p;
34     }
35
36     for (end -= 3; p < end; p += 4) {
37         uint32_t x = *(const uint32_t*)p;
38 //      if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
39 //      if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
40         if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
41             if (p[1] == 0) {
42                 if (p[0] == 0 && p[2] == 1)
43                     return p;
44                 if (p[2] == 0 && p[3] == 1)
45                     return p+1;
46             }
47             if (p[3] == 0) {
48                 if (p[2] == 0 && p[4] == 1)
49                     return p+2;
50                 if (p[4] == 0 && p[5] == 1)
51                     return p+3;
52             }
53         }
54     }
55
56     for (end += 3; p < end; p++) {
57         if (p[0] == 0 && p[1] == 0 && p[2] == 1)
58             return p;
59     }
60
61     return end + 3;
62 }
63
64 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
65     const uint8_t *out= ff_avc_find_startcode_internal(p, end);
66     if(p<out && out<end && !out[-1]) out--;
67     return out;
68 }
69
70 int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
71 {
72     const uint8_t *p = buf_in;
73     const uint8_t *end = p + size;
74     const uint8_t *nal_start, *nal_end;
75
76     size = 0;
77     nal_start = ff_avc_find_startcode(p, end);
78     while (nal_start < end) {
79         while(!*(nal_start++));
80         nal_end = ff_avc_find_startcode(nal_start, end);
81         avio_wb32(pb, nal_end - nal_start);
82         avio_write(pb, nal_start, nal_end - nal_start);
83         size += 4 + nal_end - nal_start;
84         nal_start = nal_end;
85     }
86     return size;
87 }
88
89 int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
90 {
91     AVIOContext *pb;
92     int ret = avio_open_dyn_buf(&pb);
93     if(ret < 0)
94         return ret;
95
96     ff_avc_parse_nal_units(pb, buf_in, *size);
97
98     av_freep(buf);
99     *size = avio_close_dyn_buf(pb, buf);
100     return 0;
101 }
102
103 int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
104 {
105     if (len > 6) {
106         /* check for h264 start code */
107         if (AV_RB32(data) == 0x00000001 ||
108             AV_RB24(data) == 0x000001) {
109             uint8_t *buf=NULL, *end, *start;
110             uint32_t sps_size=0, pps_size=0;
111             uint8_t *sps=0, *pps=0;
112
113             int ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
114             if (ret < 0)
115                 return ret;
116             start = buf;
117             end = buf + len;
118
119             /* look for sps and pps */
120             while (buf < end) {
121                 unsigned int size;
122                 uint8_t nal_type;
123                 size = AV_RB32(buf);
124                 nal_type = buf[4] & 0x1f;
125                 if (nal_type == 7) { /* SPS */
126                     sps = buf + 4;
127                     sps_size = size;
128                 } else if (nal_type == 8) { /* PPS */
129                     pps = buf + 4;
130                     pps_size = size;
131                 }
132                 buf += size + 4;
133             }
134             assert(sps);
135             assert(pps);
136
137             avio_w8(pb, 1); /* version */
138             avio_w8(pb, sps[1]); /* profile */
139             avio_w8(pb, sps[2]); /* profile compat */
140             avio_w8(pb, sps[3]); /* level */
141             avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
142             avio_w8(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
143
144             avio_wb16(pb, sps_size);
145             avio_write(pb, sps, sps_size);
146             avio_w8(pb, 1); /* number of pps */
147             avio_wb16(pb, pps_size);
148             avio_write(pb, pps, pps_size);
149             av_free(start);
150         } else {
151             avio_write(pb, data, len);
152         }
153     }
154     return 0;
155 }