FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
32 #include "avcodec.h"
33 #include "jpegtables.h"
34 
35 static const uint8_t jpeg_header[] = {
36  0xff, 0xd8, // SOI
37  0xff, 0xe0, // APP0
38  0x00, 0x10, // APP0 header size (including
39  // this field, but excluding preceding)
40  0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0'
41  0x01, 0x01, // version
42  0x00, // bits per type
43  0x00, 0x00, // X density
44  0x00, 0x00, // Y density
45  0x00, // X thumbnail size
46  0x00, // Y thumbnail size
47 };
48 
49 static const int dht_segment_size = 420;
50 static const uint8_t dht_segment_head[] = { 0xFF, 0xC4, 0x01, 0xA2, 0x00 };
51 static const uint8_t dht_segment_frag[] = {
52  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
53  0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
54  0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
55 };
56 
57 static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
58 {
59  memcpy(buf, src, size);
60  return buf + size;
61 }
62 
64 {
65  buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
66  buf = append(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16);
67  buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
68  buf = append(buf, avpriv_mjpeg_val_dc, 12);
69  *(buf++) = 0x10;
70  buf = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
71  buf = append(buf, avpriv_mjpeg_val_ac_luminance, 162);
72  *(buf++) = 0x11;
73  buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
74  buf = append(buf, avpriv_mjpeg_val_ac_chrominance, 162);
75  return buf;
76 }
77 
79  AVCodecContext *avctx, const char *args,
80  uint8_t **poutbuf, int *poutbuf_size,
81  const uint8_t *buf, int buf_size,
82  int keyframe)
83 {
84  int input_skip, output_size;
85  uint8_t *output, *out;
86 
87  if (buf_size < 12) {
88  av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
89  return AVERROR_INVALIDDATA;
90  }
91  if (memcmp("AVI1", buf + 6, 4)) {
92  av_log(avctx, AV_LOG_ERROR, "input is not MJPEG/AVI1\n");
93  return AVERROR_INVALIDDATA;
94  }
95  input_skip = (buf[4] << 8) + buf[5] + 4;
96  if (buf_size < input_skip) {
97  av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
98  return AVERROR_INVALIDDATA;
99  }
100  output_size = buf_size - input_skip +
101  sizeof(jpeg_header) + dht_segment_size;
102  output = out = av_malloc(output_size);
103  if (!output)
104  return AVERROR(ENOMEM);
105  out = append(out, jpeg_header, sizeof(jpeg_header));
106  out = append_dht_segment(out);
107  out = append(out, buf + input_skip, buf_size - input_skip);
108  *poutbuf = output;
109  *poutbuf_size = output_size;
110  return 1;
111 }
112 
114  .name = "mjpeg2jpeg",
115  .filter = mjpeg2jpeg_filter,
116 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
memory handling functions
static const uint8_t dht_segment_frag[]
AVBitStreamFilter ff_mjpeg2jpeg_bsf
static int mjpeg2jpeg_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
uint8_t
#define av_malloc(s)
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
const char * name
Definition: avcodec.h:5374
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
error code definitions
#define AVERROR(e)
Definition: error.h:43
Libavcodec external API header.
AVS_Value src
Definition: avisynth_c.h:482
main external API structure.
Definition: avcodec.h:1501
static uint8_t * append_dht_segment(uint8_t *buf)
void * buf
Definition: avisynth_c.h:553
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
static const uint8_t jpeg_header[]
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
static const int dht_segment_size
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static const uint8_t dht_segment_head[]