Coverage Report

Created: 2026-12-01 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/jpeg.c
Line
Count
Source
1
/*****************************************************************************
2
 * jpeg.c: jpeg decoder module making use of libjpeg.
3
 *****************************************************************************
4
 * Copyright (C) 2013-2014,2016 VLC authors and VideoLAN
5
 *
6
 * Authors: Maxim Bublis <b@codemonkey.ru>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
#ifdef HAVE_CONFIG_H
24
# include "config.h"
25
#endif
26
27
#include <vlc_common.h>
28
#include <vlc_configuration.h>
29
#include <vlc_plugin.h>
30
#include <vlc_codec.h>
31
#include <vlc_charset.h>
32
#include <vlc_ancillary.h>
33
34
#include <jpeglib.h>
35
#include <setjmp.h>
36
#include <stdckdint.h>
37
38
#define ENC_CFG_PREFIX "sout-jpeg-"
39
#define ENC_QUALITY_TEXT N_("Quality level")
40
#define ENC_QUALITY_LONGTEXT N_("Quality level " \
41
    "for encoding (this can enlarge or reduce output image size).")
42
43
44
/*
45
 * jpeg common descriptor
46
 */
47
struct jpeg_sys_t
48
{
49
    /* libjpeg error handler manager */
50
    struct jpeg_error_mgr err;
51
52
    /* setjmp buffer for internal libjpeg error handling */
53
    jmp_buf setjmp_buffer;
54
55
    vlc_object_t *p_obj;
56
};
57
58
/*
59
 * jpeg decoder descriptor
60
 */
61
typedef struct
62
{
63
    struct jpeg_sys_t common;
64
65
    JSAMPARRAY p_row_pointers;
66
    struct jpeg_decompress_struct p_jpeg;
67
} decoder_sys_t;
68
69
static int  OpenDecoder(vlc_object_t *);
70
static void CloseDecoder(vlc_object_t *);
71
72
static int DecodeBlock(decoder_t *, block_t *);
73
74
/*
75
 * jpeg encoder descriptor
76
 */
77
typedef struct
78
{
79
    struct jpeg_sys_t common;
80
81
    struct jpeg_compress_struct p_jpeg;
82
83
    size_t i_blocksize;
84
    int i_quality;
85
} encoder_sys_t;
86
87
static const char * const ppsz_enc_options[] = {
88
    "quality",
89
    NULL
90
};
91
92
static int  OpenEncoder(vlc_object_t *);
93
static void CloseEncoder(encoder_t *);
94
95
static block_t *EncodeBlock(encoder_t *, picture_t *);
96
97
/*
98
 * Module descriptor
99
 */
100
178
vlc_module_begin()
101
89
    set_subcategory(SUBCAT_INPUT_VCODEC)
102
    /* decoder main module */
103
89
    set_description(N_("JPEG image decoder"))
104
89
    set_capability("video decoder", 1000)
105
178
    set_callbacks(OpenDecoder, CloseDecoder)
106
89
    add_shortcut("jpeg")
107
108
89
#ifdef ENABLE_SOUT
109
    /* video encoder submodule */
110
89
    add_submodule()
111
89
    add_shortcut("jpeg")
112
89
    set_section(N_("Encoding"), NULL)
113
89
    set_description(N_("JPEG video encoder"))
114
89
    set_capability("video encoder", 1000)
115
89
    set_callback(OpenEncoder)
116
89
    add_integer_with_range(ENC_CFG_PREFIX "quality", 95, 0, 100,
117
89
                           ENC_QUALITY_TEXT, ENC_QUALITY_LONGTEXT)
118
89
#endif
119
    /* image encoder submodule */
120
89
    add_submodule()
121
89
    add_shortcut("jpeg")
122
89
    set_section(N_("Encoding"), NULL)
123
89
    set_description(N_("JPEG image encoder"))
124
89
    set_capability("image encoder", 1000)
125
89
    set_callback(OpenEncoder)
126
89
vlc_module_end()
127
128
129
/*
130
 * Exit error handler for libjpeg
131
 */
