diff mbox

[FFmpeg-devel] avcodec: add ATRAC Advanced Lossless decoders

Message ID 20170129173921.30249-1-onemda@gmail.com
State Accepted
Headers show

Commit Message

Paul B Mahol Jan. 29, 2017, 5:39 p.m. UTC
Only lossy part is decoded for now.

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/Makefile        |   3 +
 libavcodec/allcodecs.c     |   2 +
 libavcodec/atrac3.c        |  77 +++++++++++++++++++++-
 libavcodec/atrac3plusdec.c |  14 +++-
 libavcodec/avcodec.h       |   2 +
 libavcodec/codec_desc.c    |  14 ++++
 libavformat/oma.c          |  10 +--
 libavformat/oma.h          |   2 +
 libavformat/omadec.c       | 156 +++++++++++++++++++++++++++++++++++----------
 9 files changed, 239 insertions(+), 41 deletions(-)

Comments

Michael Niedermayer Jan. 31, 2017, 3:07 p.m. UTC | #1
On Sun, Jan 29, 2017 at 06:39:21PM +0100, Paul B Mahol wrote:
> Only lossy part is decoded for now.
> 
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
[...]
>  static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
>  {
>      OMAContext *oc  = s->priv_data;
> -    AVStream *st    = s->streams[0];
> -    int packet_size = st->codecpar->block_align;
> -    int byte_rate   = st->codecpar->bit_rate >> 3;
> -    int64_t pos     = avio_tell(s->pb);
> -    int ret         = av_get_packet(s->pb, pkt, packet_size);
> -
> -    if (ret < packet_size)
> -        pkt->flags |= AV_PKT_FLAG_CORRUPT;
> -
> -    if (ret < 0)
> -        return ret;
> -    if (!ret)
> -        return AVERROR_EOF;
> -
> -    pkt->stream_index = 0;
> -
> -    if (pos >= oc->content_start && byte_rate > 0) {
> -        pkt->pts =
> -        pkt->dts = av_rescale(pos - oc->content_start, st->time_base.den,
> -                              byte_rate * (int64_t)st->time_base.num);
> -    }
> -
> -    if (oc->encrypted) {
> -        /* previous unencrypted block saved in IV for
> -         * the next packet (CBC mode) */
> -        if (ret == packet_size)
> -            av_des_crypt(oc->av_des, pkt->data, pkt->data,
> -                         (packet_size >> 3), oc->iv, 1);
> -        else
> -            memset(oc->iv, 0, 8);
> -    }
> -
> -    return ret;
> +    return oc->read_packet(s, pkt);
>  }

moving this into read_packet() could be done in a seperate patch


>  
>  static int oma_read_probe(AVProbeData *p)
> @@ -491,8 +571,14 @@ static int oma_read_seek(struct AVFormatContext *s,
>                           int stream_index, int64_t timestamp, int flags)
>  {
>      OMAContext *oc = s->priv_data;
> -    int64_t err = ff_pcm_read_seek(s, stream_index, timestamp, flags);
> +    AVStream *st = s->streams[0];
> +    int64_t err;
> +
> +    if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL ||
> +        st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL)

> +        return -1;

should be a AVERROR code


thanks

