Revision ec0a6889

View differences:

Changelog
55 55
- Win64 support for optimized asm functions
56 56
- MJPEG/AVI1 to JPEG/JFIF bitstream filter
57 57
- ASS subtitle encoder and decoder
58
- IEC 61937 encapsulation for E-AC3 and TrueHD (for HDMI passthrough)
58
- IEC 61937 encapsulation for E-AC3, TrueHD, DTS-HD (for HDMI passthrough)
59 59
- overlay filter added
60 60
- rename aspect filter to setdar, and pixelaspect to setsar
61 61
- IEC 61937 demuxer
libavformat/spdifenc.c
48 48
#include "spdif.h"
49 49
#include "libavcodec/ac3.h"
50 50
#include "libavcodec/dca.h"
51
#include "libavcodec/dcadata.h"
51 52
#include "libavcodec/aacadtsdec.h"
53
#include "libavutil/opt.h"
52 54

  
53 55
typedef struct IEC61937Context {
56
    const AVClass *av_class;
54 57
    enum IEC61937DataType data_type;///< burst info - reference to type of payload of the data-burst
55 58
    int length_code;                ///< length code in bits or bytes, depending on data type
56 59
    int pkt_offset;                 ///< data burst repetition period in bytes
......
68 71
    int hd_buf_count;               ///< number of frames in the hd audio buffer
69 72
    int hd_buf_filled;              ///< amount of bytes in the hd audio buffer
70 73

  
74
    int dtshd_skip;                 ///< counter used for skipping DTS-HD frames
75

  
76
    /* AVOptions: */
77
    int dtshd_rate;
78
    int dtshd_fallback;
79

  
71 80
    /// function, which generates codec dependent header information.
72 81
    /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
73 82
    int (*header_info) (AVFormatContext *s, AVPacket *pkt);
74 83
} IEC61937Context;
75 84

  
85
static const AVOption options[] = {
86
{ "dtshd_rate", "mux complete DTS frames in HD mode at the specified IEC958 rate (in Hz, default 0=disabled)", offsetof(IEC61937Context, dtshd_rate), FF_OPT_TYPE_INT, 0, 0, 768000, AV_OPT_FLAG_ENCODING_PARAM },
87
{ "dtshd_fallback_time", "min secs to strip HD for after an overflow (-1: till the end, default 60)", offsetof(IEC61937Context, dtshd_fallback), FF_OPT_TYPE_INT, 60, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
88
{ NULL },
89
};
90

  
91
static const AVClass class = { "spdif", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
76 92

  
77 93
static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
78 94
{
......
115 131
    return 0;
116 132
}
117 133

  
134
/*
135
 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
136
 * periods; longer repetition periods allow for longer packets and therefore
137
 * higher bitrate. Longer repetition periods mean that the constant bitrate of
138
 * the outputted IEC 61937 stream is higher.
139
 * The repetition period is measured in IEC 60958 frames (4 bytes).
140
 */
141
enum {
142
    DTS4_REP_PER_512   = 0x0,
143
    DTS4_REP_PER_1024  = 0x1,
144
    DTS4_REP_PER_2048  = 0x2,
145
    DTS4_REP_PER_4096  = 0x3,
146
    DTS4_REP_PER_8192  = 0x4,
147
    DTS4_REP_PER_16384 = 0x5,
148
};
149

  
150
static int spdif_dts4_subtype(int period)
151
{
152
    switch (period) {
153
    case 512:   return DTS4_REP_PER_512;
154
    case 1024:  return DTS4_REP_PER_1024;
155
    case 2048:  return DTS4_REP_PER_2048;
156
    case 4096:  return DTS4_REP_PER_4096;
157
    case 8192:  return DTS4_REP_PER_8192;
158
    case 16384: return DTS4_REP_PER_16384;
159
    }
160
    return -1;
161
}
162

  
163
static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size,
164
                             int sample_rate, int blocks)
165
{
166
    IEC61937Context *ctx = s->priv_data;
167
    static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
168
    int pkt_size = pkt->size;
169
    int period;
170
    int subtype;
171

  
172
    if (!core_size) {
173
        av_log(s, AV_LOG_ERROR, "HD mode not supported for this format\n");
174
        return AVERROR(EINVAL);
175
    }
176

  
177
    if (!sample_rate) {
178
        av_log(s, AV_LOG_ERROR, "Unknown DTS sample rate for HD\n");
179
        return AVERROR_INVALIDDATA;
180
    }
181

  
182
    period = ctx->dtshd_rate * (blocks << 5) / sample_rate;
183
    subtype = spdif_dts4_subtype(period);
184

  
185
    if (subtype < 0) {
186
        av_log(s, AV_LOG_ERROR, "Specified HD rate of %d Hz would require an "
187
               "impossible repetition period of %d for the current DTS stream"
188
               " (blocks = %d, sample rate = %d)\n", ctx->dtshd_rate, period,
189
               blocks << 5, sample_rate);
190
        return AVERROR(EINVAL);
191
    }
192

  
193
    /* set pkt_offset and DTS IV subtype according to the requested output
194
     * rate */
195
    ctx->pkt_offset = period * 4;
196
    ctx->data_type = IEC61937_DTSHD | subtype << 8;
197

  
198
    /* If the bitrate is too high for transmitting at the selected
199
     * repetition period setting, strip DTS-HD until a good amount
200
     * of consecutive non-overflowing HD frames have been observed.
201
     * This generally only happens if the caller is cramming a Master
202
     * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
203
    if (sizeof(dtshd_start_code) + 2 + pkt_size
204
            > ctx->pkt_offset - BURST_HEADER_SIZE && core_size) {
205
        if (!ctx->dtshd_skip)
206
            av_log(s, AV_LOG_WARNING, "DTS-HD bitrate too high, "
207
                                      "temporarily sending core only\n");
208
        if (ctx->dtshd_fallback > 0)
209
            ctx->dtshd_skip = sample_rate * ctx->dtshd_fallback / (blocks << 5);
210
        else
211
            /* skip permanently (-1) or just once (0) */
212
            ctx->dtshd_skip = 1;
213
    }
214
    if (ctx->dtshd_skip && core_size) {
215
        pkt_size = core_size;
216
        if (ctx->dtshd_fallback >= 0)
217
            --ctx->dtshd_skip;
218
    }
219

  
220
    ctx->out_bytes   = sizeof(dtshd_start_code) + 2 + pkt_size;
221
    ctx->length_code = ctx->out_bytes;
222

  
223
    ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size,
224
                                  ctx->out_bytes);