132
static void user_error_exit(j_common_ptr p_jpeg)
133
1.65k
{
134
1.65k
    struct jpeg_sys_t *p_sys = p_jpeg->client_data;
135
1.65k
    p_jpeg->err->output_message(p_jpeg);
136
1.65k
    longjmp(p_sys->setjmp_buffer, 1);
137
1.65k
}
138
139
/*
140
 * Emit message error handler for libjpeg
141
 */
142
static void user_error_message(j_common_ptr p_jpeg)
143
2.05k
{
144
2.05k
    char error_msg[JMSG_LENGTH_MAX];
145
146
2.05k
    struct jpeg_sys_t *p_sys = p_jpeg->client_data;
147
2.05k
    p_jpeg->err->format_message(p_jpeg, error_msg);
148
2.05k
    msg_Err(p_sys->p_obj, "%s", error_msg);
149
2.05k
}
150
151
/*
152
 * Probe the decoder and return score
153
 */
154
static int OpenDecoder(vlc_object_t *p_this)
155
456k
{
156
456k
    decoder_t *p_dec = (decoder_t *)p_this;
157
158
456k
    if (p_dec->fmt_in->i_codec != VLC_CODEC_JPEG)
159
449k
    {
160
449k
        return VLC_EGENERIC;
161
449k
    }
162
163
    /* Allocate the memory needed to store the decoder's structure */
164
6.44k
    decoder_sys_t *p_sys = malloc(sizeof(decoder_sys_t));
165
6.44k
    if (p_sys == NULL)
166
0
    {
167
0
        return VLC_ENOMEM;
168
0
    }
169
170
6.44k
    p_dec->p_sys = p_sys;
171
172
6.44k
    p_sys->p_jpeg.client_data = &p_sys->common;
173
6.44k
    p_sys->common.p_obj = p_this;
174
175
6.44k
    p_sys->p_jpeg.err = jpeg_std_error(&p_sys->common.err);
176
6.44k
    p_sys->common.err.error_exit = user_error_exit;
177
6.44k
    p_sys->common.err.output_message = user_error_message;
178
179
    /* Set callbacks */
180
6.44k
    p_dec->pf_decode = DecodeBlock;
181
182
6.44k
    p_dec->fmt_out.i_codec =
183
6.44k
    p_dec->fmt_out.video.i_chroma = VLC_CODEC_RGB24;
184
6.44k
    p_dec->fmt_out.video.transfer  = TRANSFER_FUNC_SRGB;
185
6.44k
    p_dec->fmt_out.video.space     = COLOR_SPACE_SRGB;
186
6.44k
    p_dec->fmt_out.video.primaries = COLOR_PRIMARIES_SRGB;
187
6.44k
    p_dec->fmt_out.video.color_range = COLOR_RANGE_FULL;
188
189
6.44k
    return VLC_SUCCESS;
190
6.44k
}
191
192
/*
193
 * The following two functions are used to return 16 and 32 bit values from
194
 * the EXIF tag structure. That structure is borrowed from TIFF files and may be
195
 * in big endian or little endian format. The boolean b_bigEndian parameter
196
 * is TRUE if the EXIF data is in big endian format, and FALSE for little endian
197
 * Case Little Endian EXIF tag / Little Endian machine
198
 *   - just memcpy the tag structure into the value to return
199
 * Case Little Endian EXIF tag / Big Endian machine
200
 *    - memcpy the tag structure into value, and bswap it
201
 * Case Little Endian EXIF tag / Big Endian machine
202
 *   - memcpy the tag structure into value, and bswap it
203
 * Case Big Endian EXIF tag / Big Endian machine
204
 *   - just memcpy the tag structure into the value to return
205
 *
206
 * While there are byte manipulation functions in vlc_common.h, none of them
207
 * address that format of the data supplied may be either big or little endian.
208
 *
209
 * The claim is made that this is the best way to do it. Can you do better?
210
*/
211
212
7
#define G_LITTLE_ENDIAN     1234
213
5.52k
#define G_BIG_ENDIAN        4321
214
215
typedef unsigned int uint;
216
typedef unsigned short ushort;
217
218
static uint16_t
219
3.97k
de_get16( const void * ptr, uint endian ) {
220
3.97k
    return (endian == G_BIG_ENDIAN) ? GetWBE(ptr) : GetWLE(ptr);
221
3.97k
}
222
223
static uint32_t
224
785
de_get32( const void * ptr, uint endian ) {
225
785
    return (endian == G_BIG_ENDIAN) ? GetDWBE(ptr) : GetDWLE(ptr);
226
785
}
227
228
static bool getRDFFloat(const char *psz_rdf, float *out, const char *psz_var)
229
0
{
230
0
    char *p_start = strcasestr(psz_rdf, psz_var);
231
0
    if (p_start == NULL)
232
0
        return false;
233
234
0
    size_t varlen = strlen(psz_var);
235
0
    p_start += varlen;
236
0
    char *p_end = NULL;
237
    /* XML style */
238
0
    if (p_start[0] == '>')
239
0
    {
240
0
        p_start += 1;
241
0
        p_end = strchr(p_start, '<');
242
0
    }
243
0
    else if (p_start[0] == '=' && p_start[1] == '"')
244
0
    {
245
0
        p_start += 2;
246
0
        p_end = strchr(p_start, '"');
247
0
    }
248
0
    if (unlikely(p_end == NULL || p_end == p_start + 1))
249
0
        return false;
250
251
0
    *out = vlc_strtof_c(p_start, NULL);
252
0
    return true;
253
0
}
254
255
24.3k
#define EXIF_JPEG_MARKER    0xE1
256
921
#define EXIF_XMP_STRING     "http://ns.adobe.com/xap/1.0/\000"
257
258
/* read XMP metadata for projection according to
259
 * https://developers.google.com/streetview/spherical-metadata */
