]> git.sesse.net Git - ffmpeg/blob - libavcodec/mjpeg2jpeg_bsf.c
ppc: Centralize compiler-specific altivec.h #include handling in one place
[ffmpeg] / libavcodec / mjpeg2jpeg_bsf.c
1 /*
2  * MJPEG/AVI1 to JPEG/JFIF bitstream format filter
3  * Copyright (c) 2010 Adrian Daerr and Nicolas George
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * 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  * Libav is distributed in the hope that it will be useful,
13  * 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  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /*
23  * Adapted from mjpeg2jpeg.c, with original copyright:
24  * Paris 2010 Adrian Daerr, public domain
25  */
26
27 #include <string.h>
28
29 #include "libavutil/error.h"
30 #include "libavutil/mem.h"
31
32 #include "avcodec.h"
33 #include "bsf.h"
34 #include "jpegtables.h"
35
36 static const uint8_t jpeg_header[] = {
37     0xff, 0xd8,                     // SOI
38     0xff, 0xe0,                     // APP0
39     0x00, 0x10,                     // APP0 header size (including
40                                     // this field, but excluding preceding)
41     0x4a, 0x46, 0x49, 0x46, 0x00,   // ID string 'JFIF\0'
42     0x01, 0x01,                     // version
43     0x00,                           // bits per type
44     0x00, 0x00,                     // X density
45     0x00, 0x00,                     // Y density
46     0x00,                           // X thumbnail size
47     0x00,                           // Y thumbnail size
48 };
49
50 static const int dht_segment_size = 420;
51 static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
52 static const uint8_t dht_segment_frag[] = {
53     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
54     0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
55     0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
56 };
57
58 static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
59 {
60     memcpy(buf, src, size);
61     return buf + size;
62 }
63
64 static uint8_t *append_dht_segment(uint8_t *buf)
65 {
66     buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
67     buf = append(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16);
68     buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
69     buf = append(buf, avpriv_mjpeg_val_dc, 12);
70     *(buf++) = 0x10;
71     buf = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
72     buf = append(buf, avpriv_mjpeg_val_ac_luminance, 162);
73     *(buf++) = 0x11;
74     buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
75     buf = append(buf, avpriv_mjpeg_val_ac_chrominance, 162);
76     return buf;
77 }
78
79 static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
80 {
81     AVPacket *in;
82     int ret = 0;
83     int input_skip, output_size;
84     uint8_t *output;
85
86     ret = ff_bsf_get_packet(ctx, &in);
87
88     if (in->size < 12) {
89         av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
90         ret = AVERROR_INVALIDDATA;
91         goto fail;
92     }
93     if (memcmp("AVI1", in->data + 6, 4)) {
94         av_log(ctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
95         ret = AVERROR_INVALIDDATA;
96         goto fail;
97     }
98
99     input_skip = (in->data[4] << 8) + in->data[5] + 4;
100     if (in->size < input_skip) {
101         av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
102         ret = AVERROR_INVALIDDATA;
103         goto fail;
104     }
105     output_size = in->size - input_skip +
106                   sizeof(jpeg_header) + dht_segment_size;
107     ret = av_new_packet(out, output_size);
108     if (ret < 0)
109         goto fail;
110
111     output = out->data;
112
113     output = append(output, jpeg_header, sizeof(jpeg_header));
114     output = append_dht_segment(output);
115     output = append(output, in->data + input_skip, in->size - input_skip);
116
117     ret = av_packet_copy_props(out, in);
118     if (ret < 0)
119         goto fail;
120
121 fail:
122     if (ret < 0)
123         av_packet_unref(out);
124     av_packet_free(&in);
125     return ret;
126 }
127
128 const AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
129     .name           = "mjpeg2jpeg",
130     .filter         = mjpeg2jpeg_filter,
131 };