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 #include "libavutil/intreadwrite.h"
32 
33 #include "avcodec.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 
66 {
67  buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
68  buf = append(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16);
69  buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
70  buf = append(buf, avpriv_mjpeg_val_dc, 12);
71  *(buf++) = 0x10;
72  buf = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
73  buf = append(buf, avpriv_mjpeg_val_ac_luminance, 162);
74  *(buf++) = 0x11;
75  buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
76  buf = append(buf, avpriv_mjpeg_val_ac_chrominance, 162);
77  return buf;
78 }
79 
81  AVCodecContext *avctx, const char *args,
82  uint8_t **poutbuf, int *poutbuf_size,
83  const uint8_t *buf, int buf_size,
84  int keyframe)
85 {
86  int input_skip, output_size;
87  uint8_t *output, *out;
88 
89  if (buf_size < 12) {
90  av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
91  return AVERROR_INVALIDDATA;
92  }
93  if (AV_RB16(buf) != 0xffd8) {
94  av_log(avctx, AV_LOG_ERROR, "input is not MJPEG\n");
95  return AVERROR_INVALIDDATA;
96  }
97  if (buf[2] == 0xff && buf[3] == APP0) {
98  input_skip = (buf[4] << 8) + buf[5] + 4;
99  } else {
100  input_skip = 2;
101  }
102  if (buf_size < input_skip) {
103  av_log(avctx, AV_LOG_ERROR, "input is truncated\n");
104  return AVERROR_INVALIDDATA;
105  }
106  output_size = buf_size - input_skip +
107  sizeof(jpeg_header) + dht_segment_size;
108  output = out = av_malloc(output_size);
109  if (!output)
110  return AVERROR(ENOMEM);
111  out = append(out, jpeg_header, sizeof(jpeg_header));
112  out = append_dht_segment(out);
113  out = append(out, buf + input_skip, buf_size - input_skip);
114  *poutbuf = output;
115  *poutbuf_size = output_size;
116  return 1;
117 }
118 
120  .name = "mjpeg2jpeg",
121  .filter = mjpeg2jpeg_filter,
122 };
#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
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:87
MJPEG encoder and decoder.
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:5152
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
Definition: mjpeg.h:79
#define src
Definition: vp9dsp.c:530
FILE * out
Definition: movenc-test.c:54
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1532
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
static const uint8_t dht_segment_head[]