FFmpeg
mjpeg2jpeg.c
Go to the documentation of this file.
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/intreadwrite.h"
31 
32 #include "bsf.h"
33 #include "bsf_internal.h"
34 #include "jpegtables.h"
35 #include "mjpeg.h"
36 
37 static const uint8_t jpeg_header[] = {
38  0xff, 0xd8, // SOI
39  0xff, 0xe0, // APP0
40  0x00, 0x10, // APP0 header size (including
41  // this field, but excluding preceding)
42  0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0'
43  0x01, 0x01, // version
44  0x00, // bits per type
45  0x00, 0x00, // X density
46  0x00, 0x00, // Y density
47  0x00, // X thumbnail size
48  0x00, // Y thumbnail size
49 };
50 
51 static const int dht_segment_size = 420;
52 static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
53 static const uint8_t dht_segment_frag[] = {
54  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
55  0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
56  0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
57 };
58 
59 static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
60 {
61  memcpy(buf, src, size);
62  return buf + size;
63 }
64 
65 static uint8_t *append_dht_segment(uint8_t *buf)
66 {
67  buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
68  buf = append(buf, ff_mjpeg_bits_dc_luminance + 1, 16);
69  buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
70  buf = append(buf, ff_mjpeg_val_dc, 12);
71  *(buf++) = 0x10;
72  buf = append(buf, ff_mjpeg_bits_ac_luminance + 1, 16);
73  buf = append(buf, ff_mjpeg_val_ac_luminance, 162);
74  *(buf++) = 0x11;
75  buf = append(buf, ff_mjpeg_bits_ac_chrominance + 1, 16);
76  buf = append(buf, ff_mjpeg_val_ac_chrominance, 162);
77  return buf;
78 }
79 
81 {
82  AVPacket *in;
83  int ret = 0;
84  int input_skip, output_size;
85  uint8_t *output;
86 
87  ret = ff_bsf_get_packet(ctx, &in);
88  if (ret < 0)
89  return ret;
90 
91  if (in->size < 12) {
92  av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
94  goto fail;
95  }
96  if (AV_RB16(in->data) != 0xffd8) {
97  av_log(ctx, AV_LOG_ERROR, "input is not MJPEG\n");
99  goto fail;
100  }
101  if (in->data[2] == 0xff && in->data[3] == APP0) {
102  input_skip = (in->data[4] << 8) + in->data[5] + 4;
103  } else {
104  input_skip = 2;
105  }
106  if (in->size < input_skip) {
107  av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
109  goto fail;
110  }
111  output_size = in->size - input_skip +
112  sizeof(jpeg_header) + dht_segment_size;
113  ret = av_new_packet(out, output_size);
114  if (ret < 0)
115  goto fail;
116 
117  output = out->data;
118 
121  output = append(output, in->data + input_skip, in->size - input_skip);
122 
124  if (ret < 0)
125  goto fail;
126 
127 fail:
128  if (ret < 0)
130  av_packet_free(&in);
131  return ret;
132 }
133 
134 static const enum AVCodecID codec_ids[] = {
136 };
137 
139  .p.name = "mjpeg2jpeg",
140  .p.codec_ids = codec_ids,
141  .filter = mjpeg2jpeg_filter,
142 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:429
jpegtables.h
mjpeg.h
bsf_internal.h
out
FILE * out
Definition: movenc.c:55
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
append
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
Definition: mjpeg2jpeg.c:59
jpeg_header
static const uint8_t jpeg_header[]
Definition: mjpeg2jpeg.c:37
AVPacket::data
uint8_t * data
Definition: packet.h:539
ff_mjpeg_val_dc
const uint8_t ff_mjpeg_val_dc[]
Definition: jpegtabs.h:34
ff_mjpeg_bits_ac_chrominance
const uint8_t ff_mjpeg_bits_ac_chrominance[]
Definition: jpegtabs.h:66
ff_bsf_get_packet
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:235
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
bsf.h
ff_mjpeg2jpeg_bsf
const FFBitStreamFilter ff_mjpeg2jpeg_bsf
Definition: mjpeg2jpeg.c:138
fail
#define fail()
Definition: checkasm.h:189
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
intreadwrite.h
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
dht_segment_frag
static const uint8_t dht_segment_frag[]
Definition: mjpeg2jpeg.c:53
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FFBitStreamFilter
Definition: bsf_internal.h:27
append_dht_segment
static uint8_t * append_dht_segment(uint8_t *buf)
Definition: mjpeg2jpeg.c:65
mjpeg2jpeg_filter
static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
Definition: mjpeg2jpeg.c:80
ff_mjpeg_val_ac_chrominance
const uint8_t ff_mjpeg_val_ac_chrominance[]
Definition: jpegtabs.h:69
codec_ids
static enum AVCodecID codec_ids[]
Definition: mjpeg2jpeg.c:134
error.h
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
dht_segment_head
static const uint8_t dht_segment_head[]
Definition: mjpeg2jpeg.c:52
ff_mjpeg_val_ac_luminance
const uint8_t ff_mjpeg_val_ac_luminance[]
Definition: jpegtabs.h:42
AVPacket::size
int size
Definition: packet.h:540
ff_mjpeg_bits_ac_luminance
const uint8_t ff_mjpeg_bits_ac_luminance[]
Definition: jpegtabs.h:40
size
int size
Definition: twinvq_data.h:10344
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:59
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: packet.c:392
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
ret
ret
Definition: filter_design.txt:187
APP0
@ APP0
Definition: mjpeg.h:79
ff_mjpeg_bits_dc_luminance
const FF_VISIBILITY_PUSH_HIDDEN uint8_t ff_mjpeg_bits_dc_luminance[]
Definition: jpegtabs.h:32
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
dht_segment_size
static const int dht_segment_size
Definition: mjpeg2jpeg.c:51
src
#define src
Definition: vp8dsp.c:248
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98