~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/mlpdec.c

  • Committer: Gauvain Pocentek
  • Date: 2012-03-06 11:59:12 UTC
  • mfrom: (66.1.15 precise)
  • Revision ID: gauvain@pocentek.net-20120306115912-h9d6kt9j0l532oo5
* Merge from Ubuntu:
  - put back faac support
  - recommends apport-hooks-medibuntu
  - change Maintainer, Uploaders & Vcs-* fields.
* New upstream snapshot
* upload to unstable
* Build against external libmpeg2
* drop 51_FTBFS_arm.patch again
* no longer build depend on libcdparanoia-dev on the Hurd
* Fix FTBFS on the hurd.
  Thanks to Samuel Thibault <sthibault@debian.org> (Closes: #654974)
* Fix FTBFS on arm
* New upstream snapshot, Closes: #650339, #643621, #481807
* Imported Upstream version 1.0~rc4+svn34492
* Bump standards version
* Bump dependency on libav >= 4:0.8~, Closes: #653887
* Fix build-indep
* Build mplayer-gui again, Closes: #568514
* Drop debian/all-lang-config-mak.sh, no longer needed
* include .dfsg1 in version number
* remove get-orig-source target
* no longer prune compiler flags from the environment
* No longer advertise nor build 3fdx, mga and dxr3 backends,
  Closes: #496106, #442181, #533546
* beautify mplayer version identification string
* Brown paperbag upload.
* Next try to fix build failure on sparce after recent binutils change.
* Brown paperbag upload.
* Really fix build failure on sparc after recent binutils change.
* Properly set Replaces/Conflicts on mplayer2{,-dbg} to avoid
  file overwrite errors.
* Adjust versioning of mplayer listed in the mplayer-dbg's Depends field.
* Fix build failure on sparc after recent binutils change.
* Urgency medium bumped because of RC-level bugfix
  and speeding up x264 transition.
* Update to my @debian.org email.
* Upload to unstable
* Enable joystick support on Linux only, Closes: #638408
* Rebuild fixes toolchain issue on arm, Closes: #637077
* New upstream snapshot
* following the discussion started by Diego Biurrun <diego@biurrun.de>
  in debian-devel, I have prepared a new packaging of 'mplayer'
  (with code that comes from CVS)
* the upstream tar.bz cannot be distributed by Debian, since it contains
   CSS code; so I am repackaging it 
* I have tried my best to address all known issues:
  - the package contains the detailed Copyright made by Diego Biurrun 
  - the package does not contain CSS code, or  AFAIK other code on which 
     there is active patent enforcement
  - there is a script  debian/cvs-changelog.sh  that shows all changes
     done to files included in this source.
    This should comply with GPLv2 sec 2.a  (in spirit if not in letter)
    For this reason, the source code contains CVS directories.
* needs   make (>= 3.80) for 'html-chunked-$(1)' in DOCS/xml/Makefile

* some corrections, as suggested Diego Biurrun
  - binary codecs should go into /usr/lib/codecs (upstream default)
  - better template 'mplayer/install_codecs'
  - an empty 'font=' in mplayer.conf breaks mplayer: postinst corrected
* correction in 'mplayer/cfgnote'
* better mplayer.postinst and mplayer.config

* New upstream release
* better debian/copyright file
* do not ship a skin
* New upstream release
* changed DEB_BUILD_OPTIONS to DEB_BUILD_CONFIGURE ,
  DEB_BUILD_OPTIONS is used as in debian policy
* use gcc-3.4
* changed xlibs-dev to a long list of dependencies, for Debian/etch
* try to adhere to  http://www.mplayerhq.hu/DOCS/tech/binary-packaging.txt
  (see README.Debian for details)
* removed dependency on xlibmesa-dev, disabled opengl
* New upstream release
* Simon McVittie <hacks@pseudorandom.co.uk> wonderful work:
- Work around Debian bug #267442 (glibc's sys/uio.h and gcc's altivec.h have
  conflicting uses for __vector) by re-ordering #includes
