FFmpeg
mjpeg2jpeg_bsf.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/mem.h"
31 #include "libavutil/intreadwrite.h"
32 
33 #include "avcodec.h"
34 #include "bsf.h"
35 #include "jpegtables.h"
36 #include "mjpeg.h"
37 
38 static const uint8_t jpeg_header[] = {
39  0xff, 0xd8, // SOI
40  0xff, 0xe0, // APP0
41  0x00, 0x10, // APP0 header size (including
42  // this field, but excluding preceding)
43  0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0'
44  0x01, 0x01, // version
45  0x00, // bits per type
46  0x00, 0x00, // X density
47  0x00, 0x00, // Y density
48  0x00, // X thumbnail size
49  0x00, // Y thumbnail size
50 };
51 
52 static const int dht_segment_size = 420;
53 static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
54 static const uint8_t dht_segment_frag[] = {
55  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
56  0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
57  0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
58 };
59 
60 static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
61 {
62  memcpy(buf, src, size);
63  return buf + size;
64 }
65 
67 {
72  *(buf++) = 0x10;
75  *(buf++) = 0x11;
78  return buf;
79 }
80 
82 {
83  AVPacket *in;
84  int ret = 0;
85  int input_skip, output_size;
86  uint8_t *output;
87 
89  if (ret < 0)
90  return ret;
91 
92  if (in->size < 12) {
93  av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
95  goto fail;
96  }
97  if (AV_RB16(in->data) != 0xffd8) {
98  av_log(ctx, AV_LOG_ERROR, "input is not MJPEG\n");
100  goto fail;
101  }
102  if (in->data[2] == 0xff && in->data[3] == APP0) {
103  input_skip = (in->data[4] << 8) + in->data[5] + 4;
104  } else {
105  input_skip = 2;
106  }
107  if (in->size < input_skip) {
108  av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
110  goto fail;
111  }
112  output_size = in->size - input_skip +
113  sizeof(jpeg_header) + dht_segment_size;
114  ret = av_new_packet(out, output_size);
115  if (ret < 0)
116  goto fail;
117 
118  output = out->data;
119 
122  output = append(output, in->data + input_skip, in->size - input_skip);
123 
125  if (ret < 0)
126  goto fail;
127 
128 fail:
129  if (ret < 0)
131  av_packet_free(&in);
132  return ret;
133 }
134 
135 static const enum AVCodecID codec_ids[] = {
137 };
138 
140  .name = "mjpeg2jpeg",
141  .filter = mjpeg2jpeg_filter,
142  .codec_ids = codec_ids,
143 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
jpegtables.h
mjpeg.h
append
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
Definition: mjpeg2jpeg_bsf.c:60
out
FILE * out
Definition: movenc.c:54
avpriv_mjpeg_bits_ac_luminance
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
AVBitStreamFilter::name
const char * name
Definition: avcodec.h:5813
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
avpriv_mjpeg_val_ac_luminance
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
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:217
avpriv_mjpeg_bits_dc_luminance
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
AVBSFContext
The bitstream filter state.
Definition: avcodec.h:5763
append_dht_segment
static uint8_t * append_dht_segment(uint8_t *buf)
Definition: mjpeg2jpeg_bsf.c:66
bsf.h
fail
#define fail()
Definition: checkasm.h:120
jpeg_header
static const uint8_t jpeg_header[]
Definition: mjpeg2jpeg_bsf.c:38
src
#define src
Definition: vp8dsp.c:254
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
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: avpacket.c:86
avpriv_mjpeg_val_dc
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
dht_segment_frag
static const uint8_t dht_segment_frag[]
Definition: mjpeg2jpeg_bsf.c:54
ctx
AVFormatContext * ctx
Definition: movenc.c:48
mjpeg2jpeg_filter
static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
Definition: mjpeg2jpeg_bsf.c:81
dht_segment_size
static const int dht_segment_size
Definition: mjpeg2jpeg_bsf.c:52
avpriv_mjpeg_val_ac_chrominance
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
error.h
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
codec_ids
static enum AVCodecID codec_ids[]
Definition: mjpeg2jpeg_bsf.c:135
size
int size
Definition: twinvq_data.h:11134
ff_mjpeg2jpeg_bsf
const AVBitStreamFilter ff_mjpeg2jpeg_bsf
Definition: mjpeg2jpeg_bsf.c:139
AV_CODEC_ID_MJPEG
@ AV_CODEC_ID_MJPEG
Definition: avcodec.h:225
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:565
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: avcodec.h:216
uint8_t
uint8_t
Definition: audio_convert.c:194
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVBitStreamFilter
Definition: avcodec.h:5812
dht_segment_head
static const uint8_t dht_segment_head[]
Definition: mjpeg2jpeg_bsf.c:53
APP0
@ APP0
Definition: mjpeg.h:79
mem.h
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
avpriv_mjpeg_bits_ac_chrominance
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
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:94