260
static bool jpeg_ParseXMP(const uint8_t *p_buf, size_t i_buf,
261
                          video_format_t *fmt)
262
1
{
263
1
    if(i_buf < 29)
264
0
        return false;
265
266
    /* Allocate temp, zero terminated buffer */
267
    /* Fixme: custom memicmp */
268
    /* or Fixme: Who's not compliant to XML attributes case ? */
269
1
    char *psz_rdf = malloc(i_buf - 29 + 1);
270
1
    if (unlikely(psz_rdf == NULL))
271
0
        return false;
272
1
    memcpy(psz_rdf, p_buf + 29, i_buf - 29);
273
1
    psz_rdf[i_buf - 29] = '\0';
274
275
1
    if (!strcasestr(psz_rdf, "ProjectionType=\"equirectangular\"") &&
276
1
        !strcasestr(psz_rdf, "ProjectionType>equirectangular"))
277
1
    return false;
278
279
0
    fmt->projection_mode = PROJECTION_MODE_EQUIRECTANGULAR;
280
281
    /* pose handling */
282
0
    float yaw = 0.f, pitch = 0.f, roll = 0.f;
283
0
    float value;
284
0
    if (getRDFFloat(psz_rdf, &value, "PoseHeadingDegrees") &&
285
0
        value >= 0.f && value <= 360.f)
286
0
        yaw = value;
287
288
0
    getRDFFloat(psz_rdf, &pitch, "PosePitchDegrees");
289
0
    getRDFFloat(psz_rdf, &roll, "PoseRollDegrees");
290
291
    /* initial view */
292
0
    getRDFFloat(psz_rdf, &yaw, "InitialViewHeadingDegrees");
293
0
    getRDFFloat(psz_rdf, &pitch, "InitialViewPitchDegrees");
294
0
    getRDFFloat(psz_rdf, &roll, "InitialViewRollDegrees");
295
296
0
    if (getRDFFloat(psz_rdf, &value, "InitialHorizontalFOVDegrees") &&
297
0
        value >= FIELD_OF_VIEW_DEGREES_MIN && value <= FIELD_OF_VIEW_DEGREES_MAX)
298
0
        fmt->pose.fov = value;
299
300
0
    vlc_viewpoint_from_euler(&fmt->pose, yaw, pitch, roll);
301
302
0
    free(psz_rdf);
303
304
0
    return true;
305
1
}
306
307
static void jpeg_FillProjection(j_decompress_ptr cinfo, video_format_t *fmt)
308
170
{
309
170
    for (jpeg_saved_marker_ptr cmarker = cinfo->marker_list;
310
6.23k
                               cmarker != NULL;
311
6.06k
                               cmarker = cmarker->next)
312
6.06k
    {
313
6.06k
        if(cmarker->marker == EXIF_JPEG_MARKER &&
314
1.09k
           cmarker->data_length >= 32 &&
315
921
           !memcmp(cmarker->data, EXIF_XMP_STRING, 29) &&
316
1
           jpeg_ParseXMP(cmarker->data, cmarker->data_length, fmt))
317
0
           break; /* found projection marker */
318
6.06k
    }
319
170
}
320
321
/*
322
 * Look through the meta data in the libjpeg decompress structure to determine
323
 * if an EXIF Orientation tag is present. If so return true and sets orientation
324
 *  value (1-8), otherwise return false
325
 *
326
 * This function is based on the function get_orientation in io-jpeg.c, part of
327
 * the GdkPixbuf library, licensed under LGPLv2+.
328
 *   Copyright (C) 1999 Michael Zucchi, The Free Software Foundation
329
*/
330
1.84k
#define EXIF_IDENT_STRING   "Exif\0" /* Exif\000\000 */
331
static bool jpeg_ParseExifApp1( const uint8_t *p_buf, size_t i_buf, int *orientation )
332
779
{
333
334
779
    uint i;                    /* index into working buffer */
335
779
    ushort tag_type;           /* endianed tag type extracted from tiff header */
336
779
    uint ret;                  /* Return value */
337
779
    uint offset;               /* de-endianed offset in various situations */
338
779
    uint tags;                 /* number of tags in current ifd */
339
779
    uint type;                 /* de-endianed type of tag */
340
779
    uint count;                /* de-endianed count of elements in a tag */
341
779
    uint tiff = 0;             /* offset to active tiff header */
342
779
    uint endian = 0;           /* detected endian of data */
343
344
779
    const char leth[] = { 0x49, 0x49, 0x2a, 0x00 }; /* Little endian TIFF header */
345
779
    const char beth[] = { 0x4d, 0x4d, 0x00, 0x2a }; /* Big endian TIFF header */
346
347
3.18k
    #define EXIF_ORIENT_TAGID   0x112
348
349
779
    if (i_buf < 32)
350
0
        return false;
351
352
    /* Check for TIFF header and catch endianness */
353
779
    i = 0;
354
355
    /* Just skip data until TIFF header - it should be within 16 bytes from marker start.
356
       Normal structure relative to APP1 marker -
357
            0x0000: APP1 marker entry = 2 bytes
358
            0x0002: APP1 length entry = 2 bytes
359
            0x0004: Exif Identifier entry = 6 bytes
360
            0x000A: Start of TIFF header (Byte order entry) - 4 bytes
361
                    - This is what we look for, to determine endianness.
362
            0x000E: 0th IFD offset pointer - 4 bytes
363
364
            exif_marker->data points to the first data after the APP1 marker
365
            and length entries, which is the exif identification string.
366
            The TIFF header should thus normally be found at i=6, below,
367
            and the pointer to IFD0 will be at 6+4 = 10.
368
    */
369
370
5.54k
    while ( i < 16 )
371
5.53k
    {
372
        /* Little endian TIFF header */
373
5.53k
        if ( memcmp( &p_buf[i], leth, 4 ) == 0 )
374
7
        {
375
7
            endian = G_LITTLE_ENDIAN;
376
7
        }
377
        /* Big endian TIFF header */
378
5.52k
        else
379
5.52k
        if ( memcmp( &p_buf[i], beth, 4 ) == 0 )
380
763
        {
381
763
            endian = G_BIG_ENDIAN;
382
763
        }
383
        /* Keep looking through buffer */
384
4.76k
        else
385
4.76k
        {
386
4.76k
            i++;
387
4.76k
            continue;
388
4.76k
        }
389
        /* We have found either big or little endian TIFF header */
390
770
        tiff = i;
391
770
        break;
392
5.53k
    }
393
394
    /* So did we find a TIFF header or did we just hit end of buffer? */
395
779
    if ( tiff == 0 )
396
9
        return false;
397
398
    /* Read out the offset pointer to IFD0 */
399
770
    offset = de_get32( &p_buf[i] + 4, endian );
400
770
    i = i + offset;
401
402
    /* Check that we still are within the buffer and can read the tag count */
403
404
770
    if ( i > i_buf - 2 )
405
6
        return false;
406
407
    /* Find out how many tags we have in IFD0. As per the TIFF spec, the first
408
       two bytes of the IFD contain a count of the number of tags. */
409
764
    tags = de_get16( &p_buf[i], endian );
410
764
    i = i + 2;
411
412
    /* Check that we still have enough data for all tags to check. The tags
413
       are listed in consecutive 12-byte blocks. The tag ID, type, size, and
414
       a pointer to the actual value, are packed into these 12 byte entries. */
415
764
    if ( tags * 12U > i_buf - i )
416
745
        return false;
417
418
    /* Check through IFD0 for tags of interest */
419
3.18k
    while ( tags-- )
420
3.18k
    {
421
3.18k
        tag_type = de_get16( &p_buf[i], endian );
422
        /* Is this the orientation tag? */
423
3.18k
        if ( tag_type == EXIF_ORIENT_TAGID )
424
15
        {
425
15
            type = de_get16( &p_buf[i + 2], endian );
426
15
            count = de_get32( &p_buf[i + 4], endian );
427
428
            /* Check that type and count fields are OK. The orientation field
429
               will consist of a single (count=1) 2-byte integer (type=3). */
430
15
            if ( type != 3 || count != 1 )
431
6
                return false;
432
433
            /* Return the orientation value. Within the 12-byte block, the
434
               pointer to the actual data is at offset 8. */
435
9
            ret = de_get16( &p_buf[i + 8], endian );
436
9
            *orientation = ( ret <= 8 ) ? ret : 0;
437
9
            return true;
438
15
        }
439
        /* move the pointer to the next 12-byte tag field. */
440
3.17k
        i = i + 12;
441
3.17k
    }
442
443
4
    return false; /* No EXIF Orientation tag found */
444
19
}
445
446
static int jpeg_GetOrientation( j_decompress_ptr cinfo )
447
170
{
448
170
    int orientation = 0;
449
170
    for (jpeg_saved_marker_ptr cmarker = cinfo->marker_list;
450
6.20k
                               cmarker != NULL;
451
6.03k
                               cmarker = cmarker->next)
452
6.04k
    {
453
6.04k
        if(cmarker->marker == EXIF_JPEG_MARKER &&
454
1.08k
           cmarker->data_length >= 32 &&
455
920
           !memcmp(cmarker->data, EXIF_IDENT_STRING, sizeof(EXIF_IDENT_STRING)) &&
456
779
           jpeg_ParseExifApp1(cmarker->data, cmarker->data_length, &orientation))
457
9
           break; /* found orientation marker */
458
6.04k
    }
459
170
    return orientation;
460
170
}
461
462
/*
463
 * This function must be fed with a complete compressed frame.
464
 */