- Fix potential symlink attack in ./configure
- Disable support for binary codecs on platforms for which those codecs
  aren't available; also disable the corresponding Debconf note when it's
  inappropriate
- Changed Build-Depends: so it works in pbuilder
- Explicitly build-depend on libjpeg62-dev, libfontconfig1-dev,
  libungif4-dev 
- Tweak debian/rules to avoid certain errors being ignored
- Use --language=all
* provide a target  'debian/rules get-orig-source' 
  that recreates the orig.tar.gz ; then use the above orig.tar.gz
* rewrote some parts of debian/rules
* don't clean and recompile docs if upstream ships them
* mplayer-doc was shipping too much stuff
* translated man pages where not installed properly
* compile with libdv4-dev
* correct README.Debian
* Forgot build-dep on libtheora
* Must not depend on libxvidcore
* New upstream release
* new release.
* rc1 to become 0.90
* new pre-release
* new pre-release
* gtk bug fixed.
* new release.
* version bumped
* 0.60 pre2 release
* 0.60 pre-release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
    "a sample of this file.";
46
46
 
47
47
typedef struct SubStream {
48
 
    //! Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
 
48
    /// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
49
49
    uint8_t     restart_seen;
50
50
 
51
51
    //@{
52
52
    /** restart header data */
53
 
    //! The type of noise to be used in the rematrix stage.
 
53
    /// The type of noise to be used in the rematrix stage.
54
54
    uint16_t    noise_type;
55
55
 
56
 
    //! The index of the first channel coded in this substream.
 
56
    /// The index of the first channel coded in this substream.
57
57
    uint8_t     min_channel;
58
 
    //! The index of the last channel coded in this substream.
 
58
    /// The index of the last channel coded in this substream.
59
59
    uint8_t     max_channel;
60
 
    //! The number of channels input into the rematrix stage.
 
60
    /// The number of channels input into the rematrix stage.
61
61
    uint8_t     max_matrix_channel;
62
 
    //! For each channel output by the matrix, the output channel to map it to
 
62
    /// For each channel output by the matrix, the output channel to map it to
63
63
    uint8_t     ch_assign[MAX_CHANNELS];
64
64
 
65
 
    //! Channel coding parameters for channels in the substream
 
65
    /// Channel coding parameters for channels in the substream
66
66
    ChannelParams channel_params[MAX_CHANNELS];
67
67
 
68
 
    //! The left shift applied to random noise in 0x31ea substreams.
 
68
    /// The left shift applied to random noise in 0x31ea substreams.
69
69
    uint8_t     noise_shift;
70
 
    //! The current seed value for the pseudorandom noise generator(s).
 
70
    /// The current seed value for the pseudorandom noise generator(s).
71
71
    uint32_t    noisegen_seed;
72
72
 
73
 
    //! Set if the substream contains extra info to check the size of VLC blocks.
 
73
    /// Set if the substream contains extra info to check the size of VLC blocks.
74
74
    uint8_t     data_check_present;
75
75
 
76
 
    //! Bitmask of which parameter sets are conveyed in a decoding parameter block.
 
76
    /// Bitmask of which parameter sets are conveyed in a decoding parameter block.
77
77
    uint8_t     param_presence_flags;
78
78
#define PARAM_BLOCKSIZE     (1 << 7)
79
79
#define PARAM_MATRIX        (1 << 6)
88
88
    //@{
89
89
    /** matrix data */
90
90
 
91
 
    //! Number of matrices to be applied.
 
91
    /// Number of matrices to be applied.
92
92
    uint8_t     num_primitive_matrices;
93
93
 
94
 
    //! matrix output channel
 
94
    /// matrix output channel
95
95
    uint8_t     matrix_out_ch[MAX_MATRICES];
96
96
 
97
 
    //! Whether the LSBs of the matrix output are encoded in the bitstream.
 
97
    /// Whether the LSBs of the matrix output are encoded in the bitstream.
98
98
    uint8_t     lsb_bypass[MAX_MATRICES];
99
 
    //! Matrix coefficients, stored as 2.14 fixed point.
 
99
    /// Matrix coefficients, stored as 2.14 fixed point.
100
100
    int32_t     matrix_coeff[MAX_MATRICES][MAX_CHANNELS];
101
 
    //! Left shift to apply to noise values in 0x31eb substreams.
 
101
    /// Left shift to apply to noise values in 0x31eb substreams.
102
102
    uint8_t     matrix_noise_shift[MAX_MATRICES];
103
103
    //@}
104
104
 
105
 
    //! Left shift to apply to Huffman-decoded residuals.
 
105
    /// Left shift to apply to Huffman-decoded residuals.
106
106
    uint8_t     quant_step_size[MAX_CHANNELS];
107
107
 
108
 
    //! number of PCM samples in current audio block
 
108
    /// number of PCM samples in current audio block
109
109
    uint16_t    blocksize;
110
 
    //! Number of PCM samples decoded so far in this frame.
 
110
    /// Number of PCM samples decoded so far in this frame.
111
111
    uint16_t    blockpos;
112
112
 
113
 
    //! Left shift to apply to decoded PCM values to get final 24-bit output.
 
113
    /// Left shift to apply to decoded PCM values to get final 24-bit output.
114
114
    int8_t      output_shift[MAX_CHANNELS];
115
115
 
116
 
    //! Running XOR of all output samples.
 
116
    /// Running XOR of all output samples.
117
117
    int32_t     lossless_check_data;
118
118
 
119
119
} SubStream;
120
120
 