225
    if (!ctx->hd_buf)
226
        return AVERROR(ENOMEM);
227

  
228
    ctx->out_buf = ctx->hd_buf;
229

  
230
    memcpy(ctx->hd_buf, dtshd_start_code, sizeof(dtshd_start_code));
231
    AV_WB16(ctx->hd_buf + sizeof(dtshd_start_code), pkt_size);
232
    memcpy(ctx->hd_buf + sizeof(dtshd_start_code) + 2, pkt->data, pkt_size);
233

  
234
    return 0;
235
}
236

  
118 237
static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
119 238
{
120 239
    IEC61937Context *ctx = s->priv_data;
121 240
    uint32_t syncword_dts = AV_RB32(pkt->data);
122 241
    int blocks;
242
    int sample_rate = 0;
243
    int core_size = 0;
244

  
245
    if (pkt->size < 9)
246
        return AVERROR_INVALIDDATA;
123 247

  
124 248
    switch (syncword_dts) {
125 249
    case DCA_MARKER_RAW_BE:
126 250
        blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
251
        core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
252
        sample_rate = dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
127 253
        break;
128 254
    case DCA_MARKER_RAW_LE:
129 255
        blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
......
138 264
            (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
139 265
        ctx->extra_bswap = 1;
140 266
        break;
267
    case DCA_HD_MARKER:
268
        /* We only handle HD frames that are paired with core. However,
269
           sometimes DTS-HD streams with core have a stray HD frame without
270
           core in the beginning of the stream. */
271
        av_log(s, AV_LOG_ERROR, "stray DTS-HD frame\n");
272
        return AVERROR_INVALIDDATA;
141 273
    default:
142 274
        av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
143 275
        return AVERROR_INVALIDDATA;
144 276
    }
145 277
    blocks++;
278

  
279
    if (ctx->dtshd_rate)
280
        /* DTS type IV output requested */
281
        return spdif_header_dts4(s, pkt, core_size, sample_rate, blocks);
282

  
146 283
    switch (blocks) {
147 284
    case  512 >> 5: ctx->data_type = IEC61937_DTS1; break;
148 285
    case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
......
152 289
               blocks << 5);
153 290
        return AVERROR(ENOSYS);
154 291
    }
292

  
293
    /* discard extraneous data by default */
294
    if (core_size && core_size < pkt->size) {
295
        ctx->out_bytes = core_size;
296
        ctx->length_code = core_size << 3;
297
    }
298

  
155 299
    ctx->pkt_offset = blocks << 7;
156 300

  
157 301
    if (ctx->out_bytes == ctx->pkt_offset) {
......
159 303
         * preamble as it would not fit in there. This is the case for dts
160 304
         * discs and dts-in-wav. */
161 305
        ctx->use_preamble = 0;
306
    } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
307
        av_log_ask_for_sample(s, "Unrecognized large DTS frame.");
308
        /* This will fail with a "bitrate too high" in the caller */
162 309
    }
163 310

  
164 311
    return 0;
......
393 540
    spdif_write_header,
394 541
    spdif_write_packet,
395 542
    spdif_write_trailer,
543
    .priv_class = &class,
396 544
};
libavformat/version.h
25 25

  
26 26
#define LIBAVFORMAT_VERSION_MAJOR 52
27 27
#define LIBAVFORMAT_VERSION_MINOR 99
28
#define LIBAVFORMAT_VERSION_MICRO  0
28
#define LIBAVFORMAT_VERSION_MICRO  1
29 29

  
30 30
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
31 31
                                               LIBAVFORMAT_VERSION_MINOR, \

Also available in: Unified diff