465
static int DecodeBlock(decoder_t *p_dec, block_t *p_block)
466
10.3k
{
467
10.3k
    decoder_sys_t *p_sys = p_dec->p_sys;
468
10.3k
    picture_t *p_pic = 0;
469
470
10.3k
    p_sys->p_row_pointers = NULL;
471
472
10.3k
    if (!p_block) /* No Drain */
473
8.62k
        return VLCDEC_SUCCESS;
474
475
1.74k
    if (p_block->i_flags & BLOCK_FLAG_CORRUPTED )
476
0
    {
477
0
        block_Release(p_block);
478
0
        return VLCDEC_SUCCESS;
479
0
    }
480
481
    /* libjpeg longjmp's there in case of error */
482
1.74k
    if (setjmp(p_sys->common.setjmp_buffer))
483
1.65k
    {
484
1.65k
        goto error;
485
1.65k
    }
486
487
90
#define ICC_JPEG_MARKER (JPEG_APP0+2)
488
489
90
    jpeg_create_decompress(&p_sys->p_jpeg);
490
90
    jpeg_mem_src(&p_sys->p_jpeg, p_block->p_buffer, p_block->i_buffer);
491
90
    jpeg_save_markers( &p_sys->p_jpeg, EXIF_JPEG_MARKER, 0xffff );
492
90
    jpeg_save_markers( &p_sys->p_jpeg, ICC_JPEG_MARKER, 0xffff );
493
90
    jpeg_read_header(&p_sys->p_jpeg, TRUE);
494
495
90
    p_sys->p_jpeg.out_color_space = JCS_RGB;
496
497
90
    jpeg_start_decompress(&p_sys->p_jpeg);
498
499
    /* Set output properties */
500
90
    p_dec->fmt_out.video.i_visible_width  = p_dec->fmt_out.video.i_width  = p_sys->p_jpeg.output_width;
501
90
    p_dec->fmt_out.video.i_visible_height = p_dec->fmt_out.video.i_height = p_sys->p_jpeg.output_height;
502
90
    p_dec->fmt_out.video.i_sar_num = 1;
503
90
    p_dec->fmt_out.video.i_sar_den = 1;
504
505
90
    int i_otag; /* Orientation tag has valid range of 1-8. 1 is normal orientation, 0 = unspecified = normal */
506
90
    i_otag = jpeg_GetOrientation( &p_sys->p_jpeg );
507
90
    if ( i_otag > 1 )
508
0
    {
509
0
        msg_Dbg( p_dec, "Jpeg orientation is %d", i_otag );
510
0
        p_dec->fmt_out.video.orientation = ORIENT_FROM_EXIF( i_otag );
511
0
    }
512
90
    jpeg_FillProjection(&p_sys->p_jpeg, &p_dec->fmt_out.video);
513
514
    /* Get a new picture */
515
90
    if (decoder_UpdateVideoFormat(p_dec))
516
0
    {
517
0
        goto error;
518
0
    }
519
90
    p_pic = decoder_NewPicture(p_dec);
520
90
    if (!p_pic)
521
0
    {
522
0
        goto error;
523
0
    }
524
525
90
    p_pic->b_progressive = true;
526
527
    /* Read ICC profile */
528
90
#if defined(LIBJPEG_TURBO_VERSION_NUMBER) && LIBJPEG_TURBO_VERSION_NUMBER >= 1005090
529
90
    JOCTET *p_icc;
530
90
    unsigned int icc_len;
531
90
    if (jpeg_read_icc_profile(&p_sys->p_jpeg, &p_icc, &icc_len))
532
32
    {
533
32
        vlc_icc_profile_t *icc;
534
32
        icc = picture_AttachNewAncillary(p_pic, VLC_ANCILLARY_ID_ICC, sizeof(*icc) + icc_len);
535
32
        if (!icc) {
536
0
            free(p_icc);
537
0
            goto error;
538
0
        }
539
32
        memcpy(icc->data, p_icc, icc_len);
540
32
        icc->size = icc_len;
541
32
        free(p_icc);
542
32
    }
543
90
#endif /* LIBJPEG_TURBO_VERSION_NUMBER */
544
545
    /* Decode picture */
546
90
    p_sys->p_row_pointers = vlc_alloc(p_sys->p_jpeg.output_height, sizeof(JSAMPROW));
547
90
    if (!p_sys->p_row_pointers)
548
0
    {
549
0
        goto error;
550
0
    }
551
169k
    for (unsigned i = 0; i < p_sys->p_jpeg.output_height; i++) {
552
169k
        p_sys->p_row_pointers[i] = p_pic->p->p_pixels + p_pic->p->i_pitch * i;
553
169k
    }
554
555
69.1k
    while (p_sys->p_jpeg.output_scanline < p_sys->p_jpeg.output_height)
556
69.0k
    {
557
69.0k
        jpeg_read_scanlines(&p_sys->p_jpeg,
558
69.0k
                p_sys->p_row_pointers + p_sys->p_jpeg.output_scanline,
559
69.0k
                p_sys->p_jpeg.output_height - p_sys->p_jpeg.output_scanline);
560
69.0k
    }
561
562
90
    jpeg_finish_decompress(&p_sys->p_jpeg);
563
90
    jpeg_destroy_decompress(&p_sys->p_jpeg);
564
90
    free(p_sys->p_row_pointers);
565
566
90
    p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
567
568
90
    block_Release(p_block);
569
90
    decoder_QueueVideo( p_dec, p_pic );
570
90
    return VLCDEC_SUCCESS;
571
572
1.65k
error:
573
574
1.65k
    if (p_pic)
575
0
        picture_Release(p_pic);
576
1.65k
    jpeg_destroy_decompress(&p_sys->p_jpeg);
577
1.65k
    free(p_sys->p_row_pointers);
578
579
1.65k
    block_Release(p_block);
580
1.65k
    return VLCDEC_SUCCESS;
581
90
}
582
583
/*
584
 * jpeg decoder destruction
585
 */