121
121
typedef struct MLPDecodeContext {
122
122
    AVCodecContext *avctx;
 
123
    AVFrame     frame;
123
124
 
124
 
    //! Current access unit being read has a major sync.
 
125
    /// Current access unit being read has a major sync.
125
126
    int         is_major_sync_unit;
126
127
 
127
 
    //! Set if a valid major sync block has been read. Otherwise no decoding is possible.
 
128
    /// Set if a valid major sync block has been read. Otherwise no decoding is possible.
128
129
    uint8_t     params_valid;
129
130
 
130
 
    //! Number of substreams contained within this stream.
 
131
    /// Number of substreams contained within this stream.
131
132
    uint8_t     num_substreams;
132
133
 
133
 
    //! Index of the last substream to decode - further substreams are skipped.
 
134
    /// Index of the last substream to decode - further substreams are skipped.
134
135
    uint8_t     max_decoded_substream;
135
136
 
136
 
    //! number of PCM samples contained in each frame
 
137
    /// number of PCM samples contained in each frame
137
138
    int         access_unit_size;
138
 
    //! next power of two above the number of samples in each frame
 
139
    /// next power of two above the number of samples in each frame
139
140
    int         access_unit_size_pow2;
140
141
 
141
142
    SubStream   substream[MAX_SUBSTREAMS];
214
215
                            VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
215
216
 
216
217
        if (result < 0)
217
 
            return -1;
 
218
            return AVERROR_INVALIDDATA;
218
219
 
219
220
        if (lsb_bits > 0)
220
221
            result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
239
240
        m->substream[substr].lossless_check_data = 0xffffffff;
240
241
    dsputil_init(&m->dsp, avctx);
241
242
 
 
243
    avcodec_get_frame_defaults(&m->frame);
 
244
    avctx->coded_frame = &m->frame;
 
245
 
242
246
    return 0;
243
247
}
244
248
 
