libavcodec/mlpdec.c:951: error: Null Dereference
  pointer `&gb->buffer` last assigned on line 929 could be null and is dereferenced by call to `get_bits1()` at line 951, column 29.

libavcodec/mlpdec.c:906:1: start of procedure read_access_unit()
904.  *  otherwise returns the number of bytes consumed. */
905. 
906. static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
     ^
907.                             AVPacket *avpkt)
908. {

libavcodec/mlpdec.c:909:5: 
907.                             AVPacket *avpkt)
908. {
909.     const uint8_t *buf = avpkt->data;
         ^
910.     int buf_size = avpkt->size;
911.     MLPDecodeContext *m = avctx->priv_data;

libavcodec/mlpdec.c:910:5: 
908. {
909.     const uint8_t *buf = avpkt->data;
910.     int buf_size = avpkt->size;
         ^
911.     MLPDecodeContext *m = avctx->priv_data;
912.     GetBitContext gb;

libavcodec/mlpdec.c:911:5: 
909.     const uint8_t *buf = avpkt->data;
910.     int buf_size = avpkt->size;
911.     MLPDecodeContext *m = avctx->priv_data;
         ^
912.     GetBitContext gb;
913.     unsigned int length, substr;

libavcodec/mlpdec.c:915:5: 
913.     unsigned int length, substr;
914.     unsigned int substream_start;
915.     unsigned int header_size = 4;
         ^
916.     unsigned int substr_header_size = 0;
917.     uint8_t substream_parity_present[MAX_SUBSTREAMS];

libavcodec/mlpdec.c:916:5: 
914.     unsigned int substream_start;
915.     unsigned int header_size = 4;
916.     unsigned int substr_header_size = 0;
         ^
917.     uint8_t substream_parity_present[MAX_SUBSTREAMS];
918.     uint16_t substream_data_len[MAX_SUBSTREAMS];

libavcodec/mlpdec.c:921:9: Taking false branch
919.     uint8_t parity_bits;
920. 
921.     if (buf_size < 4)
             ^
922.         return 0;
923. 

libavcodec/mlpdec.c:924:5: 
922.         return 0;
923. 
924.     length = (AV_RB16(buf) & 0xfff) * 2;
         ^
925. 
926.     if (length > buf_size)

libavutil/bswap.h:44:1: start of procedure bswap_16()
  42. 
  43. #ifndef bswap_16
  44. static av_always_inline av_const uint16_t bswap_16(uint16_t x)
      ^
  45. {
  46.     x= (x>>8) | (x<<8);

libavutil/bswap.h:46:5: 
  44. static av_always_inline av_const uint16_t bswap_16(uint16_t x)
  45. {
  46.     x= (x>>8) | (x<<8);
          ^
  47.     return x;
  48. }

libavutil/bswap.h:47:5: 
  45. {
  46.     x= (x>>8) | (x<<8);
  47.     return x;
          ^
  48. }
  49. #endif

libavutil/bswap.h:48:1: return from a call to bswap_16
  46.     x= (x>>8) | (x<<8);
  47.     return x;
  48. }
      ^
  49. #endif
  50. 

libavcodec/mlpdec.c:926:9: Taking false branch
924.     length = (AV_RB16(buf) & 0xfff) * 2;
925. 
926.     if (length > buf_size)
             ^
927.         return -1;
928. 

libavcodec/mlpdec.c:929:5: 
927.         return -1;
928. 
929.     init_get_bits(&gb, (buf + 4), (length - 4) * 8);
         ^
930. 
931.     m->is_major_sync_unit = 0;

libavcodec/get_bits.h:493:1: start of procedure init_get_bits()
  491.  * @param bit_size the size of the buffer in bits
  492.  */
  493. static inline void init_get_bits(GetBitContext *s,
       ^
  494.                    const uint8_t *buffer, int bit_size)
  495. {

libavcodec/get_bits.h:496:5: 
  494.                    const uint8_t *buffer, int bit_size)
  495. {
  496.     int buffer_size= (bit_size+7)>>3;
           ^
  497.     if(buffer_size < 0 || bit_size < 0) {
  498.         buffer_size = bit_size = 0;

libavcodec/get_bits.h:497:8: Taking true branch
  495. {
  496.     int buffer_size= (bit_size+7)>>3;
  497.     if(buffer_size < 0 || bit_size < 0) {
              ^
  498.         buffer_size = bit_size = 0;
  499.         buffer = NULL;

libavcodec/get_bits.h:498:9: 
  496.     int buffer_size= (bit_size+7)>>3;
  497.     if(buffer_size < 0 || bit_size < 0) {
  498.         buffer_size = bit_size = 0;
               ^
  499.         buffer = NULL;
  500.     }

libavcodec/get_bits.h:499:9: 
  497.     if(buffer_size < 0 || bit_size < 0) {
  498.         buffer_size = bit_size = 0;
  499.         buffer = NULL;
               ^
  500.     }
  501. 

libavcodec/get_bits.h:502:5: 
  500.     }
  501. 
  502.     s->buffer= buffer;
           ^
  503.     s->size_in_bits= bit_size;
  504.     s->buffer_end= buffer + buffer_size;

libavcodec/get_bits.h:503:5: 
  501. 
  502.     s->buffer= buffer;
  503.     s->size_in_bits= bit_size;
           ^
  504.     s->buffer_end= buffer + buffer_size;
  505. #ifdef ALT_BITSTREAM_READER

libavcodec/get_bits.h:504:5: 
  502.     s->buffer= buffer;
  503.     s->size_in_bits= bit_size;
  504.     s->buffer_end= buffer + buffer_size;
           ^
  505. #ifdef ALT_BITSTREAM_READER
  506.     s->index=0;

libavcodec/get_bits.h:506:5: 
  504.     s->buffer_end= buffer + buffer_size;
  505. #ifdef ALT_BITSTREAM_READER
  506.     s->index=0;
           ^
  507. #elif defined LIBMPEG2_BITSTREAM_READER
  508.     s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));

libavcodec/get_bits.h:516:1: return from a call to init_get_bits
  514.     skip_bits_long(s, 0);
  515. #endif
  516. }
       ^
  517. 
  518. static inline void align_get_bits(GetBitContext *s)

libavcodec/mlpdec.c:931:5: 
929.     init_get_bits(&gb, (buf + 4), (length - 4) * 8);
930. 
931.     m->is_major_sync_unit = 0;
         ^
932.     if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
933.         if (read_major_sync(m, &gb) < 0)

libavcodec/mlpdec.c:932:9: 
930. 
931.     m->is_major_sync_unit = 0;
932.     if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
             ^
933.         if (read_major_sync(m, &gb) < 0)
934.             goto error;

libavcodec/get_bits.h:470:1: start of procedure show_bits_long()
  468.  * shows 0-32 bits.
  469.  */
  470. static inline unsigned int show_bits_long(GetBitContext *s, int n){
       ^
  471.     if(n<=17) return show_bits(s, n);
  472.     else{

libavcodec/get_bits.h:471:8: Taking false branch
  469.  */
  470. static inline unsigned int show_bits_long(GetBitContext *s, int n){
  471.     if(n<=17) return show_bits(s, n);
              ^
  472.     else{
  473.         GetBitContext gb= *s;

libavcodec/get_bits.h:473:9: 
  471.     if(n<=17) return show_bits(s, n);
  472.     else{
  473.         GetBitContext gb= *s;
               ^
  474.         return get_bits_long(&gb, n);
  475.     }

libavcodec/get_bits.h:474:9: 
  472.     else{
  473.         GetBitContext gb= *s;
  474.         return get_bits_long(&gb, n);
               ^
  475.     }
  476. }

libavcodec/get_bits.h:447:1: start of procedure get_bits_long()
    445.  * reads 0-32 bits.
    446.  */
    447. static inline unsigned int get_bits_long(GetBitContext *s, int n){
         ^
    448.     if(n<=17) return get_bits(s, n);
    449.     else{

libavcodec/get_bits.h:448:8: Taking false branch
    446.  */
    447. static inline unsigned int get_bits_long(GetBitContext *s, int n){
    448.     if(n<=17) return get_bits(s, n);
                ^
    449.     else{
    450. #ifdef ALT_BITSTREAM_READER_LE

libavcodec/get_bits.h:454:9: 
    452.         return ret | (get_bits(s, n-16) << 16);
    453. #else
    454.         int ret= get_bits(s, 16) << (n-16);
                 ^
    455.         return ret | get_bits(s, n-16);
    456. #endif

libavcodec/get_bits.h:385:1: start of procedure get_bits()
      383.  * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
      384.  */
      385. static inline unsigned int get_bits(GetBitContext *s, int n){
           ^
      386.     register int tmp;
      387.     OPEN_READER(re, s)

libavcodec/get_bits.h:387:5: 
      385. static inline unsigned int get_bits(GetBitContext *s, int n){
      386.     register int tmp;
      387.     OPEN_READER(re, s)
               ^
      388.     UPDATE_CACHE(re, s)
      389.     tmp= SHOW_UBITS(re, s, n);

libavcodec/get_bits.h:388:5: 
      386.     register int tmp;
      387.     OPEN_READER(re, s)
      388.     UPDATE_CACHE(re, s)
               ^
      389.     tmp= SHOW_UBITS(re, s, n);
      390.     LAST_SKIP_BITS(re, s, n)

libavutil/bswap.h:52:1: start of procedure bswap_32()
        50. 
        51. #ifndef bswap_32
        52. static av_always_inline av_const uint32_t bswap_32(uint32_t x)
            ^
        53. {
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);

libavutil/bswap.h:54:5: 
        52. static av_always_inline av_const uint32_t bswap_32(uint32_t x)
        53. {
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
                ^
        55.     x= (x>>16) | (x<<16);
        56.     return x;

libavutil/bswap.h:55:5: 
        53. {
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
        55.     x= (x>>16) | (x<<16);
                ^
        56.     return x;
        57. }

libavutil/bswap.h:56:5: 
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
        55.     x= (x>>16) | (x<<16);
        56.     return x;
                ^
        57. }
        58. #endif

libavutil/bswap.h:57:1: return from a call to bswap_32
        55.     x= (x>>16) | (x<<16);
        56.     return x;
        57. }
            ^
        58. #endif
        59. 

libavcodec/get_bits.h:389:5: 
      387.     OPEN_READER(re, s)
      388.     UPDATE_CACHE(re, s)
      389.     tmp= SHOW_UBITS(re, s, n);
               ^
      390.     LAST_SKIP_BITS(re, s, n)
      391.     CLOSE_READER(re, s)

libavcodec/get_bits.h:390:5: 
      388.     UPDATE_CACHE(re, s)
      389.     tmp= SHOW_UBITS(re, s, n);
      390.     LAST_SKIP_BITS(re, s, n)
               ^
      391.     CLOSE_READER(re, s)
      392.     return tmp;

libavcodec/get_bits.h:391:5: 
      389.     tmp= SHOW_UBITS(re, s, n);
      390.     LAST_SKIP_BITS(re, s, n)
      391.     CLOSE_READER(re, s)
               ^
      392.     return tmp;
      393. }

libavcodec/get_bits.h:392:5: 
      390.     LAST_SKIP_BITS(re, s, n)
      391.     CLOSE_READER(re, s)
      392.     return tmp;
               ^
      393. }
      394. 

libavcodec/get_bits.h:393:1: return from a call to get_bits
      391.     CLOSE_READER(re, s)
      392.     return tmp;
      393. }
           ^
      394. 
      395. /**

libavcodec/get_bits.h:455:9: 
    453. #else
    454.         int ret= get_bits(s, 16) << (n-16);
    455.         return ret | get_bits(s, n-16);
                 ^
    456. #endif
    457.     }

libavcodec/get_bits.h:385:1: start of procedure get_bits()
      383.  * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
      384.  */
      385. static inline unsigned int get_bits(GetBitContext *s, int n){
           ^
      386.     register int tmp;
      387.     OPEN_READER(re, s)

libavcodec/get_bits.h:387:5: 
      385. static inline unsigned int get_bits(GetBitContext *s, int n){
      386.     register int tmp;
      387.     OPEN_READER(re, s)
               ^
      388.     UPDATE_CACHE(re, s)
      389.     tmp= SHOW_UBITS(re, s, n);

libavcodec/get_bits.h:388:5: 
      386.     register int tmp;
      387.     OPEN_READER(re, s)
      388.     UPDATE_CACHE(re, s)
               ^
      389.     tmp= SHOW_UBITS(re, s, n);
      390.     LAST_SKIP_BITS(re, s, n)

libavutil/bswap.h:52:1: start of procedure bswap_32()
        50. 
        51. #ifndef bswap_32
        52. static av_always_inline av_const uint32_t bswap_32(uint32_t x)
            ^
        53. {
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);

libavutil/bswap.h:54:5: 
        52. static av_always_inline av_const uint32_t bswap_32(uint32_t x)
        53. {
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
                ^
        55.     x= (x>>16) | (x<<16);
        56.     return x;

libavutil/bswap.h:55:5: 
        53. {
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
        55.     x= (x>>16) | (x<<16);
                ^
        56.     return x;
        57. }

libavutil/bswap.h:56:5: 
        54.     x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
        55.     x= (x>>16) | (x<<16);
        56.     return x;
                ^
        57. }
        58. #endif

libavutil/bswap.h:57:1: return from a call to bswap_32
        55.     x= (x>>16) | (x<<16);
        56.     return x;
        57. }
            ^
        58. #endif
        59. 

libavcodec/get_bits.h:389:5: 
      387.     OPEN_READER(re, s)
      388.     UPDATE_CACHE(re, s)
      389.     tmp= SHOW_UBITS(re, s, n);
               ^
      390.     LAST_SKIP_BITS(re, s, n)
      391.     CLOSE_READER(re, s)

libavcodec/get_bits.h:390:5: 
      388.     UPDATE_CACHE(re, s)
      389.     tmp= SHOW_UBITS(re, s, n);
      390.     LAST_SKIP_BITS(re, s, n)
               ^
      391.     CLOSE_READER(re, s)
      392.     return tmp;

libavcodec/get_bits.h:391:5: 
      389.     tmp= SHOW_UBITS(re, s, n);
      390.     LAST_SKIP_BITS(re, s, n)
      391.     CLOSE_READER(re, s)
               ^
      392.     return tmp;
      393. }

libavcodec/get_bits.h:392:5: 
      390.     LAST_SKIP_BITS(re, s, n)
      391.     CLOSE_READER(re, s)
      392.     return tmp;
               ^
      393. }
      394. 

libavcodec/get_bits.h:393:1: return from a call to get_bits
      391.     CLOSE_READER(re, s)
      392.     return tmp;
      393. }
           ^
      394. 
      395. /**

libavcodec/get_bits.h:458:1: return from a call to get_bits_long
    456. #endif
    457.     }
    458. }
         ^
    459. 
    460. /**

libavcodec/get_bits.h:476:1: return from a call to show_bits_long
  474.         return get_bits_long(&gb, n);
  475.     }
  476. }
       ^
  477. 
  478. static inline int check_marker(GetBitContext *s, const char *msg)

libavcodec/mlpdec.c:932:9: Taking false branch
930. 
931.     m->is_major_sync_unit = 0;
932.     if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
             ^
933.         if (read_major_sync(m, &gb) < 0)
934.             goto error;

libavcodec/mlpdec.c:939:10: Taking false branch
937.     }
938. 
939.     if (!m->params_valid) {
              ^
940.         av_log(m->avctx, AV_LOG_WARNING,
941.                "Stream parameters not seen; skipping frame.\n");

libavcodec/mlpdec.c:946:5: 
944.     }
945. 
946.     substream_start = 0;
         ^
947. 
948.     for (substr = 0; substr < m->num_substreams; substr++) {

libavcodec/mlpdec.c:948:10: 
946.     substream_start = 0;
947. 
948.     for (substr = 0; substr < m->num_substreams; substr++) {
              ^
949.         int extraword_present, checkdata_present, end, nonrestart_substr;
950. 

libavcodec/mlpdec.c:948:22: Loop condition is true. Entering loop body
946.     substream_start = 0;
947. 
948.     for (substr = 0; substr < m->num_substreams; substr++) {
                          ^
949.         int extraword_present, checkdata_present, end, nonrestart_substr;
950. 

libavcodec/mlpdec.c:951:9: 
949.         int extraword_present, checkdata_present, end, nonrestart_substr;
950. 
951.         extraword_present = get_bits1(&gb);
             ^
952.         nonrestart_substr = get_bits1(&gb);
953.         checkdata_present = get_bits1(&gb);

libavcodec/get_bits.h:416:1: start of procedure get_bits1()
  414. }
  415. 
  416. static inline unsigned int get_bits1(GetBitContext *s){
       ^
  417. #ifdef ALT_BITSTREAM_READER
  418.     int index= s->index;

libavcodec/get_bits.h:418:5: 
  416. static inline unsigned int get_bits1(GetBitContext *s){
  417. #ifdef ALT_BITSTREAM_READER
  418.     int index= s->index;
           ^
  419.     uint8_t result= s->buffer[ index>>3 ];
  420. #ifdef ALT_BITSTREAM_READER_LE

libavcodec/get_bits.h:419:5: 
  417. #ifdef ALT_BITSTREAM_READER
  418.     int index= s->index;
  419.     uint8_t result= s->buffer[ index>>3 ];
           ^
  420. #ifdef ALT_BITSTREAM_READER_LE
  421.     result>>= (index&0x07);

static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
                            AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    MLPDecodeContext *m = avctx->priv_data;
    GetBitContext gb;
    unsigned int length, substr;
    unsigned int substream_start;
    unsigned int header_size = 4;
    unsigned int substr_header_size = 0;
    uint8_t substream_parity_present[MAX_SUBSTREAMS];
    uint16_t substream_data_len[MAX_SUBSTREAMS];
    uint8_t parity_bits;
    if (buf_size < 4)
        return 0;
    length = (AV_RB16(buf) & 0xfff) * 2;
    if (length > buf_size)
        return -1;
    init_get_bits(&gb, (buf + 4), (length - 4) * 8);
    m->is_major_sync_unit = 0;
    if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
        if (read_major_sync(m, &gb) < 0)
            goto error;
        m->is_major_sync_unit = 1;
        header_size += 28;
    }
    if (!m->params_valid) {
        av_log(m->avctx, AV_LOG_WARNING,
               "Stream parameters not seen; skipping frame.\n");
        *data_size = 0;
        return length;
    }
    substream_start = 0;
    for (substr = 0; substr < m->num_substreams; substr++) {
        int extraword_present, checkdata_present, end, nonrestart_substr;
        extraword_present = get_bits1(&gb);
        nonrestart_substr = get_bits1(&gb);
        checkdata_present = get_bits1(&gb);
        skip_bits1(&gb);
        end = get_bits(&gb, 12) * 2;
        substr_header_size += 2;
        if (extraword_present) {
            if (m->avctx->codec_id == CODEC_ID_MLP) {
                av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
                goto error;
            }
            skip_bits(&gb, 16);
            substr_header_size += 2;
        }
        if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
            av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
            goto error;
        }
        if (end + header_size + substr_header_size > length) {
            av_log(m->avctx, AV_LOG_ERROR,
                   "Indicated length of substream %d data goes off end of "
                   "packet.\n", substr);
            end = length - header_size - substr_header_size;
        }
        if (end < substream_start) {
            av_log(avctx, AV_LOG_ERROR,
                   "Indicated end offset of substream %d data "
                   "is smaller than calculated start offset.\n",
                   substr);
            goto error;
        }
        if (substr > m->max_decoded_substream)
            continue;
        substream_parity_present[substr] = checkdata_present;
        substream_data_len[substr] = end - substream_start;
        substream_start = end;
    }
    parity_bits  = ff_mlp_calculate_parity(buf, 4);
    parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
    if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
        av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
        goto error;
    }
    buf += header_size + substr_header_size;
    for (substr = 0; substr <= m->max_decoded_substream; substr++) {
        SubStream *s = &m->substream[substr];
        init_get_bits(&gb, buf, substream_data_len[substr] * 8);
        m->matrix_changed = 0;
        memset(m->filter_changed, 0, sizeof(m->filter_changed));
        s->blockpos = 0;
        do {
            unsigned int ch;
            if (get_bits1(&gb)) {
                if (get_bits1(&gb)) {
                    if (read_restart_header(m, &gb, buf, substr) < 0)
                        goto next_substr;
                    s->restart_seen = 1;
                }
                if (!s->restart_seen) {
                    goto next_substr;
                }
                if (read_decoding_params(m, &gb, substr) < 0)
                    goto next_substr;
            }
            if (m->matrix_changed > 1) {
                av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
                goto next_substr;
            }
            for (ch = 0; ch < s->max_channel; ch++)
                if (m->filter_changed[ch][FIR] > 1 ||
                    m->filter_changed[ch][IIR] > 1) {
                    av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
                    goto next_substr;
                }
            if (!s->restart_seen) {
                goto next_substr;
            }
            if (read_block_data(m, &gb, substr) < 0)
                return -1;
            if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
                goto substream_length_mismatch;
        } while (!get_bits1(&gb));
        skip_bits(&gb, (-get_bits_count(&gb)) & 15);
        if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
            int shorten_by;
            if (get_bits(&gb, 16) != 0xD234)
                return -1;
            shorten_by = get_bits(&gb, 16);
            if      (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by  & 0x2000)
                s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
            else if (m->avctx->codec_id == CODEC_ID_MLP    && shorten_by != 0xD234)
                return -1;
            if (substr == m->max_decoded_substream)
                av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
        }
        if (substream_parity_present[substr]) {
            uint8_t parity, checksum;
            if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
                goto substream_length_mismatch;
            parity   = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
            checksum = ff_mlp_checksum8       (buf, substream_data_len[substr] - 2);
            if ((get_bits(&gb, 8) ^ parity) != 0xa9    )
                av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
            if ( get_bits(&gb, 8)           != checksum)
                av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n"    , substr);
        }
        if (substream_data_len[substr] * 8 != get_bits_count(&gb)) {
            goto substream_length_mismatch;
        }
next_substr:
        if (!s->restart_seen) {
            av_log(m->avctx, AV_LOG_ERROR,
                   "No restart header present in substream %d.\n", substr);
        }
        buf += substream_data_len[substr];
    }
    rematrix_channels(m, m->max_decoded_substream);
    if (output_data(m, m->max_decoded_substream, data, data_size) < 0)
        return -1;
    return length;
substream_length_mismatch:
    av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
    return -1;
error:
    m->params_valid = 0;
    return -1;
}
['static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,\n                            AVPacket *avpkt)\n{\n    const uint8_t *buf = avpkt->data;\n    int buf_size = avpkt->size;\n    MLPDecodeContext *m = avctx->priv_data;\n    GetBitContext gb;\n    unsigned int length, substr;\n    unsigned int substream_start;\n    unsigned int header_size = 4;\n    unsigned int substr_header_size = 0;\n    uint8_t substream_parity_present[MAX_SUBSTREAMS];\n    uint16_t substream_data_len[MAX_SUBSTREAMS];\n    uint8_t parity_bits;\n    if (buf_size < 4)\n        return 0;\n    length = (AV_RB16(buf) & 0xfff) * 2;\n    if (length > buf_size)\n        return -1;\n    init_get_bits(&gb, (buf + 4), (length - 4) * 8);\n    m->is_major_sync_unit = 0;\n    if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {\n        if (read_major_sync(m, &gb) < 0)\n            goto error;\n        m->is_major_sync_unit = 1;\n        header_size += 28;\n    }\n    if (!m->params_valid) {\n        av_log(m->avctx, AV_LOG_WARNING,\n               "Stream parameters not seen; skipping frame.\\n");\n        *data_size = 0;\n        return length;\n    }\n    substream_start = 0;\n    for (substr = 0; substr < m->num_substreams; substr++) {\n        int extraword_present, checkdata_present, end, nonrestart_substr;\n        extraword_present = get_bits1(&gb);\n        nonrestart_substr = get_bits1(&gb);\n        checkdata_present = get_bits1(&gb);\n        skip_bits1(&gb);\n        end = get_bits(&gb, 12) * 2;\n        substr_header_size += 2;\n        if (extraword_present) {\n            if (m->avctx->codec_id == CODEC_ID_MLP) {\n                av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\\n");\n                goto error;\n            }\n            skip_bits(&gb, 16);\n            substr_header_size += 2;\n        }\n        if (!(nonrestart_substr ^ m->is_major_sync_unit)) {\n            av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\\n");\n            goto error;\n        }\n        if (end + header_size + substr_header_size > length) {\n            av_log(m->avctx, AV_LOG_ERROR,\n                   "Indicated length of substream %d data goes off end of "\n                   "packet.\\n", substr);\n            end = length - header_size - substr_header_size;\n        }\n        if (end < substream_start) {\n            av_log(avctx, AV_LOG_ERROR,\n                   "Indicated end offset of substream %d data "\n                   "is smaller than calculated start offset.\\n",\n                   substr);\n            goto error;\n        }\n        if (substr > m->max_decoded_substream)\n            continue;\n        substream_parity_present[substr] = checkdata_present;\n        substream_data_len[substr] = end - substream_start;\n        substream_start = end;\n    }\n    parity_bits  = ff_mlp_calculate_parity(buf, 4);\n    parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);\n    if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {\n        av_log(avctx, AV_LOG_ERROR, "Parity check failed.\\n");\n        goto error;\n    }\n    buf += header_size + substr_header_size;\n    for (substr = 0; substr <= m->max_decoded_substream; substr++) {\n        SubStream *s = &m->substream[substr];\n        init_get_bits(&gb, buf, substream_data_len[substr] * 8);\n        m->matrix_changed = 0;\n        memset(m->filter_changed, 0, sizeof(m->filter_changed));\n        s->blockpos = 0;\n        do {\n            unsigned int ch;\n            if (get_bits1(&gb)) {\n                if (get_bits1(&gb)) {\n                    if (read_restart_header(m, &gb, buf, substr) < 0)\n                        goto next_substr;\n                    s->restart_seen = 1;\n                }\n                if (!s->restart_seen) {\n                    goto next_substr;\n                }\n                if (read_decoding_params(m, &gb, substr) < 0)\n                    goto next_substr;\n            }\n            if (m->matrix_changed > 1) {\n                av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\\n");\n                goto next_substr;\n            }\n            for (ch = 0; ch < s->max_channel; ch++)\n                if (m->filter_changed[ch][FIR] > 1 ||\n                    m->filter_changed[ch][IIR] > 1) {\n                    av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\\n");\n                    goto next_substr;\n                }\n            if (!s->restart_seen) {\n                goto next_substr;\n            }\n            if (read_block_data(m, &gb, substr) < 0)\n                return -1;\n            if (get_bits_count(&gb) >= substream_data_len[substr] * 8)\n                goto substream_length_mismatch;\n        } while (!get_bits1(&gb));\n        skip_bits(&gb, (-get_bits_count(&gb)) & 15);\n        if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {\n            int shorten_by;\n            if (get_bits(&gb, 16) != 0xD234)\n                return -1;\n            shorten_by = get_bits(&gb, 16);\n            if      (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by  & 0x2000)\n                s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);\n            else if (m->avctx->codec_id == CODEC_ID_MLP    && shorten_by != 0xD234)\n                return -1;\n            if (substr == m->max_decoded_substream)\n                av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\\n");\n        }\n        if (substream_parity_present[substr]) {\n            uint8_t parity, checksum;\n            if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)\n                goto substream_length_mismatch;\n            parity   = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);\n            checksum = ff_mlp_checksum8       (buf, substream_data_len[substr] - 2);\n            if ((get_bits(&gb, 8) ^ parity) != 0xa9    )\n                av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\\n", substr);\n            if ( get_bits(&gb, 8)           != checksum)\n                av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\\n"    , substr);\n        }\n        if (substream_data_len[substr] * 8 != get_bits_count(&gb)) {\n            goto substream_length_mismatch;\n        }\nnext_substr:\n        if (!s->restart_seen) {\n            av_log(m->avctx, AV_LOG_ERROR,\n                   "No restart header present in substream %d.\\n", substr);\n        }\n        buf += substream_data_len[substr];\n    }\n    rematrix_channels(m, m->max_decoded_substream);\n    if (output_data(m, m->max_decoded_substream, data, data_size) < 0)\n        return -1;\n    return length;\nsubstream_length_mismatch:\n    av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\\n", substr);\n    return -1;\nerror:\n    m->params_valid = 0;\n    return -1;\n}', 'static av_always_inline av_const uint16_t bswap_16(uint16_t x)\n{\n    x= (x>>8) | (x<<8);\n    return x;\n}', 'static inline void init_get_bits(GetBitContext *s,\n                   const uint8_t *buffer, int bit_size)\n{\n    int buffer_size= (bit_size+7)>>3;\n    if(buffer_size < 0 || bit_size < 0) {\n        buffer_size = bit_size = 0;\n        buffer = NULL;\n    }\n    s->buffer= buffer;\n    s->size_in_bits= bit_size;\n    s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n    s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n    s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n    s->bit_count = 16 + 8*((intptr_t)buffer&1);\n    skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n    s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n    s->bit_count = 32 + 8*((intptr_t)buffer&3);\n    skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int show_bits_long(GetBitContext *s, int n){\n    if(n<=17) return show_bits(s, n);\n    else{\n        GetBitContext gb= *s;\n        return get_bits_long(&gb, n);\n    }\n}', 'static inline unsigned int get_bits_long(GetBitContext *s, int n){\n    if(n<=17) return get_bits(s, n);\n    else{\n#ifdef ALT_BITSTREAM_READER_LE\n        int ret= get_bits(s, 16);\n        return ret | (get_bits(s, n-16) << 16);\n#else\n        int ret= get_bits(s, 16) << (n-16);\n        return ret | get_bits(s, n-16);\n#endif\n    }\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n){\n    register int tmp;\n    OPEN_READER(re, s)\n    UPDATE_CACHE(re, s)\n    tmp= SHOW_UBITS(re, s, n);\n    LAST_SKIP_BITS(re, s, n)\n    CLOSE_READER(re, s)\n    return tmp;\n}', 'static av_always_inline av_const uint32_t bswap_32(uint32_t x)\n{\n    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n    x= (x>>16) | (x<<16);\n    return x;\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n    int index= s->index;\n    uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n    result>>= (index&0x07);\n    result&= 1;\n#else\n    result<<= (index&0x07);\n    result>>= 8 - 1;\n#endif\n    index++;\n    s->index= index;\n    return result;\n#else\n    return get_bits(s, 1);\n#endif\n}']