[...]
Paul B Mahol Jan. 31, 2017, 3:17 p.m. UTC | #2
On 1/31/17, Michael Niedermayer <michaelni@gmx.at> wrote:
> On Sun, Jan 29, 2017 at 06:39:21PM +0100, Paul B Mahol wrote:
>> Only lossy part is decoded for now.
>>
>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>> ---
> [...]
>>  static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
>>  {
>>      OMAContext *oc  = s->priv_data;
>> -    AVStream *st    = s->streams[0];
>> -    int packet_size = st->codecpar->block_align;
>> -    int byte_rate   = st->codecpar->bit_rate >> 3;
>> -    int64_t pos     = avio_tell(s->pb);
>> -    int ret         = av_get_packet(s->pb, pkt, packet_size);
>> -
>> -    if (ret < packet_size)
>> -        pkt->flags |= AV_PKT_FLAG_CORRUPT;
>> -
>> -    if (ret < 0)
>> -        return ret;
>> -    if (!ret)
>> -        return AVERROR_EOF;
>> -
>> -    pkt->stream_index = 0;
>> -
>> -    if (pos >= oc->content_start && byte_rate > 0) {
>> -        pkt->pts =
>> -        pkt->dts = av_rescale(pos - oc->content_start,
>> st->time_base.den,
>> -                              byte_rate * (int64_t)st->time_base.num);
>> -    }
>> -
>> -    if (oc->encrypted) {
>> -        /* previous unencrypted block saved in IV for
>> -         * the next packet (CBC mode) */
>> -        if (ret == packet_size)
>> -            av_des_crypt(oc->av_des, pkt->data, pkt->data,
>> -                         (packet_size >> 3), oc->iv, 1);
>> -        else
>> -            memset(oc->iv, 0, 8);
>> -    }
>> -
>> -    return ret;
>> +    return oc->read_packet(s, pkt);
>>  }
>
> moving this into read_packet() could be done in a seperate patch
>
>
>>
>>  static int oma_read_probe(AVProbeData *p)
>> @@ -491,8 +571,14 @@ static int oma_read_seek(struct AVFormatContext *s,
>>                           int stream_index, int64_t timestamp, int flags)
>>  {
>>      OMAContext *oc = s->priv_data;
>> -    int64_t err = ff_pcm_read_seek(s, stream_index, timestamp, flags);
>> +    AVStream *st = s->streams[0];
>> +    int64_t err;
>> +
>> +    if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL ||
>> +        st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL)
>
>> +        return -1;
>
> should be a AVERROR code

This is not error, it makes seeking possible, using other error codes
is bad idea.

>
>
> thanks
>
> [...]
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> Those who would give up essential Liberty, to purchase a little
> temporary Safety, deserve neither Liberty nor Safety -- Benjamin Franklin
>
Michael Niedermayer Jan. 31, 2017, 5:15 p.m. UTC | #3
On Tue, Jan 31, 2017 at 04:17:19PM +0100, Paul B Mahol wrote:
> On 1/31/17, Michael Niedermayer <michaelni@gmx.at> wrote:
> > On Sun, Jan 29, 2017 at 06:39:21PM +0100, Paul B Mahol wrote:
> >> Only lossy part is decoded for now.
> >>
> >> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> >> ---
> > [...]
> >>  static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
> >>  {
> >>      OMAContext *oc  = s->priv_data;
> >> -    AVStream *st    = s->streams[0];
> >> -    int packet_size = st->codecpar->block_align;
> >> -    int byte_rate   = st->codecpar->bit_rate >> 3;
> >> -    int64_t pos     = avio_tell(s->pb);
> >> -    int ret         = av_get_packet(s->pb, pkt, packet_size);
> >> -
> >> -    if (ret < packet_size)
> >> -        pkt->flags |= AV_PKT_FLAG_CORRUPT;
> >> -
> >> -    if (ret < 0)
> >> -        return ret;
> >> -    if (!ret)
> >> -        return AVERROR_EOF;
> >> -
> >> -    pkt->stream_index = 0;
> >> -
> >> -    if (pos >= oc->content_start && byte_rate > 0) {
> >> -        pkt->pts =
> >> -        pkt->dts = av_rescale(pos - oc->content_start,
> >> st->time_base.den,
> >> -                              byte_rate * (int64_t)st->time_base.num);
> >> -    }
> >> -
> >> -    if (oc->encrypted) {
> >> -        /* previous unencrypted block saved in IV for
> >> -         * the next packet (CBC mode) */
> >> -        if (ret == packet_size)
> >> -            av_des_crypt(oc->av_des, pkt->data, pkt->data,
> >> -                         (packet_size >> 3), oc->iv, 1);
> >> -        else
> >> -            memset(oc->iv, 0, 8);
> >> -    }
> >> -
> >> -    return ret;
> >> +    return oc->read_packet(s, pkt);
> >>  }
> >
> > moving this into read_packet() could be done in a seperate patch
> >
> >
> >>
> >>  static int oma_read_probe(AVProbeData *p)
> >> @@ -491,8 +571,14 @@ static int oma_read_seek(struct AVFormatContext *s,
> >>                           int stream_index, int64_t timestamp, int flags)
> >>  {
> >>      OMAContext *oc = s->priv_data;
> >> -    int64_t err = ff_pcm_read_seek(s, stream_index, timestamp, flags);
> >> +    AVStream *st = s->streams[0];
> >> +    int64_t err;
> >> +
> >> +    if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL ||
> >> +        st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL)
> >
> >> +        return -1;
> >
> > should be a AVERROR code
> 
> This is not error, it makes seeking possible, using other error codes
> is bad idea.