250
254
static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
251
255
{
252
256
    MLPHeaderInfo mh;
253
 
    int substr;
 
257
    int substr, ret;
254
258
 
255
 
    if (ff_mlp_read_major_sync(m->avctx, &mh, gb) != 0)
256
 
        return -1;
 
259
    if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
 
260
        return ret;
257
261
 
258
262
    if (mh.group1_bits == 0) {
259
263
        av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
260
 
        return -1;
 
264
        return AVERROR_INVALIDDATA;
261
265
    }
262
266
    if (mh.group2_bits > mh.group1_bits) {
263
267
        av_log(m->avctx, AV_LOG_ERROR,
264
268
               "Channel group 2 cannot have more bits per sample than group 1.\n");
265
 
        return -1;
 
269
        return AVERROR_INVALIDDATA;
266
270
    }
267
271
 
268
272
    if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
269
273
        av_log(m->avctx, AV_LOG_ERROR,
270
274
               "Channel groups with differing sample rates are not currently supported.\n");
271
 
        return -1;
 
275
        return AVERROR_INVALIDDATA;
272
276
    }
273
277
 
274
278
    if (mh.group1_samplerate == 0) {
275
279
        av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
276
 
        return -1;
 
280
        return AVERROR_INVALIDDATA;
277
281
    }
278
282
    if (mh.group1_samplerate > MAX_SAMPLERATE) {
279
283
        av_log(m->avctx, AV_LOG_ERROR,
280
284
               "Sampling rate %d is greater than the supported maximum (%d).\n",
281
285
               mh.group1_samplerate, MAX_SAMPLERATE);
282
 
        return -1;
 
286
        return AVERROR_INVALIDDATA;
283
287
    }
284
288
    if (mh.access_unit_size > MAX_BLOCKSIZE) {
285
289
        av_log(m->avctx, AV_LOG_ERROR,
286
290
               "Block size %d is greater than the supported maximum (%d).\n",
287
291
               mh.access_unit_size, MAX_BLOCKSIZE);
288
 
        return -1;
 
292
        return AVERROR_INVALIDDATA;
289
293
    }
290
294
    if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
291
295
        av_log(m->avctx, AV_LOG_ERROR,
292
296
               "Block size pow2 %d is greater than the supported maximum (%d).\n",
293
297
               mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
294
 
        return -1;
 
298
        return AVERROR_INVALIDDATA;
295
299
    }
296
300
 
297
301
    if (mh.num_substreams == 0)
298
 
        return -1;
 
302
        return AVERROR_INVALIDDATA;
299
303
    if (m->avctx->codec_id == CODEC_ID_MLP && mh.num_substreams > 2) {
300
304
        av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
301
 
        return -1;
 
305
        return AVERROR_INVALIDDATA;
302
306
    }
303
307
    if (mh.num_substreams > MAX_SUBSTREAMS) {
304
308
        av_log(m->avctx, AV_LOG_ERROR,
305
309
               "Number of substreams %d is larger than the maximum supported "
306
310
               "by the decoder. %s\n", mh.num_substreams, sample_message);
307
 
        return -1;
 
311
        return AVERROR_INVALIDDATA;
308
312
    }
309
313
 
310
314
    m->access_unit_size      = mh.access_unit_size;
351
355
    if (sync_word != 0x31ea >> 1) {
352
356
        av_log(m->avctx, AV_LOG_ERROR,
353
357
               "restart header sync incorrect (got 0x%04x)\n", sync_word);
354
 
        return -1;
 
358
        return AVERROR_INVALIDDATA;
355
359
    }
356
360
 
357
361
    s->noise_type = get_bits1(gbp);
358
362
 
359
363
    if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
360
364
        av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
361
 
        return -1;
 
365
        return AVERROR_INVALIDDATA;
362
366
    }
363
367
 
364
368
    skip_bits(gbp, 16); /* Output timestamp */
371
375
        av_log(m->avctx, AV_LOG_ERROR,
372
376
               "Max matrix channel cannot be greater than %d.\n",
373
377
               max_matrix_channel);
374
 
        return -1;
 
378
        return AVERROR_INVALIDDATA;
375
379
    }
376
380
 
377
381
    if (s->max_channel != s->max_matrix_channel) {
378
382
        av_log(m->avctx, AV_LOG_ERROR,
379
383
               "Max channel must be equal max matrix channel.\n");
380
 
        return -1;
 
384
        return AVERROR_INVALIDDATA;
381
385
    }
