| OLD | NEW |
| 1 /* | 1 /* |
| 2 * AVC helper functions for muxers | 2 * AVC helper functions for muxers |
| 3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com> | 3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com> |
| 4 * | 4 * |
| 5 * This file is part of FFmpeg. | 5 * This file is part of FFmpeg. |
| 6 * | 6 * |
| 7 * FFmpeg is free software; you can redistribute it and/or | 7 * FFmpeg is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Lesser General Public | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2.1 of the License, or (at your option) any later version. | 10 * version 2.1 of the License, or (at your option) any later version. |
| 11 * | 11 * |
| 12 * FFmpeg is distributed in the hope that it will be useful, | 12 * FFmpeg is distributed in the hope that it will be useful, |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 * Lesser General Public License for more details. | 15 * Lesser General Public License for more details. |
| 16 * | 16 * |
| 17 * You should have received a copy of the GNU Lesser General Public | 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 | 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 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 #include "libavutil/intreadwrite.h" | 22 #include "libavutil/intreadwrite.h" |
| 23 #include "avformat.h" | 23 #include "avformat.h" |
| 24 #include "avio.h" | 24 #include "avio.h" |
| 25 #include "avc.h" |
| 25 | 26 |
| 26 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end) | 27 static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uin
t8_t *end) |
| 27 { | 28 { |
| 28 const uint8_t *a = p + 4 - ((intptr_t)p & 3); | 29 const uint8_t *a = p + 4 - ((intptr_t)p & 3); |
| 29 | 30 |
| 30 for (end -= 3; p < a && p < end; p++) { | 31 for (end -= 3; p < a && p < end; p++) { |
| 31 if (p[0] == 0 && p[1] == 0 && p[2] == 1) | 32 if (p[0] == 0 && p[1] == 0 && p[2] == 1) |
| 32 return p; | 33 return p; |
| 33 } | 34 } |
| 34 | 35 |
| 35 for (end -= 3; p < end; p += 4) { | 36 for (end -= 3; p < end; p += 4) { |
| 36 uint32_t x = *(const uint32_t*)p; | 37 uint32_t x = *(const uint32_t*)p; |
| 37 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian | 38 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian |
| 38 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian | 39 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian |
| 39 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic | 40 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic |
| 40 if (p[1] == 0) { | 41 if (p[1] == 0) { |
| 41 if (p[0] == 0 && p[2] == 1) | 42 if (p[0] == 0 && p[2] == 1) |
| 42 return p-1; | 43 return p; |
| 43 if (p[2] == 0 && p[3] == 1) | 44 if (p[2] == 0 && p[3] == 1) |
| 44 return p; | 45 return p+1; |
| 45 } | 46 } |
| 46 if (p[3] == 0) { | 47 if (p[3] == 0) { |
| 47 if (p[2] == 0 && p[4] == 1) | 48 if (p[2] == 0 && p[4] == 1) |
| 48 return p+1; | 49 return p+2; |
| 49 if (p[4] == 0 && p[5] == 1) | 50 if (p[4] == 0 && p[5] == 1) |
| 50 return p+2; | 51 return p+3; |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 for (end += 3; p < end; p++) { | 56 for (end += 3; p < end; p++) { |
| 56 if (p[0] == 0 && p[1] == 0 && p[2] == 1) | 57 if (p[0] == 0 && p[1] == 0 && p[2] == 1) |
| 57 return p; | 58 return p; |
| 58 } | 59 } |
| 59 | 60 |
| 60 return end + 3; | 61 return end + 3; |
| 61 } | 62 } |
| 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 |
| 63 int ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size) | 70 int ff_avc_parse_nal_units(ByteIOContext *pb, const uint8_t *buf_in, int size) |
| 64 { | 71 { |
| 65 const uint8_t *p = buf_in; | 72 const uint8_t *p = buf_in; |
| 66 const uint8_t *end = p + size; | 73 const uint8_t *end = p + size; |
| 67 const uint8_t *nal_start, *nal_end; | 74 const uint8_t *nal_start, *nal_end; |
| 68 | 75 |
| 69 size = 0; | 76 size = 0; |
| 70 nal_start = ff_avc_find_startcode(p, end); | 77 nal_start = ff_avc_find_startcode(p, end); |
| 71 while (nal_start < end) { | 78 while (nal_start < end) { |
| 72 while(!*(nal_start++)); | 79 while(!*(nal_start++)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 put_byte(pb, 1); /* number of pps */ | 146 put_byte(pb, 1); /* number of pps */ |
| 140 put_be16(pb, pps_size); | 147 put_be16(pb, pps_size); |
| 141 put_buffer(pb, pps, pps_size); | 148 put_buffer(pb, pps, pps_size); |
| 142 av_free(start); | 149 av_free(start); |
| 143 } else { | 150 } else { |
| 144 put_buffer(pb, data, len); | 151 put_buffer(pb, data, len); |
| 145 } | 152 } |
| 146 } | 153 } |
| 147 return 0; | 154 return 0; |
| 148 } | 155 } |
| OLD | NEW |