586
static void CloseDecoder(vlc_object_t *p_this)
587
6.44k
{
588
6.44k
    decoder_t *p_dec = (decoder_t *)p_this;
589
6.44k
    decoder_sys_t *p_sys = p_dec->p_sys;
590
591
6.44k
    free(p_sys);
592
6.44k
}
593
594
/*
595
 * Probe the encoder and return score
596
 */
597
static int OpenEncoder(vlc_object_t *p_this)
598
0
{
599
0
    encoder_t *p_enc = (encoder_t *)p_this;
600
601
0
    config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg);
602
603
0
    if (p_enc->fmt_out.i_codec != VLC_CODEC_JPEG)
604
0
    {
605
0
        return VLC_EGENERIC;
606
0
    }
607
608
    /* Allocate the memory needed to store encoder's structure */
609
0
    encoder_sys_t *p_sys = malloc(sizeof(encoder_sys_t));
610
0
    if (p_sys == NULL)
611
0
    {
612
0
        return VLC_ENOMEM;
613
0
    }
614
615
0
    p_enc->p_sys = p_sys;
616
617
0
    p_sys->p_jpeg.client_data = &p_sys->common;
618
0
    p_sys->common.p_obj = p_this;
619
620
0
    p_sys->p_jpeg.err = jpeg_std_error(&p_sys->common.err);