It would be better to use a named identifier than a litteral number
normally we use AVERROR_*, i would argue that oma_read_seek() did not
seek so it didnt succeed but if you prefer this to be called something
else than AVERROR_* sure iam perfectly fine with what you prefer.

It was the use of a litteral number (which is also undocumented for
read_seek() except by code returning -1) that i wanted to comment on

[...]
Paul B Mahol Jan. 31, 2017, 5:29 p.m. UTC | #4
On 1/31/17, Michael Niedermayer <michaelni@gmx.at> wrote:
> On Tue, Jan 31, 2017 at 04:17:19PM +0100, Paul B Mahol wrote:
>> On 1/31/17, Michael Niedermayer <michaelni@gmx.at> wrote:
>> > On Sun, Jan 29, 2017 at 06:39:21PM +0100, Paul B Mahol wrote:
>> >> Only lossy part is decoded for now.
>> >>
>> >> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>> >> ---
>> > [...]
>> >>  static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
>> >>  {
>> >>      OMAContext *oc  = s->priv_data;
>> >> -    AVStream *st    = s->streams[0];
>> >> -    int packet_size = st->codecpar->block_align;
>> >> -    int byte_rate   = st->codecpar->bit_rate >> 3;
>> >> -    int64_t pos     = avio_tell(s->pb);
>> >> -    int ret         = av_get_packet(s->pb, pkt, packet_size);
>> >> -
>> >> -    if (ret < packet_size)
>> >> -        pkt->flags |= AV_PKT_FLAG_CORRUPT;
>> >> -
>> >> -    if (ret < 0)
>> >> -        return ret;
>> >> -    if (!ret)
>> >> -        return AVERROR_EOF;
>> >> -
>> >> -    pkt->stream_index = 0;
>> >> -
>> >> -    if (pos >= oc->content_start && byte_rate > 0) {
>> >> -        pkt->pts =
>> >> -        pkt->dts = av_rescale(pos - oc->content_start,
>> >> st->time_base.den,
>> >> -                              byte_rate *
>> >> (int64_t)st->time_base.num);
>> >> -    }
>> >> -
>> >> -    if (oc->encrypted) {
>> >> -        /* previous unencrypted block saved in IV for
>> >> -         * the next packet (CBC mode) */
>> >> -        if (ret == packet_size)
>> >> -            av_des_crypt(oc->av_des, pkt->data, pkt->data,
>> >> -                         (packet_size >> 3), oc->iv, 1);
>> >> -        else
>> >> -            memset(oc->iv, 0, 8);
>> >> -    }
>> >> -
>> >> -    return ret;
>> >> +    return oc->read_packet(s, pkt);
>> >>  }
>> >
>> > moving this into read_packet() could be done in a seperate patch
>> >
>> >
>> >>
>> >>  static int oma_read_probe(AVProbeData *p)
>> >> @@ -491,8 +571,14 @@ static int oma_read_seek(struct AVFormatContext
>> >> *s,
>> >>                           int stream_index, int64_t timestamp, int
>> >> flags)
>> >>  {
>> >>      OMAContext *oc = s->priv_data;
>> >> -    int64_t err = ff_pcm_read_seek(s, stream_index, timestamp,
>> >> flags);
>> >> +    AVStream *st = s->streams[0];
>> >> +    int64_t err;
>> >> +
>> >> +    if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL ||
>> >> +        st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL)
>> >
>> >> +        return -1;
>> >
>> > should be a AVERROR code
>>
>> This is not error, it makes seeking possible, using other error codes
>> is bad idea.
>
> It would be better to use a named identifier than a litteral number
> normally we use AVERROR_*, i would argue that oma_read_seek() did not
> seek so it didnt succeed but if you prefer this to be called something
> else than AVERROR_* sure iam perfectly fine with what you prefer.
>
> It was the use of a litteral number (which is also undocumented for
> read_seek() except by code returning -1) that i wanted to comment on