382
386
 
383
387
    /* This should happen for TrueHD streams with >6 channels and MLP's noise
386
390
        av_log(m->avctx, AV_LOG_ERROR,
387
391
               "Number of channels %d is larger than the maximum supported "
388
392
               "by the decoder. %s\n", s->max_channel+2, sample_message);
389
 
        return -1;
 
393
        return AVERROR_INVALIDDATA;
390
394
    }
391
395
 
392
396
    if (s->min_channel > s->max_channel) {
393
397
        av_log(m->avctx, AV_LOG_ERROR,
394
398
               "Substream min channel cannot be greater than max channel.\n");
395
 
        return -1;
 
399
        return AVERROR_INVALIDDATA;
396
400
    }
397
401
 
398
402
    if (m->avctx->request_channels > 0
431
435
            av_log(m->avctx, AV_LOG_ERROR,
432
436
                   "Assignment of matrix channel %d to invalid output channel %d. %s\n",
433
437
                   ch, ch_assign, sample_message);
434
 
            return -1;
 
438
            return AVERROR_INVALIDDATA;
435
439
        }
436
440
        s->ch_assign[ch_assign] = ch;
437
441
    }
487
491
 
488
492
    if (m->filter_changed[channel][filter]++ > 1) {
489
493
        av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
490
 
        return -1;
 
494
        return AVERROR_INVALIDDATA;
491
495
    }
492
496
 
493
497
    order = get_bits(gbp, 4);
495
499
        av_log(m->avctx, AV_LOG_ERROR,
496
500
               "%cIR filter order %d is greater than maximum %d.\n",
497
501
               fchar, order, max_order);
498
 
        return -1;
 
502
        return AVERROR_INVALIDDATA;
499
503
    }
500
504
    fp->order = order;
501
505
 
511
515
            av_log(m->avctx, AV_LOG_ERROR,
512
516
                   "%cIR filter coeff_bits must be between 1 and 16.\n",
513
517
                   fchar);
514
 
            return -1;
 
518
            return AVERROR_INVALIDDATA;
515
519
        }
516
520
        if (coeff_bits + coeff_shift > 16) {
517
521
            av_log(m->avctx, AV_LOG_ERROR,
518
522
                   "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
519
523
                   fchar);
520
 
            return -1;
 
524
            return AVERROR_INVALIDDATA;
521
525
        }
522
526
 
523
527
        for (i = 0; i < order; i++)
529
533
            if (filter == FIR) {
530
534
                av_log(m->avctx, AV_LOG_ERROR,
531
535
                       "FIR filter has state data specified.\n");
532
 
                return -1;
 
536
                return AVERROR_INVALIDDATA;
533
537
            }
534
538
 
535
539
            state_bits  = get_bits(gbp, 4);
557
561
 
558
562
    if (m->matrix_changed++ > 1) {
559
563
        av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
560
 
        return -1;
 
564
        return AVERROR_INVALIDDATA;
561
565
    }
562
566
 
563
567
    s->num_primitive_matrices = get_bits(gbp, 4);
566
570
        av_log(m->avctx, AV_LOG_ERROR,
567
571
               "Number of primitive matrices cannot be greater than %d.\n",
568
572
               max_primitive_matrices);
569
 
        return -1;
 
573
        return AVERROR_INVALIDDATA;
570
574
    }
571
575
 
572
576
    for (mat = 0; mat < s->num_primitive_matrices; mat++) {
579
583
            av_log(m->avctx, AV_LOG_ERROR,
580
584
                    "Invalid channel %d specified as output from matrix.\n",
581
585
                    s->matrix_out_ch[mat]);
582
 
            return -1;
 
586
            return AVERROR_INVALIDDATA;
583
587
        }
584
588
        if (frac_bits > 14) {
585
589
            av_log(m->avctx, AV_LOG_ERROR,
586
590
                    "Too many fractional bits specified.\n");
587
 
            return -1;
 
591
            return AVERROR_INVALIDDATA;
588
592
        }
589
593
 
590
594
        max_chan = s->max_matrix_channel;
617
621
    ChannelParams *cp = &s->channel_params[ch];
618
622
    FilterParams *fir = &cp->filter_params[FIR];
619
623
    FilterParams *iir = &cp->filter_params[IIR];
 
624
    int ret;
620
625
 
621
626
    if (s->param_presence_flags & PARAM_FIR)
622
627
        if (get_bits1(gbp))
623
 
            if (read_filter_params(m, gbp, substr, ch, FIR) < 0)
624
 
                return -1;
 
628
            if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
 
629
                return ret;
625
630
 
626
631
    if (s->param_presence_flags & PARAM_IIR)
627
632
        if (get_bits1(gbp))
628
 
            if (read_filter_params(m, gbp, substr, ch, IIR) < 0)
629
 
                return -1;
 
633
            if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
 
634
                return ret;
630
635
 
631
636
    if (fir->order + iir->order > 8) {
632
637
        av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
633
 
        return -1;
 
638
        return AVERROR_INVALIDDATA;
634
639
    }
635
640
 
636
641
    if (fir->order && iir->order &&
637
642
        fir->shift != iir->shift) {
638
643
        av_log(m->avctx, AV_LOG_ERROR,
639
644
                "FIR and IIR filters must use the same precision.\n");
640
 
        return -1;
 
645
        return AVERROR_INVALIDDATA;
641
646
    }
642
647
    /* The FIR and IIR filters must have the same precision.
643
648
     * To simplify the filtering code, only the precision of the
656
661
 
657
662
    if (cp->huff_lsbs > 24) {
658
663
        av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
659
 
        return -1;
 
664
        return AVERROR_INVALIDDATA;
660
665
    }
661
666
 
662
667
    cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
672
677
{
673
678
    SubStream *s = &m->substream[substr];
674
679
    unsigned int ch;
 
680
    int ret;
675
681
 
676
682
    if (s->param_presence_flags & PARAM_PRESENCE)
677
683
        if (get_bits1(gbp))
683
689
            if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
684
690
                av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
685
691
                s->blocksize = 0;
686
 
                return -1;
 
692
                return AVERROR_INVALIDDATA;
687
693
            }
688
694
        }
689
695
 
690
696
    if (s->param_presence_flags & PARAM_MATRIX)
691
697
        if (get_bits1(gbp))
692
 
            if (read_matrix_params(m, substr, gbp) < 0)
693
 
                return -1;
 
698
            if ((ret = read_matrix_params(m, substr, gbp)) < 0)
 
699
                return ret;
694
700
 
695
701
    if (s->param_presence_flags & PARAM_OUTSHIFT)
696
702
        if (get_bits1(gbp))
709
715
 
710
716
    for (ch = s->min_channel; ch <= s->max_channel; ch++)
711
717
        if (get_bits1(gbp))
712
 
            if (read_channel_params(m, substr, gbp, ch) < 0)
713
 
                return -1;
 
718
            if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
 
719
                return ret;
714
720
 
715
721
    return 0;
716
722
}
752
758
{
753
759
    SubStream *s = &m->substream[substr];
754
760
    unsigned int i, ch, expected_stream_pos = 0;
 
761
    int ret;
755
762
 
756
763
    if (s->data_check_present) {
757
764
        expected_stream_pos  = get_bits_count(gbp);
762
769
 
763
770
    if (s->blockpos + s->blocksize > m->access_unit_size) {
764
771
        av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
765
 
        return -1;
 
772
        return AVERROR_INVALIDDATA;
766
773
    }
767
774
 
768
775
    memset(&m->bypassed_lsbs[s->blockpos][0], 0,
769
776
           s->blocksize * sizeof(m->bypassed_lsbs[0]));
770
777
 
771
778
    for (i = 0; i < s->blocksize; i++)
772
 
        if (read_huff_channels(m, gbp, substr, i) < 0)
773
 
            return -1;
 
779
        if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
 
780
            return ret;
774
781
 
775
782
    for (ch = s->min_channel; ch <= s->max_channel; ch++)
776
783
        filter_channel(m, substr, ch);
901
908
 
902
909
/** Write the audio data into the output buffer. */
903
910
 
