LCOV - code coverage report
Current view: top level - libavcodec - mjpeg2jpeg_bsf.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 52 0.0 %
Date: 2017-12-13 18:07:29 Functions: 0 3 0.0 %

          Line data    Source code
       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           0 : static uint8_t *append(uint8_t *buf, const uint8_t *src, int size)
      61             : {
      62           0 :     memcpy(buf, src, size);
      63           0 :     return buf + size;
      64             : }
      65             : 
      66           0 : static uint8_t *append_dht_segment(uint8_t *buf)
      67             : {
      68           0 :     buf = append(buf, dht_segment_head, sizeof(dht_segment_head));
      69           0 :     buf = append(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16);
      70           0 :     buf = append(buf, dht_segment_frag, sizeof(dht_segment_frag));
      71           0 :     buf = append(buf, avpriv_mjpeg_val_dc, 12);
      72           0 :     *(buf++) = 0x10;
      73           0 :     buf = append(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16);
      74           0 :     buf = append(buf, avpriv_mjpeg_val_ac_luminance, 162);
      75           0 :     *(buf++) = 0x11;
      76           0 :     buf = append(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16);
      77           0 :     buf = append(buf, avpriv_mjpeg_val_ac_chrominance, 162);
      78           0 :     return buf;
      79             : }
      80             : 
      81           0 : static int mjpeg2jpeg_filter(AVBSFContext *ctx, AVPacket *out)
      82             : {
      83             :     AVPacket *in;
      84           0 :     int ret = 0;
      85             :     int input_skip, output_size;
      86             :     uint8_t *output;
      87             : 
      88           0 :     ret = ff_bsf_get_packet(ctx, &in);
      89           0 :     if (ret < 0)
      90           0 :         return ret;
      91             : 
      92           0 :     if (in->size < 12) {
      93           0 :         av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
      94           0 :         ret = AVERROR_INVALIDDATA;
      95           0 :         goto fail;
      96             :     }
      97           0 :     if (AV_RB16(in->data) != 0xffd8) {
      98           0 :         av_log(ctx, AV_LOG_ERROR, "input is not MJPEG\n");
      99           0 :         ret = AVERROR_INVALIDDATA;
     100           0 :         goto fail;
     101             :     }
     102           0 :     if (in->data[2] == 0xff && in->data[3] == APP0) {
     103           0 :         input_skip = (in->data[4] << 8) + in->data[5] + 4;
     104             :     } else {
     105           0 :         input_skip = 2;
     106             :     }
     107           0 :     if (in->size < input_skip) {
     108           0 :         av_log(ctx, AV_LOG_ERROR, "input is truncated\n");
     109           0 :         ret = AVERROR_INVALIDDATA;
     110           0 :         goto fail;
     111             :     }
     112           0 :     output_size = in->size - input_skip +
     113           0 :                   sizeof(jpeg_header) + dht_segment_size;
     114           0 :     ret = av_new_packet(out, output_size);
     115           0 :     if (ret < 0)
     116           0 :         goto fail;
     117             : 
     118           0 :     output = out->data;
     119             : 
     120           0 :     output = append(output, jpeg_header, sizeof(jpeg_header));
     121           0 :     output = append_dht_segment(output);
     122           0 :     output = append(output, in->data + input_skip, in->size - input_skip);
     123             : 
     124           0 :     ret = av_packet_copy_props(out, in);
     125           0 :     if (ret < 0)
     126           0 :         goto fail;
     127             : 
     128           0 : fail:
     129           0 :     if (ret < 0)
     130           0 :         av_packet_unref(out);
     131           0 :     av_packet_free(&in);
     132           0 :     return ret;
     133             : }
     134             : 
     135             : static const enum AVCodecID codec_ids[] = {
     136             :     AV_CODEC_ID_MJPEG, AV_CODEC_ID_NONE,
     137             : };
     138             : 
     139             : const AVBitStreamFilter ff_mjpeg2jpeg_bsf = {
     140             :     .name           = "mjpeg2jpeg",
     141             :     .filter         = mjpeg2jpeg_filter,
     142             :     .codec_ids      = codec_ids,
     143             : };

Generated by: LCOV version 1.13