-1 means use generice seeking.
Michael Niedermayer Jan. 31, 2017, 6:02 p.m. UTC | #5
On Tue, Jan 31, 2017 at 06:29:03PM +0100, Paul B Mahol wrote:
> On 1/31/17, Michael Niedermayer <michaelni@gmx.at> wrote:
> > On Tue, Jan 31, 2017 at 04:17:19PM +0100, Paul B Mahol wrote:
> >> On 1/31/17, Michael Niedermayer <michaelni@gmx.at> wrote:
> >> > On Sun, Jan 29, 2017 at 06:39:21PM +0100, Paul B Mahol wrote:
> >> >> Only lossy part is decoded for now.
> >> >>
> >> >> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> >> >> ---
> >> > [...]
> >> >>  static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
> >> >>  {
> >> >>      OMAContext *oc  = s->priv_data;
> >> >> -    AVStream *st    = s->streams[0];
> >> >> -    int packet_size = st->codecpar->block_align;
> >> >> -    int byte_rate   = st->codecpar->bit_rate >> 3;
> >> >> -    int64_t pos     = avio_tell(s->pb);
> >> >> -    int ret         = av_get_packet(s->pb, pkt, packet_size);
> >> >> -
> >> >> -    if (ret < packet_size)
> >> >> -        pkt->flags |= AV_PKT_FLAG_CORRUPT;
> >> >> -
> >> >> -    if (ret < 0)
> >> >> -        return ret;
> >> >> -    if (!ret)
> >> >> -        return AVERROR_EOF;
> >> >> -
> >> >> -    pkt->stream_index = 0;
> >> >> -
> >> >> -    if (pos >= oc->content_start && byte_rate > 0) {
> >> >> -        pkt->pts =
> >> >> -        pkt->dts = av_rescale(pos - oc->content_start,
> >> >> st->time_base.den,
> >> >> -                              byte_rate *
> >> >> (int64_t)st->time_base.num);
> >> >> -    }
> >> >> -
> >> >> -    if (oc->encrypted) {
> >> >> -        /* previous unencrypted block saved in IV for
> >> >> -         * the next packet (CBC mode) */
> >> >> -        if (ret == packet_size)
> >> >> -            av_des_crypt(oc->av_des, pkt->data, pkt->data,
> >> >> -                         (packet_size >> 3), oc->iv, 1);
> >> >> -        else
> >> >> -            memset(oc->iv, 0, 8);
> >> >> -    }
> >> >> -
> >> >> -    return ret;
> >> >> +    return oc->read_packet(s, pkt);
> >> >>  }
> >> >
> >> > moving this into read_packet() could be done in a seperate patch
> >> >
> >> >
> >> >>
> >> >>  static int oma_read_probe(AVProbeData *p)
> >> >> @@ -491,8 +571,14 @@ static int oma_read_seek(struct AVFormatContext
> >> >> *s,
> >> >>                           int stream_index, int64_t timestamp, int
> >> >> flags)
> >> >>  {
> >> >>      OMAContext *oc = s->priv_data;
> >> >> -    int64_t err = ff_pcm_read_seek(s, stream_index, timestamp,
> >> >> flags);
> >> >> +    AVStream *st = s->streams[0];
> >> >> +    int64_t err;
> >> >> +
> >> >> +    if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL ||
> >> >> +        st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL)
> >> >
> >> >> +        return -1;
> >> >
> >> > should be a AVERROR code
> >>
> >> This is not error, it makes seeking possible, using other error codes
> >> is bad idea.
> >
> > It would be better to use a named identifier than a litteral number
> > normally we use AVERROR_*, i would argue that oma_read_seek() did not
> > seek so it didnt succeed but if you prefer this to be called something
> > else than AVERROR_* sure iam perfectly fine with what you prefer.
> >
> > It was the use of a litteral number (which is also undocumented for
> > read_seek() except by code returning -1) that i wanted to comment on
> 
> -1 means use generice seeking.

I think any negative return results in that unless disabled but its
long ago that i worked on this code.

-1 is kind of bad because it is not descriptive, a reader doesnt know
what that does also -1 == AVERROR(EPERM) here
so it cannot be distinguished from that error

i think we should add and use soemthing like
return AVERROR_USE_GENERIC_SEEK;
return FF_USE_GENERIC_SEEK;
return FALLBACK_TO_GENERIC_SEEK;
return WHATEVER_ELSE_ONE_LIKES_TO_CALL_IT;



