FFmpeg
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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "mux.h"
28 #include "oma.h"
29 #include "rawenc.h"
30 
32 {
33  AVCodecParameters *par;
34  int srate_index;
35  int isjointstereo;
36 
37  par = s->streams[0]->codecpar;
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  par->sample_rate);
44  return AVERROR(EINVAL);
45  }
46 
47  if (ff_oma_srate_tab[srate_index] * 100 == par->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  ffio_fill(s->pb, 0, 6 * 4); /* Padding + DRM id */
59 
60  switch (par->codec_tag) {
61  case OMA_CODECID_ATRAC3:
62  if (par->ch_layout.nb_channels != 2) {
63  av_log(s, AV_LOG_ERROR, "ATRAC3 in OMA is only supported with 2 channels\n");
64  return AVERROR(EINVAL);
65  }
66  if (par->extradata_size == 14) /* WAV format extradata */
67  isjointstereo = par->extradata[6] != 0;
68  else if(par->extradata_size == 10) /* RM format extradata */
69  isjointstereo = par->extradata[8] == 0x12;
70  else {
71  av_log(s, AV_LOG_ERROR, "ATRAC3: Unsupported extradata size\n");
72  return AVERROR(EINVAL);
73  }
74  avio_wb32(s->pb, (OMA_CODECID_ATRAC3 << 24) |
75  (isjointstereo << 17) |
76  (srate_index << 13) |
77  (par->block_align/8));
78  break;
80  avio_wb32(s->pb, (OMA_CODECID_ATRAC3P << 24) |
81  (srate_index << 13) |
82  (par->ch_layout.nb_channels << 10) |
83  (par->block_align/8 - 1));
84  break;
85  default:
86  av_log(s, AV_LOG_ERROR, "unsupported codec tag %s for write\n",
87  av_fourcc2str(par->codec_tag));
88  return AVERROR(EINVAL);
89  }
90  ffio_fill(s->pb, 0, EA3_HEADER_SIZE - 36); /* Padding */
91 
92  return 0;
93 }
94 
96  .p.name = "oma",
97  .p.long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
98  .p.mime_type = "audio/x-oma",
99  .p.extensions = "oma",
100  .p.video_codec = AV_CODEC_ID_NONE,
101  .p.audio_codec = AV_CODEC_ID_ATRAC3,
102  .p.subtitle_codec = AV_CODEC_ID_NONE,
103  .write_header = oma_write_header,
104  .write_packet = ff_raw_write_packet,
105  .p.codec_tag = ff_oma_codec_tags_list,
106  .p.flags = AVFMT_NOTIMESTAMPS,
107  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
108 };
oma.h
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:124
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
ff_oma_codec_tags_list
const AVCodecTag *const ff_oma_codec_tags_list[]
Definition: oma.c:38
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
id3v2.h
ff_oma_srate_tab
const uint16_t ff_oma_srate_tab[8]
Definition: oma.c:26
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:437
OMA_CODECID_ATRAC3
@ OMA_CODECID_ATRAC3
Definition: oma.h:35
AV_CODEC_ID_ATRAC3
@ AV_CODEC_ID_ATRAC3
Definition: codec_id.h:471
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
OMA_CODECID_ATRAC3P
@ OMA_CODECID_ATRAC3P
Definition: oma.h:36
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
FFOutputFormat
Definition: mux.h:61
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:187
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
ff_oma_muxer
const FFOutputFormat ff_oma_muxer
Definition: omaenc.c:95
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
ID3v2_EA3_MAGIC
#define ID3v2_EA3_MAGIC
Definition: oma.h:31
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:365
oma_write_header
static av_cold int oma_write_header(AVFormatContext *s)
Definition: omaenc.c:31
rawenc.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
avformat.h
EA3_HEADER_SIZE
#define EA3_HEADER_SIZE
Definition: oma.h:30
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_id3v2_write_simple
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:445
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:345
mux.h