Statistics
| Branch: | Revision:

ffmpeg / libavformat / vocenc.c @ 56a10009

History | View | Annotate | Download (3.05 KB)

1 5d244b29 Aurelien Jacobs
/*
2
 * Creative Voice File muxer.
3
 * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
4
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 5d244b29 Aurelien Jacobs
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 5d244b29 Aurelien Jacobs
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 5d244b29 Aurelien Jacobs
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 5d244b29 Aurelien Jacobs
 */
21
22
#include "voc.h"
23 80b39e1c Francesco Lavra
#include "internal.h"
24 5d244b29 Aurelien Jacobs
25
26
typedef struct voc_enc_context {
27
    int param_written;
28 e998ba4f Aurelien Jacobs
} VocEncContext;
29 5d244b29 Aurelien Jacobs
30
static int voc_write_header(AVFormatContext *s)
31
{
32 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
33 5d244b29 Aurelien Jacobs
    const int header_size = 26;
34
    const int version = 0x0114;
35
36
    if (s->nb_streams != 1
37 72415b2a Stefano Sabatini
        || s->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
38 9c0edaaf Ramiro Polla
        return AVERROR_PATCHWELCOME;
39 5d244b29 Aurelien Jacobs
40 77eb5504 Anton Khirnov
    avio_write(pb, ff_voc_magic, sizeof(ff_voc_magic) - 1);
41
    avio_wl16(pb, header_size);
42
    avio_wl16(pb, version);
43
    avio_wl16(pb, ~version + 0x1234);
44 5d244b29 Aurelien Jacobs
45
    return 0;
46
}
47
48
static int voc_write_packet(AVFormatContext *s, AVPacket *pkt)
49
{
50 e998ba4f Aurelien Jacobs
    VocEncContext *voc = s->priv_data;
51 5d244b29 Aurelien Jacobs
    AVCodecContext *enc = s->streams[0]->codec;
52 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
53 5d244b29 Aurelien Jacobs
54
    if (!voc->param_written) {
55 4014fe37 Aurelien Jacobs
        if (enc->codec_tag > 0xFF) {
56 77eb5504 Anton Khirnov
            avio_w8(pb, VOC_TYPE_NEW_VOICE_DATA);
57
            avio_wl24(pb, pkt->size + 12);
58
            avio_wl32(pb, enc->sample_rate);
59
            avio_w8(pb, enc->bits_per_coded_sample);
60
            avio_w8(pb, enc->channels);
61
            avio_wl16(pb, enc->codec_tag);
62
            avio_wl32(pb, 0);
63 5d244b29 Aurelien Jacobs
        } else {
64
            if (s->streams[0]->codec->channels > 1) {
65 77eb5504 Anton Khirnov
                avio_w8(pb, VOC_TYPE_EXTENDED);
66
                avio_wl24(pb, 4);
67
                avio_wl16(pb, 65536-256000000/(enc->sample_rate*enc->channels));
68
                avio_w8(pb, enc->codec_tag);
69
                avio_w8(pb, enc->channels - 1);
70 5d244b29 Aurelien Jacobs
            }
71 77eb5504 Anton Khirnov
            avio_w8(pb, VOC_TYPE_VOICE_DATA);
72
            avio_wl24(pb, pkt->size + 2);
73
            avio_w8(pb, 256 - 1000000 / enc->sample_rate);
74
            avio_w8(pb, enc->codec_tag);
75 5d244b29 Aurelien Jacobs
        }
76
        voc->param_written = 1;
77
    } else {
78 77eb5504 Anton Khirnov
        avio_w8(pb, VOC_TYPE_VOICE_DATA_CONT);
79
        avio_wl24(pb, pkt->size);
80 5d244b29 Aurelien Jacobs
    }
81
82 77eb5504 Anton Khirnov
    avio_write(pb, pkt->data, pkt->size);
83 5d244b29 Aurelien Jacobs
    return 0;
84
}
85
86
static int voc_write_trailer(AVFormatContext *s)
87
{
88 77eb5504 Anton Khirnov
    avio_w8(s->pb, 0);
89 5d244b29 Aurelien Jacobs
    return 0;
90
}
91
92 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_voc_muxer = {
93 5d244b29 Aurelien Jacobs
    "voc",
94 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Creative Voice file format"),
95 5d244b29 Aurelien Jacobs
    "audio/x-voc",
96
    "voc",
97 e998ba4f Aurelien Jacobs
    sizeof(VocEncContext),
98 5d244b29 Aurelien Jacobs
    CODEC_ID_PCM_U8,
99
    CODEC_ID_NONE,
100
    voc_write_header,
101
    voc_write_packet,
102
    voc_write_trailer,
103 c1854592 Reimar Döffinger
    .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
104 5d244b29 Aurelien Jacobs
};