[...]
diff mbox

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 43a6add..c5505db 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -191,8 +191,11 @@  OBJS-$(CONFIG_ASV2_DECODER)            += asvdec.o asv.o mpeg12data.o
 OBJS-$(CONFIG_ASV2_ENCODER)            += asvenc.o asv.o mpeg12data.o
 OBJS-$(CONFIG_ATRAC1_DECODER)          += atrac1.o atrac.o
 OBJS-$(CONFIG_ATRAC3_DECODER)          += atrac3.o atrac.o
+OBJS-$(CONFIG_ATRAC3AL_DECODER)        += atrac3.o atrac.o
 OBJS-$(CONFIG_ATRAC3P_DECODER)         += atrac3plusdec.o atrac3plus.o \
                                           atrac3plusdsp.o atrac.o
+OBJS-$(CONFIG_ATRAC3PAL_DECODER)       += atrac3plusdec.o atrac3plus.o \
+                                          atrac3plusdsp.o atrac.o
 OBJS-$(CONFIG_AURA_DECODER)            += cyuv.o
 OBJS-$(CONFIG_AURA2_DECODER)           += aura.o
 OBJS-$(CONFIG_AVRN_DECODER)            += avrndec.o mjpegdec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index f92b2b7..7220864 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -403,7 +403,9 @@  void avcodec_register_all(void)
     REGISTER_DECODER(APE,               ape);
     REGISTER_DECODER(ATRAC1,            atrac1);
     REGISTER_DECODER(ATRAC3,            atrac3);
+    REGISTER_DECODER(ATRAC3AL,          atrac3al);
     REGISTER_DECODER(ATRAC3P,           atrac3p);
+    REGISTER_DECODER(ATRAC3PAL,         atrac3pal);
     REGISTER_DECODER(BINKAUDIO_DCT,     binkaudio_dct);
     REGISTER_DECODER(BINKAUDIO_RDFT,    binkaudio_rdft);
     REGISTER_DECODER(BMV_AUDIO,         bmv_audio);
diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c
index ffd93e4..ec1416f 100644
--- a/libavcodec/atrac3.c
+++ b/libavcodec/atrac3.c
@@ -731,6 +731,40 @@  static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
     return 0;
 }
 
+static int al_decode_frame(AVCodecContext *avctx, const uint8_t *databuf,
+                           int size, float **out_samples)
+{
+    ATRAC3Context *q = avctx->priv_data;
+    int ret, i;
+
+    /* Set the bitstream reader at the start of a channel sound unit. */
+    init_get_bits(&q->gb, databuf, size * 8);
+    /* single channels */
+    /* Decode the channel sound units. */
+    for (i = 0; i < avctx->channels; i++) {
+        ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
+                                        out_samples[i], i, q->coding_mode);
+        if (ret != 0)
+            return ret;
+        while (i < avctx->channels && get_bits_left(&q->gb) > 6 && show_bits(&q->gb, 6) != 0x28) {
+            skip_bits(&q->gb, 1);
+        }
+    }
+
+    /* Apply the iQMF synthesis filter. */
+    for (i = 0; i < avctx->channels; i++) {
+        float *p1 = out_samples[i];
+        float *p2 = p1 + 256;
+        float *p3 = p2 + 256;
+        float *p4 = p3 + 256;
+        ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
+        ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
+        ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
+    }
+
+    return 0;
+}
+
 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
                                int *got_frame_ptr, AVPacket *avpkt)
 {
@@ -771,6 +805,28 @@  static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
     return avctx->block_align;
 }
 