904
 
static int output_data_internal(MLPDecodeContext *m, unsigned int substr,
905
 
                                uint8_t *data, unsigned int *data_size, int is32)
 
911
static int output_data(MLPDecodeContext *m, unsigned int substr,
 
912
                       void *data, int *got_frame_ptr)
906
913
{
 
914
    AVCodecContext *avctx = m->avctx;
907
915
    SubStream *s = &m->substream[substr];
908
916
    unsigned int i, out_ch = 0;
909
 
    int32_t *data_32 = (int32_t*) data;
910
 
    int16_t *data_16 = (int16_t*) data;
911
 
 
912
 
    if (*data_size < (s->max_channel + 1) * s->blockpos * (is32 ? 4 : 2))
913
 
        return -1;
 
917
    int32_t *data_32;
 
918
    int16_t *data_16;
 
919
    int ret;
 
920
    int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
 
921
 
 
922
    if (m->avctx->channels != s->max_matrix_channel + 1) {
 
923
        av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
 
924
        return AVERROR_INVALIDDATA;
 
925
    }
 
926
 
 
927
    /* get output buffer */
 
928
    m->frame.nb_samples = s->blockpos;
 
929
    if ((ret = avctx->get_buffer(avctx, &m->frame)) < 0) {
 
930
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
 
931
        return ret;
 
932
    }
 
933
    data_32 = (int32_t *)m->frame.data[0];
 
934
    data_16 = (int16_t *)m->frame.data[0];
914
935
 
915
936
    for (i = 0; i < s->blockpos; i++) {
916
937
        for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
923
944
        }
924
945
    }
