LCOV - code coverage report
Current view: top level - libavformat - rpl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 149 181 82.3 %
Date: 2017-12-17 16:07:53 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  * ARMovie/RPL demuxer
       3             :  * Copyright (c) 2007 Christian Ohm, 2008 Eli Friedman
       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             : #include <inttypes.h>
      23             : #include <stdlib.h>
      24             : 
      25             : #include "libavutil/avstring.h"
      26             : #include "libavutil/dict.h"
      27             : #include "avformat.h"
      28             : #include "internal.h"
      29             : 
      30             : #define RPL_SIGNATURE "ARMovie\x0A"
      31             : #define RPL_SIGNATURE_SIZE 8
      32             : 
      33             : /** 256 is arbitrary, but should be big enough for any reasonable file. */
      34             : #define RPL_LINE_LENGTH 256
      35             : 
      36        6130 : static int rpl_probe(AVProbeData *p)
      37             : {
      38        6130 :     if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
      39        6128 :         return 0;
      40             : 
      41           2 :     return AVPROBE_SCORE_MAX;
      42             : }
      43             : 
      44             : typedef struct RPLContext {
      45             :     // RPL header data
      46             :     int32_t frames_per_chunk;
      47             : 
      48             :     // Stream position data
      49             :     uint32_t chunk_number;
      50             :     uint32_t chunk_part;
      51             :     uint32_t frame_in_part;
      52             : } RPLContext;
      53             : 
      54         405 : static int read_line(AVIOContext * pb, char* line, int bufsize)
      55             : {
      56             :     int i;
      57        7645 :     for (i = 0; i < bufsize - 1; i++) {
      58        7645 :         int b = avio_r8(pb);
      59        7645 :         if (b == 0)
      60           0 :             break;
      61        7645 :         if (b == '\n') {
      62         405 :             line[i] = '\0';
      63         405 :             return avio_feof(pb) ? -1 : 0;
      64             :         }
      65        7240 :         line[i] = b;
      66             :     }
      67           0 :     line[i] = '\0';
      68           0 :     return -1;
      69             : }
      70             : 
      71          24 : static int32_t read_int(const char* line, const char** endptr, int* error)
      72             : {
      73          24 :     unsigned long result = 0;
      74          90 :     for (; *line>='0' && *line<='9'; line++) {
      75          66 :         if (result > (0x7FFFFFFF - 9) / 10)
      76           0 :             *error = -1;
      77          66 :         result = 10 * result + *line - '0';
      78             :     }
      79          24 :     *endptr = line;
      80          24 :     return result;
      81             : }
      82             : 
      83          22 : static int32_t read_line_and_int(AVIOContext * pb, int* error)
      84             : {
      85             :     char line[RPL_LINE_LENGTH];
      86             :     const char *endptr;
      87          22 :     *error |= read_line(pb, line, sizeof(line));
      88          22 :     return read_int(line, &endptr, error);
      89             : }
      90             : 
      91             : /** Parsing for fps, which can be a fraction. Unfortunately,
      92             :   * the spec for the header leaves out a lot of details,
      93             :   * so this is mostly guessing.
      94             :   */
      95           2 : static AVRational read_fps(const char* line, int* error)
      96             : {
      97           2 :     int64_t num, den = 1;
      98             :     AVRational result;
      99           2 :     num = read_int(line, &line, error);
     100           2 :     if (*line == '.')
     101           2 :         line++;
     102          14 :     for (; *line>='0' && *line<='9'; line++) {
     103             :         // Truncate any numerator too large to fit into an int64_t
     104          12 :         if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
     105             :             break;
     106          12 :         num  = 10 * num + *line - '0';
     107          12 :         den *= 10;
     108             :     }
     109           2 :     if (!num)
     110           0 :         *error = -1;
     111           2 :     av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
     112           2 :     return result;
     113             : }
     114             : 
     115           2 : static int rpl_read_header(AVFormatContext *s)
     116             : {
     117           2 :     AVIOContext *pb = s->pb;
     118           2 :     RPLContext *rpl = s->priv_data;
     119           2 :     AVStream *vst = NULL, *ast = NULL;
     120             :     int total_audio_size;
     121           2 :     int error = 0;
     122             : 
     123             :     uint32_t i;
     124             : 
     125             :     int32_t audio_format, chunk_catalog_offset, number_of_chunks;
     126             :     AVRational fps;
     127             : 
     128             :     char line[RPL_LINE_LENGTH];
     129             : 
     130             :     // The header for RPL/ARMovie files is 21 lines of text
     131             :     // containing the various header fields.  The fields are always
     132             :     // in the same order, and other text besides the first
     133             :     // number usually isn't important.
     134             :     // (The spec says that there exists some significance
     135             :     // for the text in a few cases; samples needed.)
     136           2 :     error |= read_line(pb, line, sizeof(line));      // ARMovie
     137           2 :     error |= read_line(pb, line, sizeof(line));      // movie name
     138           2 :     av_dict_set(&s->metadata, "title"    , line, 0);
     139           2 :     error |= read_line(pb, line, sizeof(line));      // date/copyright
     140           2 :     av_dict_set(&s->metadata, "copyright", line, 0);
     141           2 :     error |= read_line(pb, line, sizeof(line));      // author and other
     142           2 :     av_dict_set(&s->metadata, "author"   , line, 0);
     143             : 
     144             :     // video headers
     145           2 :     vst = avformat_new_stream(s, NULL);
     146           2 :     if (!vst)
     147           0 :         return AVERROR(ENOMEM);
     148           2 :     vst->codecpar->codec_type      = AVMEDIA_TYPE_VIDEO;
     149           2 :     vst->codecpar->codec_tag       = read_line_and_int(pb, &error);  // video format
     150           2 :     vst->codecpar->width           = read_line_and_int(pb, &error);  // video width
     151           2 :     vst->codecpar->height          = read_line_and_int(pb, &error);  // video height
     152           2 :     vst->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error);  // video bits per sample
     153           2 :     error |= read_line(pb, line, sizeof(line));                   // video frames per second
     154           2 :     fps = read_fps(line, &error);
     155           2 :     avpriv_set_pts_info(vst, 32, fps.den, fps.num);
     156             : 
     157             :     // Figure out the video codec
     158           2 :     switch (vst->codecpar->codec_tag) {
     159             : #if 0
     160             :         case 122:
     161             :             vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE122;
     162             :             break;
     163             : #endif
     164           1 :         case 124:
     165           1 :             vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE124;
     166             :             // The header is wrong here, at least sometimes
     167           1 :             vst->codecpar->bits_per_coded_sample = 16;
     168           1 :             break;
     169           1 :         case 130:
     170           1 :             vst->codecpar->codec_id = AV_CODEC_ID_ESCAPE130;
     171           1 :             break;
     172           0 :         default:
     173           0 :             avpriv_report_missing_feature(s, "Video format %s",
     174           0 :                                           av_fourcc2str(vst->codecpar->codec_tag));
     175           0 :             vst->codecpar->codec_id = AV_CODEC_ID_NONE;
     176             :     }
     177             : 
     178             :     // Audio headers
     179             : 
     180             :     // ARMovie supports multiple audio tracks; I don't have any
     181             :     // samples, though. This code will ignore additional tracks.
     182           2 :     audio_format = read_line_and_int(pb, &error);  // audio format ID
     183           2 :     if (audio_format) {
     184           2 :         ast = avformat_new_stream(s, NULL);
     185           2 :         if (!ast)
     186           0 :             return AVERROR(ENOMEM);
     187           2 :         ast->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
     188           2 :         ast->codecpar->codec_tag       = audio_format;
     189           2 :         ast->codecpar->sample_rate     = read_line_and_int(pb, &error);  // audio bitrate
     190           2 :         ast->codecpar->channels        = read_line_and_int(pb, &error);  // number of audio channels
     191           2 :         ast->codecpar->bits_per_coded_sample = read_line_and_int(pb, &error);  // audio bits per sample
     192             :         // At least one sample uses 0 for ADPCM, which is really 4 bits
     193             :         // per sample.
     194           2 :         if (ast->codecpar->bits_per_coded_sample == 0)
     195           0 :             ast->codecpar->bits_per_coded_sample = 4;
     196             : 
     197           8 :         ast->codecpar->bit_rate = ast->codecpar->sample_rate *
     198           6 :                                   ast->codecpar->bits_per_coded_sample *
     199           2 :                                   ast->codecpar->channels;
     200             : 
     201           2 :         ast->codecpar->codec_id = AV_CODEC_ID_NONE;
     202           2 :         switch (audio_format) {
     203           1 :             case 1:
     204           1 :                 if (ast->codecpar->bits_per_coded_sample == 16) {
     205             :                     // 16-bit audio is always signed
     206           1 :                     ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
     207           1 :                     break;
     208             :                 }
     209             :                 // There are some other formats listed as legal per the spec;
     210             :                 // samples needed.
     211           0 :                 break;
     212           1 :             case 101:
     213           1 :                 if (ast->codecpar->bits_per_coded_sample == 8) {
     214             :                     // The samples with this kind of audio that I have
     215             :                     // are all unsigned.
     216           1 :                     ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
     217           1 :                     break;
     218           0 :                 } else if (ast->codecpar->bits_per_coded_sample == 4) {
     219           0 :                     ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
     220           0 :                     break;
     221             :                 }
     222           0 :                 break;
     223             :         }
     224           2 :         if (ast->codecpar->codec_id == AV_CODEC_ID_NONE)
     225           0 :             avpriv_request_sample(s, "Audio format %"PRId32,
     226             :                                   audio_format);
     227           2 :         avpriv_set_pts_info(ast, 32, 1, ast->codecpar->bit_rate);
     228             :     } else {
     229           0 :         for (i = 0; i < 3; i++)
     230           0 :             error |= read_line(pb, line, sizeof(line));
     231             :     }
     232             : 
     233           2 :     rpl->frames_per_chunk = read_line_and_int(pb, &error);  // video frames per chunk
     234           2 :     if (rpl->frames_per_chunk > 1 && vst->codecpar->codec_tag != 124)
     235           0 :         av_log(s, AV_LOG_WARNING,
     236             :                "Don't know how to split frames for video format %s. "
     237           0 :                "Video stream will be broken!\n", av_fourcc2str(vst->codecpar->codec_tag));
     238             : 
     239           2 :     number_of_chunks = read_line_and_int(pb, &error);  // number of chunks in the file
     240             :     // The number in the header is actually the index of the last chunk.
     241           2 :     number_of_chunks++;
     242             : 
     243           2 :     error |= read_line(pb, line, sizeof(line));  // "even" chunk size in bytes
     244           2 :     error |= read_line(pb, line, sizeof(line));  // "odd" chunk size in bytes
     245           2 :     chunk_catalog_offset =                       // offset of the "chunk catalog"
     246             :         read_line_and_int(pb, &error);           //   (file index)
     247           2 :     error |= read_line(pb, line, sizeof(line));  // offset to "helpful" sprite
     248           2 :     error |= read_line(pb, line, sizeof(line));  // size of "helpful" sprite
     249           2 :     error |= read_line(pb, line, sizeof(line));  // offset to key frame list
     250             : 
     251             :     // Read the index
     252           2 :     avio_seek(pb, chunk_catalog_offset, SEEK_SET);
     253           2 :     total_audio_size = 0;
     254         365 :     for (i = 0; !error && i < number_of_chunks; i++) {
     255             :         int64_t offset, video_size, audio_size;
     256         363 :         error |= read_line(pb, line, sizeof(line));
     257         363 :         if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
     258             :                         &offset, &video_size, &audio_size)) {
     259           0 :             error = -1;
     260           0 :             continue;
     261             :         }
     262         363 :         av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
     263             :                            video_size, rpl->frames_per_chunk, 0);
     264         363 :         if (ast)
     265         363 :             av_add_index_entry(ast, offset + video_size, total_audio_size,
     266             :                                audio_size, audio_size * 8, 0);
     267         363 :         total_audio_size += audio_size * 8;
     268             :     }
     269             : 
     270           2 :     if (error) return AVERROR(EIO);
     271             : 
     272           2 :     return 0;
     273             : }
     274             : 
     275         824 : static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
     276             : {
     277         824 :     RPLContext *rpl = s->priv_data;
     278         824 :     AVIOContext *pb = s->pb;
     279             :     AVStream* stream;
     280             :     AVIndexEntry* index_entry;
     281             :     int ret;
     282             : 
     283         824 :     if (rpl->chunk_part == s->nb_streams) {
     284         363 :         rpl->chunk_number++;
     285         363 :         rpl->chunk_part = 0;
     286             :     }
     287             : 
     288         824 :     stream = s->streams[rpl->chunk_part];
     289             : 
     290         824 :     if (rpl->chunk_number >= stream->nb_index_entries)
     291           2 :         return AVERROR_EOF;
     292             : 
     293         822 :     index_entry = &stream->index_entries[rpl->chunk_number];
     294             : 
     295         822 :     if (rpl->frame_in_part == 0)
     296         726 :         if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
     297           0 :             return AVERROR(EIO);
     298             : 
     299        1281 :     if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
     300         559 :         stream->codecpar->codec_tag == 124) {
     301             :         // We have to split Escape 124 frames because there are
     302             :         // multiple frames per chunk in Escape 124 samples.
     303             :         uint32_t frame_size;
     304             : 
     305         100 :         avio_skip(pb, 4); /* flags */
     306         100 :         frame_size = avio_rl32(pb);
     307         100 :         if (avio_seek(pb, -8, SEEK_CUR) < 0)
     308           0 :             return AVERROR(EIO);
     309             : 
     310         100 :         ret = av_get_packet(pb, pkt, frame_size);
     311         100 :         if (ret < 0)
     312           0 :             return ret;
     313         100 :         if (ret != frame_size) {
     314           0 :             av_packet_unref(pkt);
     315           0 :             return AVERROR(EIO);
     316             :         }
     317         100 :         pkt->duration = 1;
     318         100 :         pkt->pts = index_entry->timestamp + rpl->frame_in_part;
     319         100 :         pkt->stream_index = rpl->chunk_part;
     320             : 
     321         100 :         rpl->frame_in_part++;
     322         100 :         if (rpl->frame_in_part == rpl->frames_per_chunk) {
     323           4 :             rpl->frame_in_part = 0;
     324           4 :             rpl->chunk_part++;
     325             :         }
     326             :     } else {
     327         722 :         ret = av_get_packet(pb, pkt, index_entry->size);
     328         722 :         if (ret < 0)
     329           0 :             return ret;
     330         722 :         if (ret != index_entry->size) {
     331           0 :             av_packet_unref(pkt);
     332           0 :             return AVERROR(EIO);
     333             :         }
     334             : 
     335         722 :         if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
     336             :             // frames_per_chunk should always be one here; the header
     337             :             // parsing will warn if it isn't.
     338         359 :             pkt->duration = rpl->frames_per_chunk;
     339             :         } else {
     340             :             // All the audio codecs supported in this container
     341             :             // (at least so far) are constant-bitrate.
     342         363 :             pkt->duration = ret * 8;
     343             :         }
     344         722 :         pkt->pts = index_entry->timestamp;
     345         722 :         pkt->stream_index = rpl->chunk_part;
     346         722 :         rpl->chunk_part++;
     347             :     }
     348             : 
     349             :     // None of the Escape formats have keyframes, and the ADPCM
     350             :     // format used doesn't have keyframes.
     351         822 :     if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
     352           4 :         pkt->flags |= AV_PKT_FLAG_KEY;
     353             : 
     354         822 :     return ret;
     355             : }
     356             : 
     357             : AVInputFormat ff_rpl_demuxer = {
     358             :     .name           = "rpl",
     359             :     .long_name      = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
     360             :     .priv_data_size = sizeof(RPLContext),
     361             :     .read_probe     = rpl_probe,
     362             :     .read_header    = rpl_read_header,
     363             :     .read_packet    = rpl_read_packet,
     364             : };

Generated by: LCOV version 1.13