• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/rpl.c

Go to the documentation of this file.
00001 /*
00002  * ARMovie/RPL demuxer
00003  * Copyright (c) 2007 Christian Ohm, 2008 Eli Friedman
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/dict.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include <stdlib.h>
00027 
00028 #define RPL_SIGNATURE "ARMovie\x0A"
00029 #define RPL_SIGNATURE_SIZE 8
00030 
00032 #define RPL_LINE_LENGTH 256
00033 
00034 static int rpl_probe(AVProbeData *p)
00035 {
00036     if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
00037         return 0;
00038 
00039     return AVPROBE_SCORE_MAX;
00040 }
00041 
00042 typedef struct RPLContext {
00043     // RPL header data
00044     int32_t frames_per_chunk;
00045 
00046     // Stream position data
00047     uint32_t chunk_number;
00048     uint32_t chunk_part;
00049     uint32_t frame_in_part;
00050 } RPLContext;
00051 
00052 static int read_line(AVIOContext * pb, char* line, int bufsize)
00053 {
00054     int i;
00055     for (i = 0; i < bufsize - 1; i++) {
00056         int b = avio_r8(pb);
00057         if (b == 0)
00058             break;
00059         if (b == '\n') {
00060             line[i] = '\0';
00061             return 0;
00062         }
00063         line[i] = b;
00064     }
00065     line[i] = '\0';
00066     return -1;
00067 }
00068 
00069 static int32_t read_int(const char* line, const char** endptr, int* error)
00070 {
00071     unsigned long result = 0;
00072     for (; *line>='0' && *line<='9'; line++) {
00073         if (result > (0x7FFFFFFF - 9) / 10)
00074             *error = -1;
00075         result = 10 * result + *line - '0';
00076     }
00077     *endptr = line;
00078     return result;
00079 }
00080 
00081 static int32_t read_line_and_int(AVIOContext * pb, int* error)
00082 {
00083     char line[RPL_LINE_LENGTH];
00084     const char *endptr;
00085     *error |= read_line(pb, line, sizeof(line));
00086     return read_int(line, &endptr, error);
00087 }
00088 
00093 static AVRational read_fps(const char* line, int* error)
00094 {
00095     int64_t num, den = 1;
00096     AVRational result;
00097     num = read_int(line, &line, error);
00098     if (*line == '.')
00099         line++;
00100     for (; *line>='0' && *line<='9'; line++) {
00101         // Truncate any numerator too large to fit into an int64_t
00102         if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
00103             break;
00104         num  = 10 * num + *line - '0';
00105         den *= 10;
00106     }
00107     if (!num)
00108         *error = -1;
00109     av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
00110     return result;
00111 }
00112 
00113 static int rpl_read_header(AVFormatContext *s, AVFormatParameters *ap)
00114 {
00115     AVIOContext *pb = s->pb;
00116     RPLContext *rpl = s->priv_data;
00117     AVStream *vst = NULL, *ast = NULL;
00118     int total_audio_size;
00119     int error = 0;
00120 
00121     uint32_t i;
00122 
00123     int32_t audio_format, chunk_catalog_offset, number_of_chunks;
00124     AVRational fps;
00125 
00126     char line[RPL_LINE_LENGTH];
00127 
00128     // The header for RPL/ARMovie files is 21 lines of text
00129     // containing the various header fields.  The fields are always
00130     // in the same order, and other text besides the first
00131     // number usually isn't important.
00132     // (The spec says that there exists some significance
00133     // for the text in a few cases; samples needed.)
00134     error |= read_line(pb, line, sizeof(line));      // ARMovie
00135     error |= read_line(pb, line, sizeof(line));      // movie name
00136     av_dict_set(&s->metadata, "title"    , line, 0);
00137     error |= read_line(pb, line, sizeof(line));      // date/copyright
00138     av_dict_set(&s->metadata, "copyright", line, 0);
00139     error |= read_line(pb, line, sizeof(line));      // author and other
00140     av_dict_set(&s->metadata, "author"   , line, 0);
00141 
00142     // video headers
00143     vst = avformat_new_stream(s, NULL);
00144     if (!vst)
00145         return AVERROR(ENOMEM);
00146     vst->codec->codec_type      = AVMEDIA_TYPE_VIDEO;
00147     vst->codec->codec_tag       = read_line_and_int(pb, &error);  // video format
00148     vst->codec->width           = read_line_and_int(pb, &error);  // video width
00149     vst->codec->height          = read_line_and_int(pb, &error);  // video height
00150     vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error);  // video bits per sample
00151     error |= read_line(pb, line, sizeof(line));                   // video frames per second
00152     fps = read_fps(line, &error);
00153     avpriv_set_pts_info(vst, 32, fps.den, fps.num);
00154 
00155     // Figure out the video codec
00156     switch (vst->codec->codec_tag) {
00157 #if 0
00158         case 122:
00159             vst->codec->codec_id = CODEC_ID_ESCAPE122;
00160             break;
00161 #endif
00162         case 124:
00163             vst->codec->codec_id = CODEC_ID_ESCAPE124;
00164             // The header is wrong here, at least sometimes
00165             vst->codec->bits_per_coded_sample = 16;
00166             break;
00167 #if 0
00168         case 130:
00169             vst->codec->codec_id = CODEC_ID_ESCAPE130;
00170             break;
00171 #endif
00172         default:
00173             av_log(s, AV_LOG_WARNING,
00174                    "RPL video format %i not supported yet!\n",
00175                    vst->codec->codec_tag);
00176             vst->codec->codec_id = CODEC_ID_NONE;
00177     }
00178 
00179     // Audio headers
00180 
00181     // ARMovie supports multiple audio tracks; I don't have any
00182     // samples, though. This code will ignore additional tracks.
00183     audio_format = read_line_and_int(pb, &error);  // audio format ID
00184     if (audio_format) {
00185         ast = avformat_new_stream(s, NULL);
00186         if (!ast)
00187             return AVERROR(ENOMEM);
00188         ast->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
00189         ast->codec->codec_tag       = audio_format;
00190         ast->codec->sample_rate     = read_line_and_int(pb, &error);  // audio bitrate
00191         ast->codec->channels        = read_line_and_int(pb, &error);  // number of audio channels
00192         ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error);  // audio bits per sample
00193         // At least one sample uses 0 for ADPCM, which is really 4 bits
00194         // per sample.
00195         if (ast->codec->bits_per_coded_sample == 0)
00196             ast->codec->bits_per_coded_sample = 4;
00197 
00198         ast->codec->bit_rate = ast->codec->sample_rate *
00199                                ast->codec->bits_per_coded_sample *
00200                                ast->codec->channels;
00201 
00202         ast->codec->codec_id = CODEC_ID_NONE;
00203         switch (audio_format) {
00204             case 1:
00205                 if (ast->codec->bits_per_coded_sample == 16) {
00206                     // 16-bit audio is always signed
00207                     ast->codec->codec_id = CODEC_ID_PCM_S16LE;
00208                     break;
00209                 }
00210                 // There are some other formats listed as legal per the spec;
00211                 // samples needed.
00212                 break;
00213             case 101:
00214                 if (ast->codec->bits_per_coded_sample == 8) {
00215                     // The samples with this kind of audio that I have
00216                     // are all unsigned.
00217                     ast->codec->codec_id = CODEC_ID_PCM_U8;
00218                     break;
00219                 } else if (ast->codec->bits_per_coded_sample == 4) {
00220                     ast->codec->codec_id = CODEC_ID_ADPCM_IMA_EA_SEAD;
00221                     break;
00222                 }
00223                 break;
00224         }
00225         if (ast->codec->codec_id == CODEC_ID_NONE) {
00226             av_log(s, AV_LOG_WARNING,
00227                    "RPL audio format %i not supported yet!\n",
00228                    audio_format);
00229         }
00230         avpriv_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
00231     } else {
00232         for (i = 0; i < 3; i++)
00233             error |= read_line(pb, line, sizeof(line));
00234     }
00235 
00236     rpl->frames_per_chunk = read_line_and_int(pb, &error);  // video frames per chunk
00237     if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
00238         av_log(s, AV_LOG_WARNING,
00239                "Don't know how to split frames for video format %i. "
00240                "Video stream will be broken!\n", vst->codec->codec_tag);
00241 
00242     number_of_chunks = read_line_and_int(pb, &error);  // number of chunks in the file
00243     // The number in the header is actually the index of the last chunk.
00244     number_of_chunks++;
00245 
00246     error |= read_line(pb, line, sizeof(line));  // "even" chunk size in bytes
00247     error |= read_line(pb, line, sizeof(line));  // "odd" chunk size in bytes
00248     chunk_catalog_offset =                       // offset of the "chunk catalog"
00249         read_line_and_int(pb, &error);           //   (file index)
00250     error |= read_line(pb, line, sizeof(line));  // offset to "helpful" sprite
00251     error |= read_line(pb, line, sizeof(line));  // size of "helpful" sprite
00252     error |= read_line(pb, line, sizeof(line));  // offset to key frame list
00253 
00254     // Read the index
00255     avio_seek(pb, chunk_catalog_offset, SEEK_SET);
00256     total_audio_size = 0;
00257     for (i = 0; i < number_of_chunks; i++) {
00258         int64_t offset, video_size, audio_size;
00259         error |= read_line(pb, line, sizeof(line));
00260         if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
00261                         &offset, &video_size, &audio_size))
00262             error = -1;
00263         av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
00264                            video_size, rpl->frames_per_chunk, 0);
00265         if (ast)
00266             av_add_index_entry(ast, offset + video_size, total_audio_size,
00267                                audio_size, audio_size * 8, 0);
00268         total_audio_size += audio_size * 8;
00269     }
00270 
00271     if (error) return AVERROR(EIO);
00272 
00273     return 0;
00274 }
00275 
00276 static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
00277 {
00278     RPLContext *rpl = s->priv_data;
00279     AVIOContext *pb = s->pb;
00280     AVStream* stream;
00281     AVIndexEntry* index_entry;
00282     uint32_t ret;
00283 
00284     if (rpl->chunk_part == s->nb_streams) {
00285         rpl->chunk_number++;
00286         rpl->chunk_part = 0;
00287     }
00288 
00289     stream = s->streams[rpl->chunk_part];
00290 
00291     if (rpl->chunk_number >= stream->nb_index_entries)
00292         return -1;
00293 
00294     index_entry = &stream->index_entries[rpl->chunk_number];
00295 
00296     if (rpl->frame_in_part == 0)
00297         if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
00298             return AVERROR(EIO);
00299 
00300     if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00301         stream->codec->codec_tag == 124) {
00302         // We have to split Escape 124 frames because there are
00303         // multiple frames per chunk in Escape 124 samples.
00304         uint32_t frame_size;
00305 
00306         avio_skip(pb, 4); /* flags */
00307         frame_size = avio_rl32(pb);
00308         if (avio_seek(pb, -8, SEEK_CUR) < 0)
00309             return AVERROR(EIO);
00310 
00311         ret = av_get_packet(pb, pkt, frame_size);
00312         if (ret != frame_size) {
00313             av_free_packet(pkt);
00314             return AVERROR(EIO);
00315         }
00316         pkt->duration = 1;
00317         pkt->pts = index_entry->timestamp + rpl->frame_in_part;
00318         pkt->stream_index = rpl->chunk_part;
00319 
00320         rpl->frame_in_part++;
00321         if (rpl->frame_in_part == rpl->frames_per_chunk) {
00322             rpl->frame_in_part = 0;
00323             rpl->chunk_part++;
00324         }
00325     } else {
00326         ret = av_get_packet(pb, pkt, index_entry->size);
00327         if (ret != index_entry->size) {
00328             av_free_packet(pkt);
00329             return AVERROR(EIO);
00330         }
00331 
00332         if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00333             // frames_per_chunk should always be one here; the header
00334             // parsing will warn if it isn't.
00335             pkt->duration = rpl->frames_per_chunk;
00336         } else {
00337             // All the audio codecs supported in this container
00338             // (at least so far) are constant-bitrate.
00339             pkt->duration = ret * 8;
00340         }
00341         pkt->pts = index_entry->timestamp;
00342         pkt->stream_index = rpl->chunk_part;
00343         rpl->chunk_part++;
00344     }
00345 
00346     // None of the Escape formats have keyframes, and the ADPCM
00347     // format used doesn't have keyframes.
00348     if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
00349         pkt->flags |= AV_PKT_FLAG_KEY;
00350 
00351     return ret;
00352 }
00353 
00354 AVInputFormat ff_rpl_demuxer = {
00355     .name           = "rpl",
00356     .long_name      = NULL_IF_CONFIG_SMALL("RPL/ARMovie format"),
00357     .priv_data_size = sizeof(RPLContext),
00358     .read_probe     = rpl_probe,
00359     .read_header    = rpl_read_header,
00360     .read_packet    = rpl_read_packet,
00361 };
Generated on Thu Jan 24 2013 17:08:56 for Libav by doxygen 1.7.1