925
946
 
926
 
    *data_size = i * out_ch * (is32 ? 4 : 2);
 
947
    *got_frame_ptr   = 1;
 
948
    *(AVFrame *)data = m->frame;
927
949
 
928
950
    return 0;
929
951
}
930
952
 
931
 
static int output_data(MLPDecodeContext *m, unsigned int substr,
932
 
                       uint8_t *data, unsigned int *data_size)
933
 
{
934
 
    if (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
935
 
        return output_data_internal(m, substr, data, data_size, 1);
936
 
    else
937
 
        return output_data_internal(m, substr, data, data_size, 0);
938
 
}
939
 
 
940
 
 
941
953
/** Read an access unit from the stream.
942
954
 *  @return negative on error, 0 if not enough data is present in the input stream,
943
955
 *  otherwise the number of bytes consumed. */
944
956
 
945
 
static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
946
 
                            AVPacket *avpkt)
 
957
static int read_access_unit(AVCodecContext *avctx, void* data,
 
958
                            int *got_frame_ptr, AVPacket *avpkt)
947
959
{
948
960
    const uint8_t *buf = avpkt->data;
949
961
    int buf_size = avpkt->size;
956
968
    uint8_t substream_parity_present[MAX_SUBSTREAMS];
957
969
    uint16_t substream_data_len[MAX_SUBSTREAMS];
958
970
    uint8_t parity_bits;
 
971
    int ret;
959
972
 
960
973
    if (buf_size < 4)
961
974
        return 0;
963
976
    length = (AV_RB16(buf) & 0xfff) * 2;
964
977
 
965
978
    if (length < 4 || length > buf_size)
966
 
        return -1;
 
979
        return AVERROR_INVALIDDATA;
967
980
 
968
981
    init_get_bits(&gb, (buf + 4), (length - 4) * 8);
969
982
 
978
991
    if (!m->params_valid) {
979
992
        av_log(m->avctx, AV_LOG_WARNING,
980
993
               "Stream parameters not seen; skipping frame.\n");
981
 
        *data_size = 0;
 
994
        *got_frame_ptr = 0;
982
995
        return length;
983
996
    }
984
997
 
1069
1082
            if (!s->restart_seen)
1070
1083
                goto next_substr;
1071
1084
 
1072
 
            if (read_block_data(m, &gb, substr) < 0)
1073
 
                return -1;
 
1085
            if ((ret = read_block_data(m, &gb, substr)) < 0)
 
1086
                return ret;
1074
1087
 
1075
1088
            if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1076
1089
                goto substream_length_mismatch;
1083
1096
            int shorten_by;
1084
1097
 
1085
1098
            if (get_bits(&gb, 16) != 0xD234)
1086
 
                return -1;
 
1099
                return AVERROR_INVALIDDATA;
1087
1100
 
1088
1101
            shorten_by = get_bits(&gb, 16);
1089
1102
            if      (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by  & 0x2000)
1090
1103
                s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1091
1104
            else if (m->avctx->codec_id == CODEC_ID_MLP    && shorten_by != 0xD234)
1092
 
                return -1;
 
1105
                return AVERROR_INVALIDDATA;
1093
1106
 
1094
1107
            if (substr == m->max_decoded_substream)
1095
1108
                av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
1123
1136
 
1124
1137
    rematrix_channels(m, m->max_decoded_substream);
1125
1138
 
1126
 
    if (output_data(m, m->max_decoded_substream, data, data_size) < 0)
1127
 
        return -1;
 
1139
    if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
 
1140
        return ret;
1128
1141
 
1129
1142
    return length;
1130
1143
 
1131
1144
substream_length_mismatch:
1132
1145
    av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1133
 
    return -1;
 
1146
    return AVERROR_INVALIDDATA;
1134
1147
 
1135
1148
error:
1136
1149
    m->params_valid = 0;
1137
 
    return -1;
 
1150
    return AVERROR_INVALIDDATA;
1138
1151
}
1139
1152
 