621
0
    p_sys->common.err.error_exit = user_error_exit;
622
0
    p_sys->common.err.output_message = user_error_message;
623
624
0
    p_sys->i_quality = var_GetInteger(p_enc, ENC_CFG_PREFIX "quality");
625
626
0
    if( ckd_mul( &p_sys->i_blocksize, 3, p_enc->fmt_in.video.i_visible_width ) ||
627
0
        ckd_mul( &p_sys->i_blocksize, p_sys->i_blocksize, p_enc->fmt_in.video.i_visible_height ) )
628
0
    {
629
0
        free( p_sys );
630
0
        return VLC_EGENERIC;
631
0
    }
632
633
0
    p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma = VLC_CODEC_I420;
634
0
    p_enc->fmt_in.video.color_range = COLOR_RANGE_FULL;
635
636
0
    static const struct vlc_encoder_operations ops =
637
0
    {
638
0
        .close = CloseEncoder,
639
0
        .encode_video = EncodeBlock,
640
0
    };
641
0
    p_enc->ops = &ops;
642
643
0
    return VLC_SUCCESS;
644
0
}
645
646
/*
647
 * EncodeBlock
648
 */
649
static block_t *EncodeBlock(encoder_t *p_enc, picture_t *p_pic)
650
0
{
651
0
    encoder_sys_t *p_sys = p_enc->p_sys;
652
653
0
    if (unlikely(!p_pic))
654
0
    {
655
0
        return NULL;
656
0
    }
657
0
    block_t *p_block = block_Alloc(p_sys->i_blocksize);
658
0
    if (p_block == NULL)
659
0
    {
660
0
        return NULL;
661
0
    }
662
663
0
    JSAMPIMAGE p_row_pointers = NULL;
664
0
    unsigned long size = p_block->i_buffer;
665
666
    /* libjpeg longjmp's there in case of error */
667
0
    if (setjmp(p_sys->common.setjmp_buffer))
668
0
    {
669
0
        goto error;
670
0
    }
671
672
0
    jpeg_create_compress(&p_sys->p_jpeg);
673
0
    jpeg_mem_dest(&p_sys->p_jpeg, &p_block->p_buffer, &size);
674
675
0
    p_sys->p_jpeg.image_width = p_enc->fmt_in.video.i_visible_width;
676
0
    p_sys->p_jpeg.image_height = p_enc->fmt_in.video.i_visible_height;
677
0
    p_sys->p_jpeg.input_components = 3;
678
0
    p_sys->p_jpeg.in_color_space = JCS_YCbCr;
679
680
0
    jpeg_set_defaults(&p_sys->p_jpeg);
681
0
    jpeg_set_colorspace(&p_sys->p_jpeg, JCS_YCbCr);
682
683
0
    p_sys->p_jpeg.raw_data_in = TRUE;
684
#if JPEG_LIB_VERSION >= 70
685
    p_sys->p_jpeg.do_fancy_downsampling = FALSE;
686
#endif
687
688
0
    jpeg_set_quality(&p_sys->p_jpeg, p_sys->i_quality, TRUE);
689
690
0
    jpeg_start_compress(&p_sys->p_jpeg, TRUE);
691
692
0
    unsigned char exif[] = "Exif\x00\x00"
693
0
                           "\x4d\x4d\x00\x2a" /* TIFF BE header */
694
0
                           "\x00\x00\x00\x08" /* IFD0 offset */
695
0
                           "\x00\x01" /* IFD0 tags count */
696
0
                           "\x01\x12" /* tagtype */
697
0
                           "\x00\x03" /* value type (x03 == short) */
698
0
                           "\x00\x00\x00\x01" /* value count */
699
0
                           "\xFF\xFF\x00\x00" /* value if <= 4 bytes or value offset */
700
0
                           "\x00\x00\x00\x00" /* 0 last IFD */
701
0
                           "\x00\x00\x00\x00" /* 0 last IFD offset */
702
0
            ;
703
0
    exif[24] = 0x00;
704
0
    exif[25] = ORIENT_TO_EXIF(p_pic->format.orientation);
705
0
    jpeg_write_marker(&p_sys->p_jpeg, JPEG_APP0 + 1, exif, sizeof(exif) - 1);
706
707
    /* Encode picture */
708
0
    p_row_pointers = vlc_alloc(p_pic->i_planes, sizeof(JSAMPARRAY));
709
0
    if (p_row_pointers == NULL)
710
0
    {
711
0
        goto error;
712
0
    }
713
714
0
    for (int i = 0; i < p_pic->i_planes; i++)
715
0
    {
716
0
        p_row_pointers[i] = vlc_alloc(p_sys->p_jpeg.comp_info[i].v_samp_factor, sizeof(JSAMPROW) * DCTSIZE);
717
0
    }
718
719
0
    while (p_sys->p_jpeg.next_scanline < p_sys->p_jpeg.image_height)
720
0
    {
721
0
        for (int i = 0; i < p_pic->i_planes; i++)
722
0
        {
723
0
            int i_offset = p_sys->p_jpeg.next_scanline * p_sys->p_jpeg.comp_info[i].v_samp_factor / p_sys->p_jpeg.max_v_samp_factor;
724
725
0
            for (int j = 0; j < p_sys->p_jpeg.comp_info[i].v_samp_factor * DCTSIZE; j++)
726
0
            {
727
0
                p_row_pointers[i][j] = p_pic->p[i].p_pixels + p_pic->p[i].i_pitch * (i_offset + j);
728
0
            }
729
0
        }
730
0
        jpeg_write_raw_data(&p_sys->p_jpeg, p_row_pointers, p_sys->p_jpeg.max_v_samp_factor * DCTSIZE);
731
0
    }
732
733
0
    jpeg_finish_compress(&p_sys->p_jpeg);
734
0
    jpeg_destroy_compress(&p_sys->p_jpeg);
735
736
0
    for (int i = 0; i < p_pic->i_planes; i++)
737
0
    {
738
0
        free(p_row_pointers[i]);
739
0
    }
740
0
    free(p_row_pointers);
741
742
0
    p_block->i_buffer = size;
743
0
    p_block->i_dts = p_block->i_pts = p_pic->date;
744
745
0
    return p_block;
746
747
0
error:
748
0
    jpeg_destroy_compress(&p_sys->p_jpeg);
749
750
0
    if (p_row_pointers != NULL)
751
0
    {
752
0
        for (int i = 0; i < p_pic->i_planes; i++)
753
0
        {
754
0
            free(p_row_pointers[i]);
755
0
        }
756
0
    }
757
0
    free(p_row_pointers);
758
759
0
    block_Release(p_block);
760
761
0
    return NULL;
762
0
}
763
764
/*
765
 * jpeg encoder destruction
766
 */
767
static void CloseEncoder(encoder_t *p_enc)
768
0
{
769
0
    encoder_sys_t *p_sys = p_enc->p_sys;
770
771
0
    free(p_sys);
772
0
}