| 1 | /* |
|---|
| 2 | * bitstream_info.h |
|---|
| 3 | * Created by Graham Booker on 1/6/07. |
|---|
| 4 | * |
|---|
| 5 | * This file is part of Perian. |
|---|
| 6 | * |
|---|
| 7 | * This library 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 | * This library 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 "bitstream_info.h" |
|---|
| 23 | |
|---|
| 24 | #include <AudioToolbox/AudioToolbox.h> |
|---|
| 25 | #include <QuickTime/QuickTime.h> |
|---|
| 26 | |
|---|
| 27 | #import "ac3tab.h" |
|---|
| 28 | #import "CommonUtils.h" |
|---|
| 29 | //ffmpeg's struct Picture conflicts with QuickDraw's |
|---|
| 30 | #define Picture MPEGPICTURE |
|---|
| 31 | |
|---|
| 32 | #include "avcodec.h" |
|---|
| 33 | |
|---|
| 34 | #include "bswap.h" |
|---|
| 35 | #include "libavutil/internal.h" |
|---|
| 36 | #include "mpegvideo.h" |
|---|
| 37 | #include "parser.h" |
|---|
| 38 | #include "golomb.h" |
|---|
| 39 | #include "mpeg4video_parser.h" |
|---|
| 40 | #include "Codecprintf.h" |
|---|
| 41 | |
|---|
| 42 | #include "CodecIDs.h" |
|---|
| 43 | |
|---|
| 44 | #undef malloc |
|---|
| 45 | #undef free |
|---|
| 46 | |
|---|
| 47 | static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 }; |
|---|
| 48 | static const int ac3_layout_no_lfe[8] = { |
|---|
| 49 | kAudioChannelLayoutTag_Stereo, |
|---|
| 50 | kAudioChannelLayoutTag_Mono, |
|---|
| 51 | kAudioChannelLayoutTag_Stereo, |
|---|
| 52 | kAudioChannelLayoutTag_ITU_3_0, |
|---|
| 53 | kAudioChannelLayoutTag_ITU_2_1, |
|---|
| 54 | kAudioChannelLayoutTag_ITU_3_1, |
|---|
| 55 | kAudioChannelLayoutTag_ITU_2_2, |
|---|
| 56 | kAudioChannelLayoutTag_ITU_3_2}; |
|---|
| 57 | |
|---|
| 58 | static const int ac3_layout_lfe[8] = { |
|---|
| 59 | kAudioChannelLayoutTag_DVD_4, |
|---|
| 60 | kAudioChannelLayoutTag_Mono, //No layout for this, hopefully we never have it |
|---|
| 61 | kAudioChannelLayoutTag_DVD_4, |
|---|
| 62 | kAudioChannelLayoutTag_DVD_10, |
|---|
| 63 | kAudioChannelLayoutTag_DVD_5, |
|---|
| 64 | kAudioChannelLayoutTag_DVD_11, |
|---|
| 65 | kAudioChannelLayoutTag_DVD_6, |
|---|
| 66 | kAudioChannelLayoutTag_ITU_3_2_1}; |
|---|
| 67 | |
|---|
| 68 | static const uint16_t ac3_freqs[3] = { 48000, 44100, 32000 }; |
|---|
| 69 | static const uint16_t ac3_bitratetab[] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640}; |
|---|
| 70 | static const uint8_t ac3_halfrate[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}; |
|---|
| 71 | |
|---|
| 72 | /* From: http://svn.mplayerhq.hu/ac3/ (LGPL) |
|---|
| 73 | * Synchronize to ac3 bitstream. |
|---|
| 74 | * This function searches for the syncword '0xb77'. |
|---|
| 75 | * |
|---|
| 76 | * @param buf Pointer to "probable" ac3 bitstream buffer |
|---|
| 77 | * @param buf_size Size of buffer |
|---|
| 78 | * @return Returns the position where syncword is found, -1 if no syncword is found |
|---|
| 79 | */ |
|---|
| 80 | static int ac3_synchronize(uint8_t *buf, int buf_size) |
|---|
| 81 | { |
|---|
| 82 | int i; |
|---|
| 83 | |
|---|
| 84 | for (i = 0; i < buf_size - 1; i++) |
|---|
| 85 | if (buf[i] == 0x0b && buf[i + 1] == 0x77) |
|---|
| 86 | return i; |
|---|
| 87 | |
|---|
| 88 | return -1; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /* A lot of this was stolen from: http://svn.mplayerhq.hu/ac3/ (LGPL) |
|---|
| 92 | * Fill info from an ac3 stream |
|---|
| 93 | * |
|---|
| 94 | * @param asdb Pointer to the AudioStreamBasicDescription to fill |
|---|
| 95 | * @param acl Pointer to the AudioChannelLayout to fill |
|---|
| 96 | * @param buffer Pointer to the buffer data to scan |
|---|
| 97 | * @param buff_size Size of the buffer |
|---|
| 98 | * @return 1 if successfull, 0 otherwise |
|---|
| 99 | */ |
|---|
| 100 | |
|---|
| 101 | int parse_ac3_bitstream(AudioStreamBasicDescription *asbd, AudioChannelLayout *acl, uint8_t *buffer, int buff_size) |
|---|
| 102 | { |
|---|
| 103 | int offset = ac3_synchronize(buffer, buff_size); |
|---|
| 104 | if(offset == -1) |
|---|
| 105 | return 0; |
|---|
| 106 | |
|---|
| 107 | if(buff_size < offset + 7) |
|---|
| 108 | return 0; |
|---|
| 109 | |
|---|
| 110 | uint8_t fscod_and_frmsizecod = buffer[offset + 4]; |
|---|
| 111 | |
|---|
| 112 | uint8_t fscod = fscod_and_frmsizecod >> 6; |
|---|
| 113 | uint8_t frmsizecod = fscod_and_frmsizecod & 0x3f; |
|---|
| 114 | if(frmsizecod >= 38) |
|---|
| 115 | return 0; |
|---|
| 116 | |
|---|
| 117 | uint8_t bsid = buffer[offset + 5] >> 3; |
|---|
| 118 | if(bsid >= 0x12) |
|---|
| 119 | return 0; |
|---|
| 120 | |
|---|
| 121 | uint8_t acmod = buffer[offset + 6] >> 5; |
|---|
| 122 | uint8_t shift = 4; |
|---|
| 123 | if(acmod & 0x01 && acmod != 0x01) |
|---|
| 124 | shift -= 2; |
|---|
| 125 | if(acmod & 0x04) |
|---|
| 126 | shift -= 2; |
|---|
| 127 | if(acmod == 0x02) |
|---|
| 128 | shift -= 2; |
|---|
| 129 | uint8_t lfe = (buffer[offset + 6] >> shift) & 0x01; |
|---|
| 130 | |
|---|
| 131 | /* This is a valid frame!!! */ |
|---|
| 132 | uint16_t bitrate = ac3_bitratetab[frmsizecod >> 1]; |
|---|
| 133 | uint8_t half = ac3_halfrate[bsid]; |
|---|
| 134 | int sample_rate = ac3_freqs[fscod] >> half; |
|---|
| 135 | int framesize = 0; |
|---|
| 136 | switch (fscod) { |
|---|
| 137 | case 0: |
|---|
| 138 | framesize = 4 * bitrate; |
|---|
| 139 | break; |
|---|
| 140 | case 1: |
|---|
| 141 | framesize = (320 * bitrate / 147 + (frmsizecod & 1 ? 1 : 0)) * 2; |
|---|
| 142 | break; |
|---|
| 143 | case 2: |
|---|
| 144 | framesize = 6 * bitrate; |
|---|
| 145 | break; |
|---|
| 146 | default: |
|---|
| 147 | break; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | shift = 0; |
|---|
| 151 | if(bsid > 8) |
|---|
| 152 | shift = bsid - 8; |
|---|
| 153 | |
|---|
| 154 | /* Setup the AudioStreamBasicDescription and AudioChannelLayout */ |
|---|
| 155 | memset(asbd, 0, sizeof(AudioStreamBasicDescription)); |
|---|
| 156 | asbd->mSampleRate = sample_rate >> shift; |
|---|
| 157 | asbd->mFormatID = kAudioFormatAC3MS; |
|---|
| 158 | asbd->mChannelsPerFrame = nfchans_tbl[acmod] + lfe; |
|---|
| 159 | |
|---|
| 160 | memset(acl, 0, sizeof(AudioChannelLayout)); |
|---|
| 161 | if(lfe) |
|---|
| 162 | acl->mChannelLayoutTag = ac3_layout_lfe[acmod]; |
|---|
| 163 | else |
|---|
| 164 | acl->mChannelLayoutTag = ac3_layout_no_lfe[acmod]; |
|---|
| 165 | |
|---|
| 166 | return 1; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | static int parse_mpeg4_extra(FFusionParserContext *parser, const uint8_t *buf, int buf_size) |
|---|
| 170 | { |
|---|
| 171 | ParseContext1 *pc1 = (ParseContext1 *)parser->pc->priv_data; |
|---|
| 172 | pc1->pc.frame_start_found = 0; |
|---|
| 173 | |
|---|
| 174 | MpegEncContext *s = pc1->enc; |
|---|
| 175 | GetBitContext gb1, *gb = &gb1; |
|---|
| 176 | |
|---|
| 177 | s->avctx = parser->avctx; |
|---|
| 178 | s->current_picture_ptr = &s->current_picture; |
|---|
| 179 | |
|---|
| 180 | init_get_bits(gb, buf, 8 * buf_size); |
|---|
| 181 | ff_mpeg4_decode_picture_header(s, gb); |
|---|
| 182 | return 1; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /* |
|---|
| 186 | * Long story short, FFMpeg's parsers suck for our use. This function parses an mpeg4 bitstream, |
|---|
| 187 | * and assumes that it is given at least a full frame of data. |
|---|
| 188 | * @param parser A FFusionParserContext structure containg all our info |
|---|
| 189 | * @param buf The buffer to parse |
|---|
| 190 | * @param buf_size Size of the input buffer |
|---|
| 191 | * @param out_buf_size The number of bytes present in the first frame of data |
|---|
| 192 | * @param type The frame Type: FF_*_TYPE |
|---|
| 193 | * @param pts The PTS of the frame |
|---|
| 194 | * @return 1 if a frame is found, 0 otherwise |
|---|
| 195 | */ |
|---|
| 196 | static int parse_mpeg4_stream(FFusionParserContext *parser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped) |
|---|
| 197 | { |
|---|
| 198 | ParseContext1 *pc1 = (ParseContext1 *)parser->pc->priv_data; |
|---|
| 199 | pc1->pc.frame_start_found = 0; |
|---|
| 200 | |
|---|
| 201 | int endOfFrame = ff_mpeg4_find_frame_end(&(pc1->pc), buf, buf_size); |
|---|
| 202 | |
|---|
| 203 | MpegEncContext *s = pc1->enc; |
|---|
| 204 | GetBitContext gb1, *gb = &gb1; |
|---|
| 205 | |
|---|
| 206 | s->avctx = parser->avctx; |
|---|
| 207 | s->current_picture_ptr = &s->current_picture; |
|---|
| 208 | |
|---|
| 209 | init_get_bits(gb, buf, 8 * buf_size); |
|---|
| 210 | int parse_res = ff_mpeg4_decode_picture_header(s, gb); |
|---|
| 211 | if(parse_res == FRAME_SKIPPED) { |
|---|
| 212 | *out_buf_size = buf_size; |
|---|
| 213 | *type = FF_P_TYPE; |
|---|
| 214 | *skippable = 1; |
|---|
| 215 | *skipped = 1; |
|---|
| 216 | } |
|---|
| 217 | if(parse_res != 0) |
|---|
| 218 | return 0; |
|---|
| 219 | |
|---|
| 220 | *type = s->pict_type; |
|---|
| 221 | *skippable = (*type == FF_B_TYPE); |
|---|
| 222 | *skipped = 0; |
|---|
| 223 | #if 0 /*this was an attempt to figure out the PTS information and detect an out of order P frame before we hit its B frame */ |
|---|
| 224 | int64_t *lastPtsPtr = (int64_t *)parser->internalContext; |
|---|
| 225 | int64_t lastPts = *lastPtsPtr; |
|---|
| 226 | int64_t currentPts = s->current_picture.pts; |
|---|
| 227 | |
|---|
| 228 | switch(s->pict_type) |
|---|
| 229 | { |
|---|
| 230 | case FF_I_TYPE: |
|---|
| 231 | *lastPtsPtr = currentPts; |
|---|
| 232 | *precedesAPastFrame = 0; |
|---|
| 233 | break; |
|---|
| 234 | case FF_P_TYPE: |
|---|
| 235 | if(currentPts > lastPts + 1) |
|---|
| 236 | *precedesAPastFrame = 1; |
|---|
| 237 | else |
|---|
| 238 | *precedesAPastFrame = 0; |
|---|
| 239 | *lastPtsPtr = currentPts; |
|---|
| 240 | break; |
|---|
| 241 | case FF_B_TYPE: |
|---|
| 242 | *precedesAPastFrame = 0; |
|---|
| 243 | break; |
|---|
| 244 | default: |
|---|
| 245 | break; |
|---|
| 246 | } |
|---|
| 247 | #endif |
|---|
| 248 | |
|---|
| 249 | if(endOfFrame == END_NOT_FOUND) |
|---|
| 250 | *out_buf_size = buf_size; |
|---|
| 251 | else |
|---|
| 252 | *out_buf_size = endOfFrame; |
|---|
| 253 | return 1; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | static int parse_mpeg12_stream(FFusionParserContext *ffparser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped) |
|---|
| 257 | { |
|---|
| 258 | const uint8_t *out_unused; |
|---|
| 259 | int size_unused; |
|---|
| 260 | AVCodecParser *parser = ffparser->pc->parser; |
|---|
| 261 | |
|---|
| 262 | parser->parser_parse(ffparser->pc, ffparser->avctx, &out_unused, &size_unused, buf, buf_size); |
|---|
| 263 | |
|---|
| 264 | *out_buf_size = buf_size; |
|---|
| 265 | *type = ffparser->pc->pict_type; |
|---|
| 266 | *skippable = *type == FF_B_TYPE; |
|---|
| 267 | *skipped = 0; |
|---|
| 268 | |
|---|
| 269 | return 1; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | extern AVCodecParser mpeg4video_parser; |
|---|
| 273 | |
|---|
| 274 | static FFusionParser ffusionMpeg4VideoParser = { |
|---|
| 275 | &mpeg4video_parser, |
|---|
| 276 | 0, |
|---|
| 277 | NULL, |
|---|
| 278 | parse_mpeg4_extra, |
|---|
| 279 | parse_mpeg4_stream, |
|---|
| 280 | }; |
|---|
| 281 | |
|---|
| 282 | extern AVCodecParser mpegvideo_parser; |
|---|
| 283 | |
|---|
| 284 | static FFusionParser ffusionMpeg12VideoParser = { |
|---|
| 285 | &mpegvideo_parser, |
|---|
| 286 | 0, |
|---|
| 287 | NULL, |
|---|
| 288 | NULL, |
|---|
| 289 | parse_mpeg12_stream, |
|---|
| 290 | }; |
|---|
| 291 | |
|---|
| 292 | typedef struct H264ParserContext_s |
|---|
| 293 | { |
|---|
| 294 | int is_avc; |
|---|
| 295 | int nal_length_size; |
|---|
| 296 | int prevPts; |
|---|
| 297 | |
|---|
| 298 | int profile_idc; |
|---|
| 299 | int level_idc; |
|---|
| 300 | |
|---|
| 301 | int poc_type; |
|---|
| 302 | int log2_max_frame_num; |
|---|
| 303 | int frame_mbs_only_flag; |
|---|
| 304 | int num_reorder_frames; |
|---|
| 305 | int pic_order_present_flag; |
|---|
| 306 | |
|---|
| 307 | int log2_max_poc_lsb; |
|---|
| 308 | int poc_msb; |
|---|
| 309 | int prev_poc_lsb; |
|---|
| 310 | |
|---|
| 311 | int delta_pic_order_always_zero_flag; |
|---|
| 312 | int offset_for_non_ref_pic; |
|---|
| 313 | int num_ref_frames_in_pic_order_cnt_cycle; |
|---|
| 314 | int sum_of_offset_for_ref_frames; |
|---|
| 315 | |
|---|
| 316 | int chroma_format_idc; |
|---|
| 317 | |
|---|
| 318 | int gaps_in_frame_num_value_allowed_flag; |
|---|
| 319 | }H264ParserContext; |
|---|
| 320 | |
|---|
| 321 | static int decode_nal(const uint8_t *buf, int buf_size, uint8_t *out_buf, int *out_buf_size, int *type, int *nal_ref_idc) |
|---|
| 322 | { |
|---|
| 323 | int i; |
|---|
| 324 | int outIndex = 0; |
|---|
| 325 | int numNulls = 0; |
|---|
| 326 | |
|---|
| 327 | for(i=1; i<buf_size; i++) |
|---|
| 328 | { |
|---|
| 329 | if(buf[i] == 0) |
|---|
| 330 | numNulls++; |
|---|
| 331 | else if(numNulls == 2) |
|---|
| 332 | { |
|---|
| 333 | numNulls = 0; |
|---|
| 334 | if(buf[i] < 3) |
|---|
| 335 | { |
|---|
| 336 | /* end of nal */ |
|---|
| 337 | outIndex -= 2; |
|---|
| 338 | break; |
|---|
| 339 | } |
|---|
| 340 | else if(buf[i] == 3) |
|---|
| 341 | /* This is just a simple escape of 0x00 00 03 */ |
|---|
| 342 | continue; |
|---|
| 343 | } |
|---|
| 344 | out_buf[outIndex] = buf[i]; |
|---|
| 345 | outIndex++; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | if(outIndex <= 0) |
|---|
| 349 | return 0; |
|---|
| 350 | |
|---|
| 351 | *type = buf[0] & 0x1f; |
|---|
| 352 | *nal_ref_idc = (buf[0] >> 5) & 0x03; |
|---|
| 353 | *out_buf_size = outIndex; |
|---|
| 354 | return 1; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | static void skip_scaling_list(GetBitContext *gb, int size){ |
|---|
| 358 | int i, next = 8, last = 8; |
|---|
| 359 | |
|---|
| 360 | if(get_bits1(gb)) /* matrix not written, we use the predicted one */ |
|---|
| 361 | for(i=0;i<size;i++){ |
|---|
| 362 | if(next) |
|---|
| 363 | next = (last + get_se_golomb(gb)) & 0xff; |
|---|
| 364 | if(!i && !next){ /* matrix not written, we use the preset one */ |
|---|
| 365 | break; |
|---|
| 366 | } |
|---|
| 367 | last = next ? next : last; |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | static void skip_scaling_matrices(GetBitContext *gb){ |
|---|
| 372 | if(get_bits1(gb)){ |
|---|
| 373 | skip_scaling_list(gb, 16); // Intra, Y |
|---|
| 374 | skip_scaling_list(gb, 16); // Intra, Cr |
|---|
| 375 | skip_scaling_list(gb, 16); // Intra, Cb |
|---|
| 376 | skip_scaling_list(gb, 16); // Inter, Y |
|---|
| 377 | skip_scaling_list(gb, 16); // Inter, Cr |
|---|
| 378 | skip_scaling_list(gb, 16); // Inter, Cb |
|---|
| 379 | skip_scaling_list(gb, 64); // Intra, Y |
|---|
| 380 | skip_scaling_list(gb, 64); // Inter, Y |
|---|
| 381 | } |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | static void skip_hrd_parameters(GetBitContext *gb) |
|---|
| 385 | { |
|---|
| 386 | int cpb_cnt_minus1 = get_ue_golomb(gb); |
|---|
| 387 | get_bits(gb, 4); //bit_rate_scale |
|---|
| 388 | get_bits(gb, 4); //cpb_size_scale |
|---|
| 389 | int i; |
|---|
| 390 | for(i=0; i<=cpb_cnt_minus1; i++) |
|---|
| 391 | { |
|---|
| 392 | get_ue_golomb(gb); //bit_rate_value_minus1[i] |
|---|
| 393 | get_ue_golomb(gb); //cpb_size_value_minus1[i] |
|---|
| 394 | get_bits1(gb); //cbr_flag[i] |
|---|
| 395 | } |
|---|
| 396 | get_bits(gb, 5); //initial_cpb_removal_delay_length_minus1 |
|---|
| 397 | get_bits(gb, 5); //cpb_removal_delay_length_minus1 |
|---|
| 398 | get_bits(gb, 5); //dpb_output_delay_length_minus1 |
|---|
| 399 | get_bits(gb, 5); //time_offset_length |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | static void decode_sps(H264ParserContext *context, const uint8_t *buf, int buf_size) |
|---|
| 403 | { |
|---|
| 404 | GetBitContext getbit, *gb = &getbit; |
|---|
| 405 | |
|---|
| 406 | init_get_bits(gb, buf, 8 * buf_size); |
|---|
| 407 | context->profile_idc= get_bits(gb, 8); |
|---|
| 408 | get_bits1(gb); //constraint_set0_flag |
|---|
| 409 | get_bits1(gb); //constraint_set1_flag |
|---|
| 410 | get_bits1(gb); //constraint_set2_flag |
|---|
| 411 | get_bits1(gb); //constraint_set3_flag |
|---|
| 412 | get_bits(gb, 4); //reserved |
|---|
| 413 | context->level_idc = get_bits(gb, 8); //level_idc |
|---|
| 414 | get_ue_golomb(gb); //seq_parameter_set_id |
|---|
| 415 | if(context->profile_idc >= 100) |
|---|
| 416 | { |
|---|
| 417 | context->chroma_format_idc = get_ue_golomb(gb); |
|---|
| 418 | //high profile |
|---|
| 419 | if(context->chroma_format_idc == 3) //chroma_format_idc |
|---|
| 420 | get_bits1(gb); //residual_color_transfrom_flag |
|---|
| 421 | get_ue_golomb(gb); //bit_depth_luma_minus8 |
|---|
| 422 | get_ue_golomb(gb); //bit_depth_chroma_minus8 |
|---|
| 423 | get_bits1(gb); //transform_bypass |
|---|
| 424 | skip_scaling_matrices(gb); |
|---|
| 425 | } |
|---|
| 426 | context->log2_max_frame_num = get_ue_golomb(gb) + 4; |
|---|
| 427 | context->poc_type = get_ue_golomb(gb); |
|---|
| 428 | if(context->poc_type == 0) |
|---|
| 429 | context->log2_max_poc_lsb = get_ue_golomb(gb) + 4; |
|---|
| 430 | else if(context->poc_type == 1) |
|---|
| 431 | { |
|---|
| 432 | int i; |
|---|
| 433 | |
|---|
| 434 | context->delta_pic_order_always_zero_flag = get_bits1(gb); |
|---|
| 435 | context->offset_for_non_ref_pic = get_se_golomb(gb); |
|---|
| 436 | get_se_golomb(gb); //offset_for_top_to_bottom_field |
|---|
| 437 | context->num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(gb); |
|---|
| 438 | context->sum_of_offset_for_ref_frames = 0; |
|---|
| 439 | for(i=0; i<context->num_ref_frames_in_pic_order_cnt_cycle; i++) |
|---|
| 440 | context->sum_of_offset_for_ref_frames += get_se_golomb(gb); //offset_for_ref_frame[i] |
|---|
| 441 | } |
|---|
| 442 | get_ue_golomb(gb); //num_ref_frames |
|---|
| 443 | context->gaps_in_frame_num_value_allowed_flag = get_bits1(gb); //gaps_in_frame_num_value_allowed_flag |
|---|
| 444 | get_ue_golomb(gb); //pic_width_in_mbs_minus1 |
|---|
| 445 | get_ue_golomb(gb); //pic_height_in_map_units_minus1 |
|---|
| 446 | int mbs_only = get_bits1(gb); |
|---|
| 447 | context->frame_mbs_only_flag = mbs_only; |
|---|
| 448 | #if 1 //This is a test to get num_reorder_frames |
|---|
| 449 | if(!mbs_only) |
|---|
| 450 | get_bits1(gb); //mb_adaptive_frame_field_flag |
|---|
| 451 | get_bits1(gb); //direct_8x8_inference_flag |
|---|
| 452 | if(get_bits1(gb)) //frame_cropping_flag |
|---|
| 453 | { |
|---|
| 454 | get_ue_golomb(gb); //frame_crop_left_offset |
|---|
| 455 | get_ue_golomb(gb); //frame_crop_right_offset |
|---|
| 456 | get_ue_golomb(gb); //frame_crop_top_offset |
|---|
| 457 | get_ue_golomb(gb); //frame_crop_bottom_offset |
|---|
| 458 | } |
|---|
| 459 | if(get_bits1(gb)) //vui_parameters_present_flag |
|---|
| 460 | { |
|---|
| 461 | if(get_bits1(gb)) //aspect_ratio_info_present_flag |
|---|
| 462 | { |
|---|
| 463 | if(get_bits(gb, 8) == 255) //aspect_ratio_idc |
|---|
| 464 | { |
|---|
| 465 | get_bits(gb, 16); //sar_width |
|---|
| 466 | get_bits(gb, 16); //sar_height |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | if(get_bits1(gb)) //overscan_info_present_flag |
|---|
| 470 | get_bits1(gb); //overscan_appropriate_flag |
|---|
| 471 | if(get_bits1(gb)) //video_signal_type_present_flag |
|---|
| 472 | { |
|---|
| 473 | get_bits(gb, 3); //video_format |
|---|
| 474 | get_bits1(gb); //video_full_range_flag |
|---|
| 475 | if(get_bits1(gb)) //colour_description_present_flag |
|---|
| 476 | { |
|---|
| 477 | get_bits(gb, 8); //colour_primaries |
|---|
| 478 | get_bits(gb, 8); //transfer_characteristics |
|---|
| 479 | get_bits(gb, 8); //matrix_coefficients |
|---|
| 480 | } |
|---|
| 481 | } |
|---|
| 482 | if(get_bits1(gb)) //chroma_loc_info_present_flag |
|---|
| 483 | { |
|---|
| 484 | get_ue_golomb(gb); //chroma_sample_loc_type_top_field |
|---|
| 485 | get_ue_golomb(gb); //chroma_sample_loc_type_bottom_field |
|---|
| 486 | } |
|---|
| 487 | if(get_bits1(gb)) //timing_info_present_flag |
|---|
| 488 | { |
|---|
| 489 | get_bits(gb, 32); //num_units_in_tick |
|---|
| 490 | get_bits(gb, 32); //time_scale |
|---|
| 491 | get_bits1(gb); //fixed_frame_rate_flag |
|---|
| 492 | } |
|---|
| 493 | int nal_hrd_parameters_present_flag = get_bits1(gb); |
|---|
| 494 | if(nal_hrd_parameters_present_flag) |
|---|
| 495 | { |
|---|
| 496 | skip_hrd_parameters(gb); |
|---|
| 497 | } |
|---|
| 498 | int vcl_hrd_parameters_present_flag = get_bits1(gb); |
|---|
| 499 | if(vcl_hrd_parameters_present_flag) |
|---|
| 500 | { |
|---|
| 501 | skip_hrd_parameters(gb); |
|---|
| 502 | } |
|---|
| 503 | if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) |
|---|
| 504 | get_bits1(gb); //low_delay_hrd_flag |
|---|
| 505 | get_bits1(gb); //pic_struct_present_flag |
|---|
| 506 | if(get_bits1(gb)) //bitstream_restriction_flag |
|---|
| 507 | { |
|---|
| 508 | get_bits1(gb); //motion_vectors_over_pic_boundaries_flag |
|---|
| 509 | get_ue_golomb(gb); //max_bytes_per_pic_denom |
|---|
| 510 | get_ue_golomb(gb); //max_bits_per_mb_denom |
|---|
| 511 | get_ue_golomb(gb); //log2_max_mv_length_horizontal |
|---|
| 512 | get_ue_golomb(gb); //log2_max_mv_length_vertical |
|---|
| 513 | context->num_reorder_frames = get_ue_golomb(gb); |
|---|
| 514 | get_ue_golomb(gb); //max_dec_frame_buffering |
|---|
| 515 | } |
|---|
| 516 | } |
|---|
| 517 | #endif |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | static void decode_pps(H264ParserContext *context, const uint8_t *buf, int buf_size) |
|---|
| 521 | { |
|---|
| 522 | GetBitContext getbit, *gb = &getbit; |
|---|
| 523 | |
|---|
| 524 | init_get_bits(gb, buf, 8 * buf_size); |
|---|
| 525 | get_ue_golomb(gb); //pic_parameter_set_id |
|---|
| 526 | get_ue_golomb(gb); //seq_parameter_set_id |
|---|
| 527 | get_bits1(gb); //entropy_coding_mode_flag |
|---|
| 528 | context->pic_order_present_flag = get_bits1(gb); |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | static int inline decode_slice_header(H264ParserContext *context, const uint8_t *buf, int buf_size, int nal_ref_idc, int nal_type, int just_type, int *type, int *pts) |
|---|
| 532 | { |
|---|
| 533 | GetBitContext getbit, *gb = &getbit; |
|---|
| 534 | int slice_type; |
|---|
| 535 | int field_pic_flag = 0; |
|---|
| 536 | int bottom_field_flag = 0; |
|---|
| 537 | int frame_number; |
|---|
| 538 | // static const uint8_t slice_type_map[5] = {FF_P_TYPE, FF_B_TYPE, FF_I_TYPE, FF_SP_TYPE, FF_SI_TYPE}; |
|---|
| 539 | static const uint8_t slice_type_map[5] = {FF_P_TYPE, FF_P_TYPE, FF_I_TYPE, FF_SP_TYPE, FF_SI_TYPE}; |
|---|
| 540 | |
|---|
| 541 | init_get_bits(gb, buf, 8 * buf_size); |
|---|
| 542 | |
|---|
| 543 | get_ue_golomb(gb); //first_mb_in_slice |
|---|
| 544 | slice_type = get_ue_golomb(gb); |
|---|
| 545 | if(slice_type > 9) |
|---|
| 546 | return 0; |
|---|
| 547 | |
|---|
| 548 | if(slice_type > 4) |
|---|
| 549 | slice_type -= 5; |
|---|
| 550 | |
|---|
| 551 | *type = slice_type_map[slice_type]; |
|---|
| 552 | if(just_type) |
|---|
| 553 | return 1; |
|---|
| 554 | |
|---|
| 555 | get_ue_golomb(gb); //pic_parameter_set_id |
|---|
| 556 | frame_number = get_bits(gb, context->log2_max_frame_num); |
|---|
| 557 | if(!context->frame_mbs_only_flag) |
|---|
| 558 | { |
|---|
| 559 | field_pic_flag = get_bits1(gb); |
|---|
| 560 | if(field_pic_flag) |
|---|
| 561 | { |
|---|
| 562 | bottom_field_flag = get_bits1(gb); |
|---|
| 563 | } |
|---|
| 564 | } |
|---|
| 565 | if(nal_type == 5) |
|---|
| 566 | get_ue_golomb(gb); //idr_pic_id |
|---|
| 567 | if(context->poc_type == 0) |
|---|
| 568 | { |
|---|
| 569 | int pts_lsb = get_bits(gb, context->log2_max_poc_lsb); |
|---|
| 570 | int delta_pic_order_cnt_bottom = 0; |
|---|
| 571 | int maxPicOrderCntLsb = 1 << context->log2_max_poc_lsb; |
|---|
| 572 | int pic_order_msb; |
|---|
| 573 | |
|---|
| 574 | if(context->pic_order_present_flag && !field_pic_flag) |
|---|
| 575 | delta_pic_order_cnt_bottom = get_se_golomb(gb); |
|---|
| 576 | if((pts_lsb < context->prev_poc_lsb) && (context->prev_poc_lsb - pts_lsb) >= maxPicOrderCntLsb) |
|---|
| 577 | pic_order_msb = context->poc_msb + maxPicOrderCntLsb; |
|---|
| 578 | else if((pts_lsb > context->prev_poc_lsb) && (pts_lsb - context->prev_poc_lsb) > maxPicOrderCntLsb) |
|---|
| 579 | pic_order_msb = context->poc_msb - maxPicOrderCntLsb; |
|---|
| 580 | else |
|---|
| 581 | pic_order_msb = context->poc_msb; |
|---|
| 582 | |
|---|
| 583 | context->poc_msb = pic_order_msb; |
|---|
| 584 | |
|---|
| 585 | *pts = pic_order_msb + pts_lsb; |
|---|
| 586 | if(delta_pic_order_cnt_bottom < 0) |
|---|
| 587 | *pts += delta_pic_order_cnt_bottom; |
|---|
| 588 | |
|---|
| 589 | } |
|---|
| 590 | else if(context->poc_type == 1 && !context->delta_pic_order_always_zero_flag) |
|---|
| 591 | { |
|---|
| 592 | int delta_pic_order_cnt[2] = {0, 0}; |
|---|
| 593 | delta_pic_order_cnt[0] = get_se_golomb(gb); |
|---|
| 594 | if(context->pic_order_present_flag && !field_pic_flag) |
|---|
| 595 | delta_pic_order_cnt[1] = get_se_golomb(gb); |
|---|
| 596 | |
|---|
| 597 | int frame_num_offset = 0; //I think this is wrong, but the pts code isn't used anywhere, so no harm yet and this removes a warning. |
|---|
| 598 | |
|---|
| 599 | int abs_frame_num = 0; |
|---|
| 600 | int num_ref_frames_in_pic_order_cnt_cycle = context->num_ref_frames_in_pic_order_cnt_cycle; |
|---|
| 601 | if(num_ref_frames_in_pic_order_cnt_cycle != 0) |
|---|
| 602 | abs_frame_num = frame_num_offset + frame_number; |
|---|
| 603 | |
|---|
| 604 | if(nal_ref_idc == 0 && abs_frame_num > 0) |
|---|
| 605 | abs_frame_num--; |
|---|
| 606 | |
|---|
| 607 | int expected_delta_per_poc_cycle = context->sum_of_offset_for_ref_frames; |
|---|
| 608 | int expectedpoc = 0; |
|---|
| 609 | if(abs_frame_num > 0) |
|---|
| 610 | { |
|---|
| 611 | int poc_cycle_cnt = (abs_frame_num - 1) / num_ref_frames_in_pic_order_cnt_cycle; |
|---|
| 612 | |
|---|
| 613 | expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle + context->sum_of_offset_for_ref_frames; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | if(nal_ref_idc == 0) |
|---|
| 617 | expectedpoc = expectedpoc + context->offset_for_non_ref_pic; |
|---|
| 618 | *pts = expectedpoc + delta_pic_order_cnt[0]; |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | return 1; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | #define NAL_PEEK_SIZE 32 |
|---|
| 625 | |
|---|
| 626 | static int inline decode_nals(H264ParserContext *context, const uint8_t *buf, int buf_size, int *type, int *skippable) |
|---|
| 627 | { |
|---|
| 628 | int nalsize = 0; |
|---|
| 629 | int buf_index = 0; |
|---|
| 630 | int ret = 0; |
|---|
| 631 | int pts_decoded = 0; |
|---|
| 632 | int lowestType = 20; |
|---|
| 633 | |
|---|
| 634 | *skippable = 1; |
|---|
| 635 | |
|---|
| 636 | #if 0 /*this was an attempt to figure out the PTS information and detect an out of order P frame before we hit its B frame */ |
|---|
| 637 | if(context->poc_type == 2) |
|---|
| 638 | { |
|---|
| 639 | //decode and display order are the same |
|---|
| 640 | pts_decoded = 1; |
|---|
| 641 | *precedesAPastFrame = 0; |
|---|
| 642 | } |
|---|
| 643 | #endif |
|---|
| 644 | |
|---|
| 645 | for(;;) |
|---|
| 646 | { |
|---|
| 647 | if(context->is_avc) |
|---|
| 648 | { |
|---|
| 649 | if(buf_index >= buf_size) |
|---|
| 650 | break; |
|---|
| 651 | nalsize = 0; |
|---|
| 652 | switch (context->nal_length_size) { |
|---|
| 653 | case 1: |
|---|
| 654 | nalsize = buf[buf_index]; |
|---|
| 655 | buf_index++; |
|---|
| 656 | break; |
|---|
| 657 | case 2: |
|---|
| 658 | nalsize = (buf[buf_index] << 8) | buf[buf_index+1]; |
|---|
| 659 | buf_index += 2; |
|---|
| 660 | break; |
|---|
| 661 | case 3: |
|---|
| 662 | nalsize = (buf[buf_index] << 16) | (buf[buf_index+1] << 8) | buf[buf_index + 2]; |
|---|
| 663 | buf_index += 3; |
|---|
| 664 | break; |
|---|
| 665 | case 4: |
|---|
| 666 | nalsize = (buf[buf_index] << 24) | (buf[buf_index+1] << 16) | (buf[buf_index + 2] << 8) | buf[buf_index + 3]; |
|---|
| 667 | buf_index += 4; |
|---|
| 668 | break; |
|---|
| 669 | default: |
|---|
| 670 | break; |
|---|
| 671 | } |
|---|
| 672 | if(nalsize <= 1 || nalsize > buf_size) |
|---|
| 673 | { |
|---|
| 674 | if(nalsize == 1) |
|---|
| 675 | { |
|---|
| 676 | buf_index++; |
|---|
| 677 | continue; |
|---|
| 678 | } |
|---|
| 679 | else |
|---|
| 680 | break; |
|---|
| 681 | } |
|---|
| 682 | } |
|---|
| 683 | else |
|---|
| 684 | { |
|---|
| 685 | int num_nuls = 0; |
|---|
| 686 | int start_offset = 0; |
|---|
| 687 | //do start code prefix search |
|---|
| 688 | for(; buf_index < buf_size; buf_index++) |
|---|
| 689 | { |
|---|
| 690 | if(buf[buf_index] == 0) |
|---|
| 691 | num_nuls++; |
|---|
| 692 | else |
|---|
| 693 | { |
|---|
| 694 | if(num_nuls >= 2 && buf[buf_index] == 1) |
|---|
| 695 | break; |
|---|
| 696 | num_nuls = 0; |
|---|
| 697 | } |
|---|
| 698 | } |
|---|
| 699 | start_offset = buf_index + 1; |
|---|
| 700 | //do start code prefix search |
|---|
| 701 | for(buf_index++; buf_index < buf_size; buf_index++) |
|---|
| 702 | { |
|---|
| 703 | if(buf[buf_index] == 0) |
|---|
| 704 | { |
|---|
| 705 | if(num_nuls == 2) |
|---|
| 706 | break; |
|---|
| 707 | num_nuls++; |
|---|
| 708 | } |
|---|
| 709 | else |
|---|
| 710 | { |
|---|
| 711 | if(num_nuls == 2 && buf[buf_index] == 1) |
|---|
| 712 | break; |
|---|
| 713 | num_nuls = 0; |
|---|
| 714 | } |
|---|
| 715 | } |
|---|
| 716 | if(start_offset >= buf_size) |
|---|
| 717 | //no more |
|---|
| 718 | break; |
|---|
| 719 | nalsize = buf_index - start_offset; |
|---|
| 720 | if(buf_index < buf_size) |
|---|
| 721 | //Take off the next NAL's startcode |
|---|
| 722 | nalsize -= 2; |
|---|
| 723 | //skip the start code |
|---|
| 724 | buf_index = start_offset; |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | uint8_t partOfNal[NAL_PEEK_SIZE]; |
|---|
| 728 | int decodedNalSize, nalType; |
|---|
| 729 | int nal_ref_idc; |
|---|
| 730 | int slice_type = 0; |
|---|
| 731 | |
|---|
| 732 | if(decode_nal(buf + buf_index, FFMIN(nalsize, NAL_PEEK_SIZE), partOfNal, &decodedNalSize, &nalType, &nal_ref_idc)) |
|---|
| 733 | { |
|---|
| 734 | int pts = 0; |
|---|
| 735 | if(nalType == 1 || nalType == 2) |
|---|
| 736 | { |
|---|
| 737 | if(decode_slice_header(context, partOfNal, decodedNalSize, nal_ref_idc, nalType, pts_decoded, &slice_type, &pts)) |
|---|
| 738 | { |
|---|
| 739 | ret = 1; |
|---|
| 740 | if(slice_type < lowestType) |
|---|
| 741 | lowestType = slice_type; |
|---|
| 742 | if(nal_ref_idc) |
|---|
| 743 | *skippable = 0; |
|---|
| 744 | if(pts_decoded == 0) |
|---|
| 745 | { |
|---|
| 746 | pts_decoded = 1; |
|---|
| 747 | if(pts > context->prevPts) |
|---|
| 748 | { |
|---|
| 749 | if(pts < context->prevPts) |
|---|
| 750 | lowestType = FF_B_TYPE; |
|---|
| 751 | context->prevPts = pts; |
|---|
| 752 | } |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | // Parser users assume I-frames are IDR-frames |
|---|
| 757 | // but in H.264 they don't have to be. |
|---|
| 758 | // Mark these as P-frames if they effectively are. |
|---|
| 759 | if (lowestType == FF_I_TYPE) lowestType = FF_P_TYPE; |
|---|
| 760 | } |
|---|
| 761 | else if(nalType == 5) |
|---|
| 762 | { |
|---|
| 763 | ret = 1; |
|---|
| 764 | #if 0 /*this was an attempt to figure out the PTS information and detect an out of order P frame before we hit its B frame */ |
|---|
| 765 | context->prev_poc_lsb = 0; |
|---|
| 766 | context->poc_msb = 0; |
|---|
| 767 | context->prevPts = 0; |
|---|
| 768 | *precedesAPastFrame = 0; |
|---|
| 769 | #endif |
|---|
| 770 | *skippable = 0; |
|---|
| 771 | lowestType = FF_I_TYPE; |
|---|
| 772 | } |
|---|
| 773 | } |
|---|
| 774 | buf_index += nalsize; |
|---|
| 775 | } |
|---|
| 776 | if(lowestType != 20) |
|---|
| 777 | *type = lowestType; |
|---|
| 778 | |
|---|
| 779 | return ret; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | /* |
|---|
| 783 | * This function parses an h.264 bitstream, and assumes that it is given at least a full frame of data. |
|---|
| 784 | * @param parser A FFusionParserContext structure containg all our info |
|---|
| 785 | * @param buf The buffer to parse |
|---|
| 786 | * @param buf_size Size of the input buffer |
|---|
| 787 | * @param out_buf_size The number of bytes present in the first frame of data |
|---|
| 788 | * @param type The frame Type: FF_*_TYPE |
|---|
| 789 | * @param pts The PTS of the frame |
|---|
| 790 | * @return 1 if a frame is found, 0 otherwise |
|---|
| 791 | */ |
|---|
| 792 | static int parse_h264_stream(FFusionParserContext *parser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped) |
|---|
| 793 | { |
|---|
| 794 | int endOfFrame; |
|---|
| 795 | int size = 0; |
|---|
| 796 | const uint8_t *parseBuf = buf; |
|---|
| 797 | int parseSize; |
|---|
| 798 | |
|---|
| 799 | /* |
|---|
| 800 | * Somehow figure out of frame type |
|---|
| 801 | * For our use, a frame with any B slices is a B frame, and then a frame with any P slices is a P frame. |
|---|
| 802 | * An I frame has only I slices. |
|---|
| 803 | * I expect this involves a NAL decoder, and then look at the slice types. |
|---|
| 804 | * Nal is a f(1) always set to 0, u(2) of nal_ref_idc, and then u(5) of nal_unit_type. |
|---|
| 805 | * Nal types 1, 2 start a non-I frame, and type 5 starts an I frame. Each start with a slice header. |
|---|
| 806 | * Slice header has a ue(v) for first_mb_in_slice and then a ue(v) for the slice_type |
|---|
| 807 | * Slice types 0, 5 are P, 1, 6 are B, 2, 7 are I |
|---|
| 808 | */ |
|---|
| 809 | |
|---|
| 810 | do |
|---|
| 811 | { |
|---|
| 812 | parseBuf = parseBuf + size; |
|---|
| 813 | parseSize = buf_size - size; |
|---|
| 814 | endOfFrame = (parser->parserStructure->avparse->split)(parser->avctx, parseBuf, parseSize); |
|---|
| 815 | if(endOfFrame == 0) |
|---|
| 816 | size = buf_size; |
|---|
| 817 | else |
|---|
| 818 | { |
|---|
| 819 | size += endOfFrame; |
|---|
| 820 | parseSize = endOfFrame; |
|---|
| 821 | } |
|---|
| 822 | }while(decode_nals(parser->internalContext, parseBuf, parseSize, type, skippable) == 0 && size < buf_size); |
|---|
| 823 | |
|---|
| 824 | *skipped = 0; |
|---|
| 825 | *out_buf_size = size; |
|---|
| 826 | return 1; |
|---|
| 827 | } |
|---|
| 828 | |
|---|
| 829 | static int init_h264_parser(FFusionParserContext *parser) |
|---|
| 830 | { |
|---|
| 831 | H264ParserContext *context = parser->internalContext; |
|---|
| 832 | |
|---|
| 833 | context->nal_length_size = 2; |
|---|
| 834 | context->is_avc = 0; |
|---|
| 835 | return 1; |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | static int parse_extra_data_h264(FFusionParserContext *parser, const uint8_t *buf, int buf_size) |
|---|
| 839 | { |
|---|
| 840 | H264ParserContext *context = parser->internalContext; |
|---|
| 841 | const uint8_t *cur = buf; |
|---|
| 842 | int count, i, type, ref; |
|---|
| 843 | |
|---|
| 844 | context->is_avc = 1; |
|---|
| 845 | count = *(cur+5) & 0x1f; |
|---|
| 846 | cur += 6; |
|---|
| 847 | for (i=0; i<count; i++) |
|---|
| 848 | { |
|---|
| 849 | int size = AV_RB16(cur); |
|---|
| 850 | int out_size = 0; |
|---|
| 851 | uint8_t *decoded = av_mallocz(size+FF_INPUT_BUFFER_PADDING_SIZE); |
|---|
| 852 | if(decode_nal(cur + 2, size, decoded, &out_size, &type, &ref)) |
|---|
| 853 | decode_sps(context, decoded, out_size); |
|---|
| 854 | cur += size + 2; |
|---|
| 855 | free(decoded); |
|---|
| 856 | } |
|---|
| 857 | count = *(cur++); |
|---|
| 858 | for (i=0; i<count; i++) |
|---|
| 859 | { |
|---|
| 860 | int size = AV_RB16(cur); |
|---|
| 861 | int out_size = 0; |
|---|
| 862 | uint8_t *decoded = av_mallocz(size+FF_INPUT_BUFFER_PADDING_SIZE); |
|---|
| 863 | if(decode_nal(cur + 2, size, decoded, &out_size, &type, &ref)) |
|---|
| 864 | decode_pps(context, decoded, out_size); |
|---|
| 865 | cur += size + 2; |
|---|
| 866 | free(decoded); |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | context->nal_length_size = ((*(buf+4)) & 0x03) + 1; |
|---|
| 870 | |
|---|
| 871 | return 1; |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | extern AVCodecParser h264_parser; |
|---|
| 875 | |
|---|
| 876 | static FFusionParser ffusionH264Parser = { |
|---|
| 877 | &h264_parser, |
|---|
| 878 | sizeof(H264ParserContext), |
|---|
| 879 | init_h264_parser, |
|---|
| 880 | parse_extra_data_h264, |
|---|
| 881 | parse_h264_stream, |
|---|
| 882 | }; |
|---|
| 883 | |
|---|
| 884 | static FFusionParser *ffusionFirstParser = NULL; |
|---|
| 885 | |
|---|
| 886 | void registerFFusionParsers(FFusionParser *parser) |
|---|
| 887 | { |
|---|
| 888 | parser->next = ffusionFirstParser; |
|---|
| 889 | ffusionFirstParser = parser; |
|---|
| 890 | } |
|---|
| 891 | |
|---|
| 892 | void initFFusionParsers() |
|---|
| 893 | { |
|---|
| 894 | static Boolean inited = FALSE; |
|---|
| 895 | int unlock = PerianInitEnter(&inited); |
|---|
| 896 | |
|---|
| 897 | if(!inited) |
|---|
| 898 | { |
|---|
| 899 | inited = TRUE; |
|---|
| 900 | registerFFusionParsers(&ffusionMpeg4VideoParser); |
|---|
| 901 | registerFFusionParsers(&ffusionH264Parser); |
|---|
| 902 | registerFFusionParsers(&ffusionMpeg12VideoParser); |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | PerianInitExit(unlock); |
|---|
| 906 | } |
|---|
| 907 | |
|---|
| 908 | void ffusionParserFree(FFusionParserContext *parser) |
|---|
| 909 | { |
|---|
| 910 | AVCodecParser *avparse = parser->parserStructure->avparse; |
|---|
| 911 | |
|---|
| 912 | if(parser->pc) |
|---|
| 913 | { |
|---|
| 914 | if (avparse->parser_close) |
|---|
| 915 | avparse->parser_close(parser->pc); |
|---|
| 916 | av_free(parser->pc->priv_data); |
|---|
| 917 | av_free(parser->pc); |
|---|
| 918 | } |
|---|
| 919 | av_free(parser->avctx); |
|---|
| 920 | av_free(parser->internalContext); |
|---|
| 921 | free(parser); |
|---|
| 922 | } |
|---|
| 923 | |
|---|
| 924 | FFusionParserContext *ffusionParserInit(int codec_id) |
|---|
| 925 | { |
|---|
| 926 | AVCodecParserContext *s; |
|---|
| 927 | AVCodecParser *parser; |
|---|
| 928 | FFusionParser *ffParser; |
|---|
| 929 | int ret, i; |
|---|
| 930 | |
|---|
| 931 | if(codec_id == CODEC_ID_NONE) |
|---|
| 932 | return NULL; |
|---|
| 933 | |
|---|
| 934 | if (!ffusionFirstParser) initFFusionParsers(); |
|---|
| 935 | |
|---|
| 936 | for(ffParser = ffusionFirstParser; ffParser != NULL; ffParser = ffParser->next) { |
|---|
| 937 | parser = ffParser->avparse; |
|---|
| 938 | |
|---|
| 939 | for (i = 0; i < 5; i++) |
|---|
| 940 | if (parser->codec_ids[i] == codec_id) |
|---|
| 941 | goto found; |
|---|
| 942 | } |
|---|
| 943 | return NULL; |
|---|
| 944 | found: |
|---|
| 945 | s = av_mallocz(sizeof(AVCodecParserContext)); |
|---|
| 946 | if (!s) |
|---|
| 947 | return NULL; |
|---|
| 948 | s->parser = parser; |
|---|
| 949 | s->priv_data = av_mallocz(parser->priv_data_size); |
|---|
| 950 | if (!s->priv_data) { |
|---|
| 951 | av_free(s); |
|---|
| 952 | return NULL; |
|---|
| 953 | } |
|---|
| 954 | if (parser->parser_init) { |
|---|
| 955 | ret = parser->parser_init(s); |
|---|
| 956 | if (ret != 0) { |
|---|
| 957 | av_free(s->priv_data); |
|---|
| 958 | av_free(s); |
|---|
| 959 | return NULL; |
|---|
| 960 | } |
|---|
| 961 | } |
|---|
| 962 | s->fetch_timestamp=1; |
|---|
| 963 | s->flags |= PARSER_FLAG_COMPLETE_FRAMES; |
|---|
| 964 | |
|---|
| 965 | FFusionParserContext *parserContext = malloc(sizeof(FFusionParserContext)); |
|---|
| 966 | parserContext->avctx = avcodec_alloc_context(); |
|---|
| 967 | parserContext->pc = s; |
|---|
| 968 | parserContext->parserStructure = ffParser; |
|---|
| 969 | if(ffParser->internalContextSize) |
|---|
| 970 | parserContext->internalContext = av_mallocz(ffParser->internalContextSize); |
|---|
| 971 | else |
|---|
| 972 | parserContext->internalContext = NULL; |
|---|
| 973 | if(ffParser->init) |
|---|
| 974 | (ffParser->init)(parserContext); |
|---|
| 975 | return parserContext; |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| 978 | /* |
|---|
| 979 | * @param parser FFusionParserContext pointer |
|---|
| 980 | * @param buf The buffer to parse |
|---|
| 981 | * @param buf_size Size of the input buffer |
|---|
| 982 | * @return 1 if successful, 0 otherwise |
|---|
| 983 | */ |
|---|
| 984 | int ffusionParseExtraData(FFusionParserContext *parser, const uint8_t *buf, int buf_size) |
|---|
| 985 | { |
|---|
| 986 | if(parser->parserStructure->extra_data) |
|---|
| 987 | return (parser->parserStructure->extra_data)(parser, buf, buf_size); |
|---|
| 988 | return 1; |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | /* |
|---|
| 992 | * @param parser FFusionParserContext pointer |
|---|
| 993 | * @param buf The buffer to parse |
|---|
| 994 | * @param buf_size Size of the input buffer |
|---|
| 995 | * @param out_buf_size The number of bytes present in the first frame of data |
|---|
| 996 | * @param type The frame Type: FF_*_TYPE |
|---|
| 997 | * @param skippable If nothing depends on the frame |
|---|
| 998 | * @param skipped If the frame is an N-vop or equivalent |
|---|
| 999 | * @return 1 if a frame is found, 0 otherwise |
|---|
| 1000 | */ |
|---|
| 1001 | int ffusionParse(FFusionParserContext *parser, const uint8_t *buf, int buf_size, int *out_buf_size, int *type, int *skippable, int *skipped) |
|---|
| 1002 | { |
|---|
| 1003 | if(parser->parserStructure->parser_parse) |
|---|
| 1004 | return (parser->parserStructure->parser_parse)(parser, buf, buf_size, out_buf_size, type, skippable, skipped); |
|---|
| 1005 | return 0; |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | void ffusionLogDebugInfo(FFusionParserContext *parser, FILE *log) |
|---|
| 1009 | { |
|---|
| 1010 | if (parser) { |
|---|
| 1011 | if (parser->parserStructure == &ffusionH264Parser) { |
|---|
| 1012 | H264ParserContext *h264parser = parser->internalContext; |
|---|
| 1013 | |
|---|
| 1014 | Codecprintf(log, "H.264 format: profile %d level %d\n\tis_avc %d\n\tframe_mbs_only %d\n\tchroma_format_idc %d\n\tframe_num_gaps %d\n\tnum_reorder_frames %d\n", |
|---|
| 1015 | h264parser->profile_idc, h264parser->level_idc, h264parser->is_avc, h264parser->frame_mbs_only_flag, h264parser->chroma_format_idc, h264parser->gaps_in_frame_num_value_allowed_flag, |
|---|
| 1016 | h264parser->num_reorder_frames); |
|---|
| 1017 | } |
|---|
| 1018 | } |
|---|
| 1019 | } |
|---|
| 1020 | |
|---|
| 1021 | FFusionDecodeAbilities ffusionIsParsedVideoDecodable(FFusionParserContext *parser) |
|---|
| 1022 | { |
|---|
| 1023 | if (!parser) return FFUSION_PREFER_DECODE; |
|---|
| 1024 | |
|---|
| 1025 | if (parser->parserStructure == &ffusionH264Parser) { |
|---|
| 1026 | H264ParserContext *h264parser = parser->internalContext; |
|---|
| 1027 | FFusionDecodeAbilities ret = FFUSION_PREFER_DECODE; |
|---|
| 1028 | |
|---|
| 1029 | //QT is bad at high profile |
|---|
| 1030 | //and x264 B-pyramid (sps.vui.num_reorder_frames > 1) |
|---|
| 1031 | //FIXME x264 will generate pyramid differently soon (core 78+), and that might work better |
|---|
| 1032 | if(h264parser->profile_idc < 100 && h264parser->num_reorder_frames < 2 && |
|---|
| 1033 | !CFPreferencesGetAppBooleanValue(CFSTR("DecodeAllProfiles"), PERIAN_PREF_DOMAIN, NULL)) |
|---|
| 1034 | ret = FFUSION_PREFER_NOT_DECODE; |
|---|
| 1035 | |
|---|
| 1036 | //PAFF/MBAFF |
|---|
| 1037 | //ffmpeg is ok at this now but we can't test it (not enough AVCHD samples) |
|---|
| 1038 | //and the quicktime api for it may or may not actually exist |
|---|
| 1039 | if(!h264parser->frame_mbs_only_flag) |
|---|
| 1040 | ret = FFUSION_CANNOT_DECODE; |
|---|
| 1041 | |
|---|
| 1042 | //4:2:2 chroma |
|---|
| 1043 | if(h264parser->chroma_format_idc > 1) |
|---|
| 1044 | ret = FFUSION_CANNOT_DECODE; |
|---|
| 1045 | |
|---|
| 1046 | return ret; |
|---|
| 1047 | } |
|---|
| 1048 | |
|---|
| 1049 | return FFUSION_PREFER_DECODE; |
|---|
| 1050 | } |
|---|
| 1051 | #ifdef DEBUG_BUILD |
|---|
| 1052 | //FFMPEG doesn't configure properly (their fault), so define their idoticly undefined symbols. |
|---|
| 1053 | int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2], int ref_mv_scale, int size, int h){return 0;} |
|---|
| 1054 | int ff_get_mb_score(MpegEncContext * s, int mx, int my, int src_index,int ref_index, int size, int h, int add_rate){return 0;} |
|---|
| 1055 | int ff_init_me(MpegEncContext *s){return 0;} |
|---|
| 1056 | int ff_rate_control_init(struct MpegEncContext *s){return 0;} |
|---|
| 1057 | float ff_rate_estimate_qscale(struct MpegEncContext *s, int dry_run){return 0;} |
|---|
| 1058 | void ff_write_pass1_stats(struct MpegEncContext *s){} |
|---|
| 1059 | void h263_encode_init(MpegEncContext *s){} |
|---|
| 1060 | #endif |
|---|