1140
1153
AVCodec ff_mlp_decoder = {
1141
 
    "mlp",
1142
 
    AVMEDIA_TYPE_AUDIO,
1143
 
    CODEC_ID_MLP,
1144
 
    sizeof(MLPDecodeContext),
1145
 
    mlp_decode_init,
1146
 
    NULL,
1147
 
    NULL,
1148
 
    read_access_unit,
 
1154
    .name           = "mlp",
 
1155
    .type           = AVMEDIA_TYPE_AUDIO,
 
1156
    .id             = CODEC_ID_MLP,
 
1157
    .priv_data_size = sizeof(MLPDecodeContext),
 
1158
    .init           = mlp_decode_init,
 
1159
    .decode         = read_access_unit,
 
1160
    .capabilities   = CODEC_CAP_DR1,
1149
1161
    .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
1150
1162
};
1151
1163
 
1152
1164
#if CONFIG_TRUEHD_DECODER
1153
1165
AVCodec ff_truehd_decoder = {
1154
 
    "truehd",
1155
 
    AVMEDIA_TYPE_AUDIO,
1156
 
    CODEC_ID_TRUEHD,
1157
 
    sizeof(MLPDecodeContext),
1158
 
    mlp_decode_init,
1159
 
    NULL,
1160
 
    NULL,
1161
 
    read_access_unit,
 
1166
    .name           = "truehd",
 
1167
    .type           = AVMEDIA_TYPE_AUDIO,
 
1168
    .id             = CODEC_ID_TRUEHD,
 
1169
    .priv_data_size = sizeof(MLPDecodeContext),
 
1170
    .init           = mlp_decode_init,
 
1171
    .decode         = read_access_unit,
 
1172
    .capabilities   = CODEC_CAP_DR1,
1162
1173
    .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
1163
1174
};
1164
1175
#endif /* CONFIG_TRUEHD_DECODER */