+static int atrac3al_decode_frame(AVCodecContext *avctx, void *data,
+                                 int *got_frame_ptr, AVPacket *avpkt)
+{
+    AVFrame *frame = data;
+    int ret;
+
+    frame->nb_samples = SAMPLES_PER_FRAME;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+
+    ret = al_decode_frame(avctx, avpkt->data, avpkt->size,
+                          (float **)frame->extended_data);
+    if (ret) {
+        av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n");
+        return ret;
+    }
+
+    *got_frame_ptr = 1;
+
+    return avpkt->size;
+}
+
 static av_cold void atrac3_init_static_data(void)
 {
     int i;
@@ -807,7 +863,12 @@  static av_cold int atrac3_decode_init(AVCodecContext *avctx)
     static_init_done = 1;
 
     /* Take care of the codec-specific extradata. */
-    if (avctx->extradata_size == 14) {
+    if (avctx->codec_id == AV_CODEC_ID_ATRAC3AL) {
+        version           = 4;
+        samples_per_frame = SAMPLES_PER_FRAME * avctx->channels;
+        delay             = 0x88E;
+        q->coding_mode    = SINGLE;
+    } else if (avctx->extradata_size == 14) {
         /* Parse the extradata, WAV format */
         av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n",
                bytestream_get_le16(&edata_ptr));  // Unknown value always 1
@@ -937,3 +998,17 @@  AVCodec ff_atrac3_decoder = {
     .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
                                                         AV_SAMPLE_FMT_NONE },
 };
+
+AVCodec ff_atrac3al_decoder = {
+    .name             = "atrac3al",
+    .long_name        = NULL_IF_CONFIG_SMALL("ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"),
+    .type             = AVMEDIA_TYPE_AUDIO,
+    .id               = AV_CODEC_ID_ATRAC3AL,
+    .priv_data_size   = sizeof(ATRAC3Context),
+    .init             = atrac3_decode_init,
+    .close            = atrac3_decode_close,
+    .decode           = atrac3al_decode_frame,
+    .capabilities     = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
+    .sample_fmts      = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+                                                        AV_SAMPLE_FMT_NONE },
+};
diff --git a/libavcodec/atrac3plusdec.c b/libavcodec/atrac3plusdec.c
index ec2b1ad..ea67d31 100644
--- a/libavcodec/atrac3plusdec.c
+++ b/libavcodec/atrac3plusdec.c
@@ -384,7 +384,7 @@  static int atrac3p_decode_frame(AVCodecContext *avctx, void *data,
 
     *got_frame_ptr = 1;
 
-    return FFMIN(avctx->block_align, avpkt->size);
+    return avctx->codec_id == AV_CODEC_ID_ATRAC3P ? FFMIN(avctx->block_align, avpkt->size) : avpkt->size;
 }
 
 AVCodec ff_atrac3p_decoder = {
@@ -398,3 +398,15 @@  AVCodec ff_atrac3p_decoder = {
     .close          = atrac3p_decode_close,
     .decode         = atrac3p_decode_frame,
 };
+
+AVCodec ff_atrac3pal_decoder = {
+    .name           = "atrac3pal",
+    .long_name      = NULL_IF_CONFIG_SMALL("ATRAC3+ AL (Adaptive TRansform Acoustic Coding 3+ Advanced Lossless)"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_ATRAC3PAL,
+    .capabilities   = AV_CODEC_CAP_DR1,
+    .priv_data_size = sizeof(ATRAC3PContext),
+    .init           = atrac3p_decode_init,
+    .close          = atrac3p_decode_close,
+    .decode         = atrac3p_decode_frame,
+};
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 1e681e9..8f69f1e 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -603,6 +603,8 @@  enum AVCodecID {
     AV_CODEC_ID_XMA1,
     AV_CODEC_ID_XMA2,
     AV_CODEC_ID_DST,
+    AV_CODEC_ID_ATRAC3AL,
+    AV_CODEC_ID_ATRAC3PAL,
 
     /* subtitle codecs */
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 09d3c48..77c69a4 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2477,6 +2477,20 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .props     = AV_CODEC_PROP_LOSSY,
     },
     {
+        .id        = AV_CODEC_ID_ATRAC3PAL,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "atrac3pal",
+        .long_name = NULL_IF_CONFIG_SMALL("ATRAC3+ AL (Adaptive TRansform Acoustic Coding 3+ Advanced Lossless)"),
+        .props     = AV_CODEC_PROP_LOSSLESS,
+    },
+    {
+        .id        = AV_CODEC_ID_ATRAC3AL,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "atrac3al",
+        .long_name = NULL_IF_CONFIG_SMALL("ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"),
+        .props     = AV_CODEC_PROP_LOSSLESS,
+    },
+    {
         .id        = AV_CODEC_ID_EAC3,
         .type      = AVMEDIA_TYPE_AUDIO,
         .name      = "eac3",
diff --git a/libavformat/oma.c b/libavformat/oma.c
index 2702867..f7ae3c9 100644
--- a/libavformat/oma.c
+++ b/libavformat/oma.c
@@ -26,10 +26,12 @@ 
 const uint16_t ff_oma_srate_tab[8] = { 320, 441, 480, 882, 960, 0 };
 
 const AVCodecTag ff_oma_codec_tags[] = {
-    { AV_CODEC_ID_ATRAC3,      OMA_CODECID_ATRAC3  },
-    { AV_CODEC_ID_ATRAC3P,     OMA_CODECID_ATRAC3P },
-    { AV_CODEC_ID_MP3,         OMA_CODECID_MP3     },
-    { AV_CODEC_ID_PCM_S16BE,   OMA_CODECID_LPCM    },
+    { AV_CODEC_ID_ATRAC3,      OMA_CODECID_ATRAC3    },
+    { AV_CODEC_ID_ATRAC3P,     OMA_CODECID_ATRAC3P   },
+    { AV_CODEC_ID_MP3,         OMA_CODECID_MP3       },
+    { AV_CODEC_ID_PCM_S16BE,   OMA_CODECID_LPCM      },
+    { AV_CODEC_ID_ATRAC3PAL,   OMA_CODECID_ATRAC3PAL },
+    { AV_CODEC_ID_ATRAC3AL,    OMA_CODECID_ATRAC3AL  },
     { 0 },
 };
 
diff --git a/libavformat/oma.h b/libavformat/oma.h
index e2a187b..36fd012 100644
--- a/libavformat/oma.h
+++ b/libavformat/oma.h
@@ -35,6 +35,8 @@  enum {
     OMA_CODECID_MP3     = 3,
     OMA_CODECID_LPCM    = 4,
     OMA_CODECID_WMA     = 5,
+    OMA_CODECID_ATRAC3PAL = 33,
+    OMA_CODECID_ATRAC3AL  = 34,
 };
 
 extern const uint16_t ff_oma_srate_tab[8];
diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index 757ae53..b2a7788 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -75,6 +75,8 @@  typedef struct OMAContext {
     uint8_t e_val[8];
     uint8_t iv[8];
     struct AVDES *av_des;
+
+    int (*read_packet)(AVFormatContext *s, AVPacket *pkt);
 } OMAContext;
 
 static void hex_log(AVFormatContext *s, int level,
@@ -302,6 +304,82 @@  static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
     return 0;
 }
 
+static int read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    OMAContext *oc  = s->priv_data;
+    AVStream *st    = s->streams[0];
+    int packet_size = st->codecpar->block_align;
+    int byte_rate   = st->codecpar->bit_rate >> 3;
+    int64_t pos     = avio_tell(s->pb);
+    int ret         = av_get_packet(s->pb, pkt, packet_size);
+
+    if (ret < packet_size)
+        pkt->flags |= AV_PKT_FLAG_CORRUPT;
+
+    if (ret < 0)
+        return ret;
+    if (!ret)
+        return AVERROR_EOF;
+
+    pkt->stream_index = 0;
+
+    if (pos >= oc->content_start && byte_rate > 0) {
+        pkt->pts =
+        pkt->dts = av_rescale(pos - oc->content_start, st->time_base.den,
+                              byte_rate * (int64_t)st->time_base.num);
+    }
+
+    if (oc->encrypted) {
+        /* previous unencrypted block saved in IV for
+         * the next packet (CBC mode) */
+        if (ret == packet_size)
+            av_des_crypt(oc->av_des, pkt->data, pkt->data,
+                         (packet_size >> 3), oc->iv, 1);
+        else
+            memset(oc->iv, 0, 8);
+    }
+
+    return ret;
+}
+
+static int aal_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int64_t pos = avio_tell(s->pb);
+    int ret, type, pts;
+    int packet_size;
+    unsigned tag;
+
+    if (avio_feof(s->pb))
+        return AVERROR_EOF;
+
+    tag = avio_rb24(s->pb);
+    if (tag == 0)
+        return AVERROR_EOF;
+    else if (tag != MKBETAG(0,'B','L','K'))
+        return AVERROR_INVALIDDATA;
+
+    type = avio_r8(s->pb);
+    packet_size = avio_rb16(s->pb);
+    avio_skip(s->pb, 2);
+    pts = avio_rb32(s->pb);
+    avio_skip(s->pb, 12);
+    ret = av_get_packet(s->pb, pkt, packet_size);
+    if (ret < packet_size)
+        pkt->flags |= AV_PKT_FLAG_CORRUPT;
+
+    if (ret < 0)
+        return ret;
+    if (!ret)
+        return AVERROR_EOF;
+
+    pkt->stream_index = 0;
+    pkt->pos = pos;
+    pkt->pts = pts * 2048LL;
+    pkt->duration = 2048;
+
+    return ret;
+}
+
 static int oma_read_header(AVFormatContext *s)
 {
     int     ret, framesize, jsflag, samplerate;
@@ -347,6 +425,8 @@  static int oma_read_header(AVFormatContext *s)
     st->codecpar->codec_id   = ff_codec_get_id(ff_oma_codec_tags,
                                                st->codecpar->codec_tag);
 
+    oc->read_packet = read_packet;
+
     switch (buf[32]) {
     case OMA_CODECID_ATRAC3:
         samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7] * 100;
@@ -417,6 +497,39 @@  static int oma_read_header(AVFormatContext *s)
             av_get_bits_per_sample(st->codecpar->codec_id);
         avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
         break;
+    case OMA_CODECID_ATRAC3AL:
+        samplerate = ff_oma_srate_tab[(codec_params >> 9) & 7] * 100;
+        if (!samplerate) {
+            av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        st->codecpar->channels    = 2;
+        st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
+        st->codecpar->sample_rate = samplerate;
+        avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
+        oc->read_packet = aal_read_packet;
+        framesize = 4096;
+        break;
+    case OMA_CODECID_ATRAC3PAL:
+        channel_id = (codec_params >> 19) & 7;
+        if (!channel_id) {
+            av_log(s, AV_LOG_ERROR,
+                   "Invalid ATRAC-AL channel id: %"PRIu32"\n", channel_id);
+            return AVERROR_INVALIDDATA;
+        }
+        st->codecpar->channel_layout = ff_oma_chid_to_native_layout[channel_id - 1];
+        st->codecpar->channels       = ff_oma_chid_to_num_channels[channel_id - 1];
+        samplerate = ff_oma_srate_tab[(codec_params >> 16) & 7] * 100;
+        if (!samplerate) {
+            av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
+            return AVERROR_INVALIDDATA;
+        }
+        st->codecpar->sample_rate = samplerate;
+        avpriv_set_pts_info(st, 64, 1, samplerate);
+        oc->read_packet = aal_read_packet;
+        framesize = 4096;
+        break;
     default:
         av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n", buf[32]);
         return AVERROR(ENOSYS);
@@ -427,43 +540,10 @@  static int oma_read_header(AVFormatContext *s)
     return 0;
 }
 
-
 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     OMAContext *oc  = s->priv_data;
-    AVStream *st    = s->streams[0];
-    int packet_size = st->codecpar->block_align;
-    int byte_rate   = st->codecpar->bit_rate >> 3;
-    int64_t pos     = avio_tell(s->pb);
-    int ret         = av_get_packet(s->pb, pkt, packet_size);
-
-    if (ret < packet_size)
-        pkt->flags |= AV_PKT_FLAG_CORRUPT;
-
-    if (ret < 0)
-        return ret;
-    if (!ret)
-        return AVERROR_EOF;
-
-    pkt->stream_index = 0;
-
-    if (pos >= oc->content_start && byte_rate > 0) {
-        pkt->pts =
-        pkt->dts = av_rescale(pos - oc->content_start, st->time_base.den,
-                              byte_rate * (int64_t)st->time_base.num);
-    }
-
-    if (oc->encrypted) {
-        /* previous unencrypted block saved in IV for
-         * the next packet (CBC mode) */
-        if (ret == packet_size)
-            av_des_crypt(oc->av_des, pkt->data, pkt->data,
-                         (packet_size >> 3), oc->iv, 1);
-        else
-            memset(oc->iv, 0, 8);
-    }
-
-    return ret;
+    return oc->read_packet(s, pkt);
 }
 
 static int oma_read_probe(AVProbeData *p)
@@ -491,8 +571,14 @@  static int oma_read_seek(struct AVFormatContext *s,
                          int stream_index, int64_t timestamp, int flags)
 {
     OMAContext *oc = s->priv_data;
-    int64_t err = ff_pcm_read_seek(s, stream_index, timestamp, flags);
+    AVStream *st = s->streams[0];
+    int64_t err;
+
+    if (st->codecpar->codec_id == AV_CODEC_ID_ATRAC3PAL ||
+        st->codecpar->codec_id == AV_CODEC_ID_ATRAC3AL)
+        return -1;
 
+    err = ff_pcm_read_seek(s, stream_index, timestamp, flags);
     if (!oc->encrypted)
         return err;