omaenc.c 3.65 KB
Newer Older
1 2 3 4 5
/*
 * Sony OpenMG (OMA) muxer
 *
 * Copyright (c) 2011 Michael Karcher
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20 21 22 23 24 25 26 27 28 29 30 31 32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avformat.h"
#include "avio_internal.h"
#include "id3v2.h"
#include "internal.h"
#include "oma.h"
#include "rawenc.h"

static av_cold int oma_write_header(AVFormatContext *s)
{
    int i;
33
    AVCodecParameters *par;
34 35 36
    int srate_index;
    int isjointstereo;

37
    par = s->streams[0]->codecpar;
38 39 40 41 42
    /* check for support of the format first */

    for (srate_index = 0; ; srate_index++) {
        if (ff_oma_srate_tab[srate_index] == 0) {
            av_log(s, AV_LOG_ERROR, "Sample rate %d not supported in OpenMG audio\n",
43
                   par->sample_rate);
44 45 46
            return AVERROR(EINVAL);
        }

47
        if (ff_oma_srate_tab[srate_index] * 100 == par->sample_rate)
48 49 50 51
            break;
    }

    /* Metadata; OpenMG does not support ID3v2.4 */
52
    ff_id3v2_write_simple(s, 3, ID3v2_EA3_MAGIC);
53 54 55 56 57 58 59 60

    ffio_wfourcc(s->pb, "EA3\0");
    avio_w8(s->pb, EA3_HEADER_SIZE >> 7);
    avio_w8(s->pb, EA3_HEADER_SIZE & 0x7F);
    avio_wl16(s->pb, 0xFFFF);       /* not encrypted */
    for (i = 0; i < 6; i++)
        avio_wl32(s->pb, 0);        /* Padding + DRM id */

61
    switch (par->codec_tag) {
62
    case OMA_CODECID_ATRAC3:
63
        if (par->channels != 2) {
64
            av_log(s, AV_LOG_ERROR, "ATRAC3 in OMA is only supported with 2 channels\n");
65 66
            return AVERROR(EINVAL);
        }
67 68 69 70
        if (par->extradata_size == 14) /* WAV format extradata */
            isjointstereo = par->extradata[6] != 0;
        else if(par->extradata_size == 10) /* RM format extradata */
            isjointstereo = par->extradata[8] == 0x12;
71 72 73 74 75 76 77
        else {
            av_log(s, AV_LOG_ERROR, "ATRAC3: Unsupported extradata size\n");
            return AVERROR(EINVAL);
        }
        avio_wb32(s->pb, (OMA_CODECID_ATRAC3 << 24) |
                         (isjointstereo << 17) |
                         (srate_index << 13) |
78
                         (par->block_align/8));
79 80 81 82
        break;
    case OMA_CODECID_ATRAC3P:
        avio_wb32(s->pb, (OMA_CODECID_ATRAC3P << 24) |
                         (srate_index << 13) |
83 84
                         (par->channels << 10) |
                         (par->block_align/8 - 1));
85 86
        break;
    default:
87 88
        av_log(s, AV_LOG_ERROR, "unsupported codec tag %s for write\n",
               av_fourcc2str(par->codec_tag));
89
        return AVERROR(EINVAL);
90 91 92 93 94 95 96 97 98 99 100 101
    }
    for (i = 0; i < (EA3_HEADER_SIZE - 36)/4; i++)
        avio_wl32(s->pb, 0);        /* Padding */

    return 0;
}

AVOutputFormat ff_oma_muxer = {
    .name              = "oma",
    .long_name         = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
    .mime_type         = "audio/x-oma",
    .extensions        = "oma",
102
    .audio_codec       = AV_CODEC_ID_ATRAC3,
103 104 105
    .write_header      = oma_write_header,
    .write_packet      = ff_raw_write_packet,
    .codec_tag         = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
106
    .flags             = AVFMT_NOTIMESTAMPS,
107
};