Libav
omaenc.c
Go to the documentation of this file.
1 /*
2  * Sony OpenMG (OMA) muxer
3  *
4  * Copyright (c) 2011 Michael Karcher
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "id3v2.h"
26 #include "internal.h"
27 #include "oma.h"
28 #include "rawenc.h"
29 
31 {
32  int i;
33  AVCodecContext *format;
34  int srate_index;
35  int isjointstereo;
36 
37  format = s->streams[0]->codec;
38  /* check for support of the format first */
39 
40  for (srate_index = 0; ; srate_index++) {
41  if (ff_oma_srate_tab[srate_index] == 0) {
42  av_log(s, AV_LOG_ERROR, "Sample rate %d not supported in OpenMG audio\n",
43  format->sample_rate);
44  return AVERROR(EINVAL);
45  }
46 
47  if (ff_oma_srate_tab[srate_index] * 100 == format->sample_rate)
48  break;
49  }
50 
51  /* Metadata; OpenMG does not support ID3v2.4 */
53 
54  ffio_wfourcc(s->pb, "EA3\0");
55  avio_w8(s->pb, EA3_HEADER_SIZE >> 7);
56  avio_w8(s->pb, EA3_HEADER_SIZE & 0x7F);
57  avio_wl16(s->pb, 0xFFFF); /* not encrypted */
58  for (i = 0; i < 6; i++)
59  avio_wl32(s->pb, 0); /* Padding + DRM id */
60 
61  switch(format->codec_tag) {
62  case OMA_CODECID_ATRAC3:
63  if (format->channels != 2) {
64  av_log(s, AV_LOG_ERROR, "ATRAC3 in OMA is only supported with 2 channels");
65  return AVERROR(EINVAL);
66  }
67  if (format->extradata_size == 14) /* WAV format extradata */
68  isjointstereo = format->extradata[6] != 0;
69  else if(format->extradata_size == 10) /* RM format extradata */
70  isjointstereo = format->extradata[8] == 0x12;
71  else {
72  av_log(s, AV_LOG_ERROR, "ATRAC3: Unsupported extradata size\n");
73  return AVERROR(EINVAL);
74  }
75  avio_wb32(s->pb, (OMA_CODECID_ATRAC3 << 24) |
76  (isjointstereo << 17) |
77  (srate_index << 13) |
78  (format->block_align/8));
79  break;
81  avio_wb32(s->pb, (OMA_CODECID_ATRAC3P << 24) |
82  (srate_index << 13) |
83  (format->channels << 10) |
84  (format->block_align/8 - 1));
85  break;
86  default:
87  av_log(s, AV_LOG_ERROR, "OMA: unsupported codec tag %d for write\n",
88  format->codec_tag);
89  }
90  for (i = 0; i < (EA3_HEADER_SIZE - 36)/4; i++)
91  avio_wl32(s->pb, 0); /* Padding */
92 
93  return 0;
94 }
95 
97  .name = "oma",
98  .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
99  .mime_type = "audio/x-oma",
100  .extensions = "oma",
101  .audio_codec = AV_CODEC_ID_ATRAC3,
102  .write_header = oma_write_header,
103  .write_packet = ff_raw_write_packet,
104  .codec_tag = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
105  .flags = AVFMT_NOTIMESTAMPS,
106 };
static av_cold int oma_write_header(AVFormatContext *s)
Definition: omaenc.c:30
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:336
int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version, const char *magic)
Write an ID3v2 tag containing all global metadata from s.
Definition: id3v2enc.c:232
#define ID3v2_EA3_MAGIC
Definition: oma.h:29
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1828
const AVCodecTag ff_oma_codec_tags[]
Definition: oma.c:28
Format I/O context.
Definition: avformat.h:922
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
#define av_cold
Definition: attributes.h:66
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:67
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
const char * name
Definition: avformat.h:446
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:26
#define EA3_HEADER_SIZE
Definition: oma.h:28
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:416
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
main external API structure.
Definition: avcodec.h:1050
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
int extradata_size
Definition: avcodec.h:1165
Main libavformat public API header.
AVOutputFormat ff_oma_muxer
Definition: omaenc.c:96
int channels
number of audio channels
Definition: avcodec.h:1792
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
const uint16_t ff_oma_srate_tab[8]
Definition: oma.c:26