LCOV - code coverage report
Current view: top level - libavformat - rmdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 584 890 65.6 %
Date: 2017-12-16 13:57:32 Functions: 24 27 88.9 %

          Line data    Source code
       1             : /*
       2             :  * "Real" compatible demuxer.
       3             :  * Copyright (c) 2000, 2001 Fabrice Bellard
       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             : 
      24             : #include "libavutil/avassert.h"
      25             : #include "libavutil/avstring.h"
      26             : #include "libavutil/channel_layout.h"
      27             : #include "libavutil/internal.h"
      28             : #include "libavutil/intreadwrite.h"
      29             : #include "libavutil/dict.h"
      30             : #include "avformat.h"
      31             : #include "avio_internal.h"
      32             : #include "internal.h"
      33             : #include "rmsipr.h"
      34             : #include "rm.h"
      35             : 
      36             : #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/ATRAC
      37             : #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
      38             : #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
      39             : #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
      40             : #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
      41             : #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
      42             : 
      43             : struct RMStream {
      44             :     AVPacket pkt;      ///< place to store merged video frame / reordered audio data
      45             :     int videobufsize;  ///< current assembled frame size
      46             :     int videobufpos;   ///< position for the next slice in the video buffer
      47             :     int curpic_num;    ///< picture number of current frame
      48             :     int cur_slice, slices;
      49             :     int64_t pktpos;    ///< first slice position in file
      50             :     /// Audio descrambling matrix parameters
      51             :     int64_t audiotimestamp; ///< Audio packet timestamp
      52             :     int sub_packet_cnt; // Subpacket counter, used while reading
      53             :     int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
      54             :     int audio_framesize; /// Audio frame size from container
      55             :     int sub_packet_lengths[16]; /// Length of each subpacket
      56             :     int32_t deint_id;  ///< deinterleaver used in audio stream
      57             : };
      58             : 
      59             : typedef struct RMDemuxContext {
      60             :     int nb_packets;
      61             :     int old_format;
      62             :     int current_stream;
      63             :     int remaining_len;
      64             :     int audio_stream_num; ///< Stream number for audio packets
      65             :     int audio_pkt_cnt; ///< Output packet counter
      66             :     int data_end;
      67             : } RMDemuxContext;
      68             : 
      69             : static int rm_read_close(AVFormatContext *s);
      70             : 
      71         366 : static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
      72             : {
      73             :     int i;
      74             :     char *q, r;
      75             : 
      76         366 :     q = buf;
      77        6025 :     for(i=0;i<len;i++) {
      78        5659 :         r = avio_r8(pb);
      79        5659 :         if (i < buf_size - 1)
      80        5560 :             *q++ = r;
      81             :     }
      82         366 :     if (buf_size > 0) *q = '\0';
      83         366 : }
      84             : 
      85         189 : static void get_str8(AVIOContext *pb, char *buf, int buf_size)
      86             : {
      87         189 :     get_strl(pb, buf, buf_size, avio_r8(pb));
      88         189 : }
      89             : 
      90          28 : static int rm_read_extradata(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, unsigned size)
      91             : {
      92          28 :     if (size >= 1<<24) {
      93           0 :         av_log(s, AV_LOG_ERROR, "extradata size %u too large\n", size);
      94           0 :         return -1;
      95             :     }
      96          28 :     if (ff_get_extradata(s, par, pb, size) < 0)
      97           0 :         return AVERROR(ENOMEM);
      98          28 :     return 0;
      99             : }
     100             : 
     101          25 : static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
     102             : {
     103             :     char buf[1024];
     104             :     int i;
     105             : 
     106         125 :     for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
     107         100 :         int len = wide ? avio_rb16(pb) : avio_r8(pb);
     108         100 :         get_strl(pb, buf, sizeof(buf), len);
     109         100 :         av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
     110             :     }
     111          25 : }
     112             : 
     113          43 : RMStream *ff_rm_alloc_rmstream (void)
     114             : {
     115          43 :     RMStream *rms = av_mallocz(sizeof(RMStream));
     116          43 :     if (!rms)
     117           0 :         return NULL;
     118          43 :     rms->curpic_num = -1;
     119          43 :     return rms;
     120             : }
     121             : 
     122          33 : void ff_rm_free_rmstream (RMStream *rms)
     123             : {
     124          33 :     av_packet_unref(&rms->pkt);
     125          33 : }
     126             : 
     127          14 : static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
     128             :                                      AVStream *st, RMStream *ast, int read_all)
     129             : {
     130             :     char buf[256];
     131             :     uint32_t version;
     132             :     int ret;
     133             : 
     134             :     /* ra type header */
     135          14 :     version = avio_rb16(pb); /* version */
     136          14 :     if (version == 3) {
     137             :         unsigned bytes_per_minute;
     138           2 :         int header_size = avio_rb16(pb);
     139           2 :         int64_t startpos = avio_tell(pb);
     140           2 :         avio_skip(pb, 8);
     141           2 :         bytes_per_minute = avio_rb16(pb);
     142           2 :         avio_skip(pb, 4);
     143           2 :         rm_read_metadata(s, pb, 0);
     144           2 :         if ((startpos + header_size) >= avio_tell(pb) + 2) {
     145             :             // fourcc (should always be "lpcJ")
     146           2 :             avio_r8(pb);
     147           2 :             get_str8(pb, buf, sizeof(buf));
     148             :         }
     149             :         // Skip extra header crap (this should never happen)
     150           2 :         if ((startpos + header_size) > avio_tell(pb))
     151           0 :             avio_skip(pb, header_size + startpos - avio_tell(pb));
     152           2 :         if (bytes_per_minute)
     153           2 :             st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
     154           2 :         st->codecpar->sample_rate = 8000;
     155           2 :         st->codecpar->channels = 1;
     156           2 :         st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     157           2 :         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     158           2 :         st->codecpar->codec_id = AV_CODEC_ID_RA_144;
     159           2 :         ast->deint_id = DEINT_ID_INT0;
     160             :     } else {
     161             :         int flavor, sub_packet_h, coded_framesize, sub_packet_size;
     162             :         int codecdata_length;
     163             :         unsigned bytes_per_minute;
     164             :         /* old version (4) */
     165          12 :         avio_skip(pb, 2); /* unused */
     166          12 :         avio_rb32(pb); /* .ra4 */
     167          12 :         avio_rb32(pb); /* data size */
     168          12 :         avio_rb16(pb); /* version2 */
     169          12 :         avio_rb32(pb); /* header size */
     170          12 :         flavor= avio_rb16(pb); /* add codec info / flavor */
     171          12 :         ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
     172          12 :         avio_rb32(pb); /* ??? */
     173          12 :         bytes_per_minute = avio_rb32(pb);
     174          12 :         if (version == 4) {
     175           7 :             if (bytes_per_minute)
     176           7 :                 st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
     177             :         }
     178          12 :         avio_rb32(pb); /* ??? */
     179          12 :         ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
     180          12 :         st->codecpar->block_align= avio_rb16(pb); /* frame size */
     181          12 :         ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
     182          12 :         avio_rb16(pb); /* ??? */
     183          12 :         if (version == 5) {
     184           5 :             avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
     185             :         }
     186          12 :         st->codecpar->sample_rate = avio_rb16(pb);
     187          12 :         avio_rb32(pb);
     188          12 :         st->codecpar->channels = avio_rb16(pb);
     189          12 :         if (version == 5) {
     190           5 :             ast->deint_id = avio_rl32(pb);
     191           5 :             avio_read(pb, buf, 4);
     192           5 :             buf[4] = 0;
     193             :         } else {
     194           7 :             AV_WL32(buf, 0);
     195           7 :             get_str8(pb, buf, sizeof(buf)); /* desc */
     196           7 :             ast->deint_id = AV_RL32(buf);
     197           7 :             get_str8(pb, buf, sizeof(buf)); /* desc */
     198             :         }
     199          12 :         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     200          12 :         st->codecpar->codec_tag  = AV_RL32(buf);
     201          12 :         st->codecpar->codec_id   = ff_codec_get_id(ff_rm_codec_tags,
     202          12 :                                                    st->codecpar->codec_tag);
     203             : 
     204          12 :         switch (st->codecpar->codec_id) {
     205           1 :         case AV_CODEC_ID_AC3:
     206           1 :             st->need_parsing = AVSTREAM_PARSE_FULL;
     207           1 :             break;
     208           1 :         case AV_CODEC_ID_RA_288:
     209           1 :             st->codecpar->extradata_size= 0;
     210           1 :             av_freep(&st->codecpar->extradata);
     211           1 :             ast->audio_framesize = st->codecpar->block_align;
     212           1 :             st->codecpar->block_align = coded_framesize;
     213           1 :             break;
     214           5 :         case AV_CODEC_ID_COOK:
     215           5 :             st->need_parsing = AVSTREAM_PARSE_HEADERS;
     216           9 :         case AV_CODEC_ID_ATRAC3:
     217             :         case AV_CODEC_ID_SIPR:
     218           9 :             if (read_all) {
     219           0 :                 codecdata_length = 0;
     220             :             } else {
     221           9 :                 avio_rb16(pb); avio_r8(pb);
     222           9 :                 if (version == 5)
     223           5 :                     avio_r8(pb);
     224           9 :                 codecdata_length = avio_rb32(pb);
     225           9 :                 if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
     226           0 :                     av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
     227           0 :                     return -1;
     228             :                 }
     229             :             }
     230             : 
     231           9 :             ast->audio_framesize = st->codecpar->block_align;
     232           9 :             if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) {
     233           4 :                 if (flavor > 3) {
     234           0 :                     av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
     235             :                            flavor);
     236           0 :                     return -1;
     237             :                 }
     238           4 :                 st->codecpar->block_align = ff_sipr_subpk_size[flavor];
     239           4 :                 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     240             :             } else {
     241           5 :                 if(sub_packet_size <= 0){
     242           0 :                     av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
     243           0 :                     return -1;
     244             :                 }
     245           5 :                 st->codecpar->block_align = ast->sub_packet_size;
     246             :             }
     247           9 :             if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length)) < 0)
     248           0 :                 return ret;
     249             : 
     250           9 :             break;
     251           0 :         case AV_CODEC_ID_AAC:
     252           0 :             avio_rb16(pb); avio_r8(pb);
     253           0 :             if (version == 5)
     254           0 :                 avio_r8(pb);
     255           0 :             codecdata_length = avio_rb32(pb);
     256           0 :             if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
     257           0 :                 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
     258           0 :                 return -1;
     259             :             }
     260           0 :             if (codecdata_length >= 1) {
     261           0 :                 avio_r8(pb);
     262           0 :                 if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length - 1)) < 0)
     263           0 :                     return ret;
     264             :             }
     265           0 :             break;
     266             :         }
     267          12 :         switch (ast->deint_id) {
     268           1 :         case DEINT_ID_INT4:
     269           1 :             if (ast->coded_framesize > ast->audio_framesize ||
     270           1 :                 sub_packet_h <= 1 ||
     271           1 :                 ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
     272           0 :                 return AVERROR_INVALIDDATA;
     273           1 :             if (ast->coded_framesize * sub_packet_h != 2*ast->audio_framesize) {
     274           0 :                 avpriv_request_sample(s, "mismatching interleaver parameters");
     275           0 :                 return AVERROR_INVALIDDATA;
     276             :             }
     277           1 :             break;
     278           5 :         case DEINT_ID_GENR:
     279          10 :             if (ast->sub_packet_size <= 0 ||
     280           5 :                 ast->sub_packet_size > ast->audio_framesize)
     281           0 :                 return AVERROR_INVALIDDATA;
     282           5 :             if (ast->audio_framesize % ast->sub_packet_size)
     283           0 :                 return AVERROR_INVALIDDATA;
     284           5 :             break;
     285           6 :         case DEINT_ID_SIPR:
     286             :         case DEINT_ID_INT0:
     287             :         case DEINT_ID_VBRS:
     288             :         case DEINT_ID_VBRF:
     289           6 :             break;
     290           0 :         default:
     291           0 :             av_log(s, AV_LOG_ERROR ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
     292           0 :             return AVERROR_INVALIDDATA;
     293             :         }
     294          23 :         if (ast->deint_id == DEINT_ID_INT4 ||
     295          17 :             ast->deint_id == DEINT_ID_GENR ||
     296           6 :             ast->deint_id == DEINT_ID_SIPR) {
     297          20 :             if (st->codecpar->block_align <= 0 ||
     298          20 :                 ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
     299          10 :                 ast->audio_framesize * sub_packet_h < st->codecpar->block_align)
     300           0 :                 return AVERROR_INVALIDDATA;
     301          10 :             if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
     302           0 :                 return AVERROR(ENOMEM);
     303             :         }
     304             : 
     305          12 :         if (read_all) {
     306           0 :             avio_r8(pb);
     307           0 :             avio_r8(pb);
     308           0 :             avio_r8(pb);
     309           0 :             rm_read_metadata(s, pb, 0);
     310             :         }
     311             :     }
     312          14 :     return 0;
     313             : }
     314             : 
     315          42 : int ff_rm_read_mdpr_codecdata(AVFormatContext *s, AVIOContext *pb,
     316             :                               AVStream *st, RMStream *rst,
     317             :                               unsigned int codec_data_size, const uint8_t *mime)
     318             : {
     319             :     unsigned int v;
     320             :     int size;
     321             :     int64_t codec_pos;
     322             :     int ret;
     323             : 
     324          42 :     if (codec_data_size > INT_MAX)
     325           0 :         return AVERROR_INVALIDDATA;
     326          42 :     if (codec_data_size == 0)
     327           0 :         return 0;
     328             : 
     329          42 :     avpriv_set_pts_info(st, 64, 1, 1000);
     330          42 :     codec_pos = avio_tell(pb);
     331          42 :     v = avio_rb32(pb);
     332             : 
     333          42 :     if (v == MKTAG(0xfd, 'a', 'r', '.')) {
     334             :         /* ra type header */
     335          13 :         if (rm_read_audio_stream_info(s, pb, st, rst, 0))
     336           0 :             return -1;
     337          29 :     } else if (v == MKBETAG('L', 'S', 'D', ':')) {
     338           1 :         avio_seek(pb, -4, SEEK_CUR);
     339           1 :         if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size)) < 0)
     340           0 :             return ret;
     341             : 
     342           1 :         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     343           1 :         st->codecpar->codec_tag  = AV_RL32(st->codecpar->extradata);
     344           1 :         st->codecpar->codec_id   = ff_codec_get_id(ff_rm_codec_tags,
     345           1 :                                                 st->codecpar->codec_tag);
     346          38 :     } else if(mime && !strcmp(mime, "logical-fileinfo")){
     347             :         int stream_count, rule_count, property_count, i;
     348          10 :         ff_free_stream(s, st);
     349          10 :         if (avio_rb16(pb) != 0) {
     350           0 :             av_log(s, AV_LOG_WARNING, "Unsupported version\n");
     351           0 :             goto skip;
     352             :         }
     353          10 :         stream_count = avio_rb16(pb);
     354          10 :         avio_skip(pb, 6*stream_count);
     355          10 :         rule_count = avio_rb16(pb);
     356          10 :         avio_skip(pb, 2*rule_count);
     357          10 :         property_count = avio_rb16(pb);
     358          99 :         for(i=0; i<property_count; i++){
     359             :             uint8_t name[128], val[128];
     360          89 :             avio_rb32(pb);
     361          89 :             if (avio_rb16(pb) != 0) {
     362           0 :                 av_log(s, AV_LOG_WARNING, "Unsupported Name value property version\n");
     363           0 :                 goto skip; //FIXME skip just this one
     364             :             }
     365          89 :             get_str8(pb, name, sizeof(name));
     366          89 :             switch(avio_rb32(pb)) {
     367          77 :             case 2: get_strl(pb, val, sizeof(val), avio_rb16(pb));
     368          77 :                 av_dict_set(&s->metadata, name, val, 0);
     369          77 :                 break;
     370          12 :             default: avio_skip(pb, avio_rb16(pb));
     371             :             }
     372             :         }
     373             :     } else {
     374             :         int fps;
     375          18 :         if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
     376           0 :         fail1:
     377           0 :             av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
     378           0 :             goto skip;
     379             :         }
     380          18 :         st->codecpar->codec_tag = avio_rl32(pb);
     381          18 :         st->codecpar->codec_id  = ff_codec_get_id(ff_rm_codec_tags,
     382          18 :                                                   st->codecpar->codec_tag);
     383          18 :         av_log(s, AV_LOG_TRACE, "%"PRIX32" %X\n",
     384          18 :                st->codecpar->codec_tag, MKTAG('R', 'V', '2', '0'));
     385          18 :         if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
     386           0 :             goto fail1;
     387          18 :         st->codecpar->width  = avio_rb16(pb);
     388          18 :         st->codecpar->height = avio_rb16(pb);
     389          18 :         avio_skip(pb, 2); // looks like bits per sample
     390          18 :         avio_skip(pb, 4); // always zero?
     391          18 :         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     392          18 :         st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
     393          18 :         fps = avio_rb32(pb);
     394             : 
     395          18 :         if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
     396           0 :             return ret;
     397             : 
     398          18 :         if (fps > 0) {
     399          18 :             av_reduce(&st->avg_frame_rate.den, &st->avg_frame_rate.num,
     400             :                       0x10000, fps, (1 << 30) - 1);
     401             : #if FF_API_R_FRAME_RATE
     402          18 :             st->r_frame_rate = st->avg_frame_rate;
     403             : #endif
     404           0 :         } else if (s->error_recognition & AV_EF_EXPLODE) {
     405           0 :             av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
     406           0 :             return AVERROR_INVALIDDATA;
     407             :         }
     408             :     }
     409             : 
     410          42 : skip:
     411             :     /* skip codec info */
     412          42 :     size = avio_tell(pb) - codec_pos;
     413          42 :     if (codec_data_size >= size) {
     414          42 :         avio_skip(pb, codec_data_size - size);
     415             :     } else {
     416           0 :         av_log(s, AV_LOG_WARNING, "codec_data_size %u < size %d\n", codec_data_size, size);
     417             :     }
     418             : 
     419          42 :     return 0;
     420             : }
     421             : 
     422             : /** this function assumes that the demuxer has already seeked to the start
     423             :  * of the INDX chunk, and will bail out if not. */
     424          10 : static int rm_read_index(AVFormatContext *s)
     425             : {
     426          10 :     AVIOContext *pb = s->pb;
     427             :     unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
     428             :     AVStream *st;
     429             : 
     430             :     do {
     431          12 :         if (avio_rl32(pb) != MKTAG('I','N','D','X'))
     432           8 :             return -1;
     433           4 :         size     = avio_rb32(pb);
     434           4 :         if (size < 20)
     435           0 :             return -1;
     436           4 :         avio_skip(pb, 2);
     437           4 :         n_pkts   = avio_rb32(pb);
     438           4 :         str_id   = avio_rb16(pb);
     439           4 :         next_off = avio_rb32(pb);
     440           6 :         for (n = 0; n < s->nb_streams; n++)
     441           5 :             if (s->streams[n]->id == str_id) {
     442           3 :                 st = s->streams[n];
     443           3 :                 break;
     444             :             }
     445           4 :         if (n == s->nb_streams) {
     446           1 :             av_log(s, AV_LOG_ERROR,
     447             :                    "Invalid stream index %d for index at pos %"PRId64"\n",
     448             :                    str_id, avio_tell(pb));
     449           1 :             goto skip;
     450           3 :         } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
     451           0 :             av_log(s, AV_LOG_ERROR,
     452             :                    "Nr. of packets in packet index for stream index %d "
     453             :                    "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
     454             :                    str_id, avio_size(pb), avio_tell(pb),
     455           0 :                    (avio_size(pb) - avio_tell(pb)) / 14);
     456           0 :             goto skip;
     457             :         }
     458             : 
     459          21 :         for (n = 0; n < n_pkts; n++) {
     460          18 :             avio_skip(pb, 2);
     461          18 :             pts = avio_rb32(pb);
     462          18 :             pos = avio_rb32(pb);
     463          18 :             avio_skip(pb, 4); /* packet no. */
     464             : 
     465          18 :             av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
     466             :         }
     467             : 
     468           3 : skip:
     469           4 :         if (next_off && avio_tell(pb) < next_off &&
     470           0 :             avio_seek(pb, next_off, SEEK_SET) < 0) {
     471           0 :             av_log(s, AV_LOG_ERROR,
     472             :                    "Non-linear index detected, not supported\n");
     473           0 :             return -1;
     474             :         }
     475           4 :     } while (next_off);
     476             : 
     477           2 :     return 0;
     478             : }
     479             : 
     480           1 : static int rm_read_header_old(AVFormatContext *s)
     481             : {
     482           1 :     RMDemuxContext *rm = s->priv_data;
     483             :     AVStream *st;
     484             : 
     485           1 :     rm->old_format = 1;
     486           1 :     st = avformat_new_stream(s, NULL);
     487           1 :     if (!st)
     488           0 :         return -1;
     489           1 :     st->priv_data = ff_rm_alloc_rmstream();
     490           1 :     if (!st->priv_data)
     491           0 :         return AVERROR(ENOMEM);
     492           1 :     return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
     493             : }
     494             : 
     495           0 : static int rm_read_multi(AVFormatContext *s, AVIOContext *pb,
     496             :                          AVStream *st, char *mime)
     497             : {
     498           0 :     int number_of_streams = avio_rb16(pb);
     499             :     int number_of_mdpr;
     500             :     int i, ret;
     501             :     unsigned size2;
     502           0 :     for (i = 0; i<number_of_streams; i++)
     503           0 :         avio_rb16(pb);
     504           0 :     number_of_mdpr = avio_rb16(pb);
     505           0 :     if (number_of_mdpr != 1) {
     506           0 :         avpriv_request_sample(s, "MLTI with multiple (%d) MDPR", number_of_mdpr);
     507             :     }
     508           0 :     for (i = 0; i < number_of_mdpr; i++) {
     509             :         AVStream *st2;
     510           0 :         if (i > 0) {
     511           0 :             st2 = avformat_new_stream(s, NULL);
     512           0 :             if (!st2) {
     513           0 :                 ret = AVERROR(ENOMEM);
     514           0 :                 return ret;
     515             :             }
     516           0 :             st2->id = st->id + (i<<16);
     517           0 :             st2->codecpar->bit_rate = st->codecpar->bit_rate;
     518           0 :             st2->start_time = st->start_time;
     519           0 :             st2->duration   = st->duration;
     520           0 :             st2->codecpar->codec_type = AVMEDIA_TYPE_DATA;
     521           0 :             st2->priv_data = ff_rm_alloc_rmstream();
     522           0 :             if (!st2->priv_data)
     523           0 :                 return AVERROR(ENOMEM);
     524             :         } else
     525           0 :             st2 = st;
     526             : 
     527           0 :         size2 = avio_rb32(pb);
     528           0 :         ret = ff_rm_read_mdpr_codecdata(s, s->pb, st2, st2->priv_data,
     529             :                                         size2, mime);
     530           0 :         if (ret < 0)
     531           0 :             return ret;
     532             :     }
     533           0 :     return 0;
     534             : }
     535             : 
     536          24 : static int rm_read_header(AVFormatContext *s)
     537             : {
     538          24 :     RMDemuxContext *rm = s->priv_data;
     539             :     AVStream *st;
     540          24 :     AVIOContext *pb = s->pb;
     541             :     unsigned int tag;
     542             :     int tag_size;
     543             :     unsigned int start_time, duration;
     544          24 :     unsigned int data_off = 0, indx_off = 0;
     545             :     char buf[128], mime[128];
     546          24 :     int flags = 0;
     547          24 :     int ret = -1;
     548             :     unsigned size, v;
     549             :     int64_t codec_pos;
     550             : 
     551          24 :     tag = avio_rl32(pb);
     552          24 :     if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
     553             :         /* very old .ra format */
     554           1 :         return rm_read_header_old(s);
     555          23 :     } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
     556           0 :         return AVERROR(EIO);
     557             :     }
     558             : 
     559          23 :     tag_size = avio_rb32(pb);
     560          23 :     avio_skip(pb, tag_size - 8);
     561             : 
     562             :     for(;;) {
     563         199 :         if (avio_feof(pb))
     564           0 :             goto fail;
     565         111 :         tag = avio_rl32(pb);
     566         111 :         tag_size = avio_rb32(pb);
     567         111 :         avio_rb16(pb);
     568         111 :         av_log(s, AV_LOG_TRACE, "tag=%s size=%d\n",
     569         111 :                av_fourcc2str(tag), tag_size);
     570         111 :         if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
     571           0 :             goto fail;
     572         111 :         switch(tag) {
     573          23 :         case MKTAG('P', 'R', 'O', 'P'):
     574             :             /* file header */
     575          23 :             avio_rb32(pb); /* max bit rate */
     576          23 :             avio_rb32(pb); /* avg bit rate */
     577          23 :             avio_rb32(pb); /* max packet size */
     578          23 :             avio_rb32(pb); /* avg packet size */
     579          23 :             avio_rb32(pb); /* nb packets */
     580          23 :             duration = avio_rb32(pb); /* duration */
     581          23 :             s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
     582          23 :             avio_rb32(pb); /* preroll */
     583          23 :             indx_off = avio_rb32(pb); /* index offset */
     584          23 :             data_off = avio_rb32(pb); /* data offset */
     585          23 :             avio_rb16(pb); /* nb streams */
     586          23 :             flags = avio_rb16(pb); /* flags */
     587          23 :             break;
     588          23 :         case MKTAG('C', 'O', 'N', 'T'):
     589          23 :             rm_read_metadata(s, pb, 1);
     590          23 :             break;
     591          42 :         case MKTAG('M', 'D', 'P', 'R'):
     592          42 :             st = avformat_new_stream(s, NULL);
     593          42 :             if (!st) {
     594           0 :                 ret = AVERROR(ENOMEM);
     595           0 :                 goto fail;
     596             :             }
     597          42 :             st->id = avio_rb16(pb);
     598          42 :             avio_rb32(pb); /* max bit rate */
     599          42 :             st->codecpar->bit_rate = avio_rb32(pb); /* bit rate */
     600          42 :             avio_rb32(pb); /* max packet size */
     601          42 :             avio_rb32(pb); /* avg packet size */
     602          42 :             start_time = avio_rb32(pb); /* start time */
     603          42 :             avio_rb32(pb); /* preroll */
     604          42 :             duration = avio_rb32(pb); /* duration */
     605          42 :             st->start_time = start_time;
     606          42 :             st->duration = duration;
     607          42 :             if(duration>0)
     608          31 :                 s->duration = AV_NOPTS_VALUE;
     609          42 :             get_str8(pb, buf, sizeof(buf)); /* desc */
     610          42 :             get_str8(pb, mime, sizeof(mime)); /* mimetype */
     611          42 :             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
     612          42 :             st->priv_data = ff_rm_alloc_rmstream();
     613          42 :             if (!st->priv_data)
     614           0 :                 return AVERROR(ENOMEM);
     615             : 
     616          42 :             size = avio_rb32(pb);
     617          42 :             codec_pos = avio_tell(pb);
     618             : 
     619          42 :             ffio_ensure_seekback(pb, 4);
     620          42 :             v = avio_rb32(pb);
     621          42 :             if (v == MKBETAG('M', 'L', 'T', 'I')) {
     622           0 :                 ret = rm_read_multi(s, s->pb, st, mime);
     623           0 :                 if (ret < 0)
     624           0 :                     goto fail;
     625           0 :                 avio_seek(pb, codec_pos + size, SEEK_SET);
     626             :             } else {
     627          42 :                 avio_skip(pb, -4);
     628          42 :                 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
     629             :                                               size, mime) < 0)
     630           0 :                     goto fail;
     631             :             }
     632             : 
     633          42 :             break;
     634          23 :         case MKTAG('D', 'A', 'T', 'A'):
     635          23 :             goto header_end;
     636           0 :         default:
     637             :             /* unknown tag: skip it */
     638           0 :             avio_skip(pb, tag_size - 10);
     639           0 :             break;
     640             :         }
     641             :     }
     642          23 :  header_end:
     643          23 :     rm->nb_packets = avio_rb32(pb); /* number of packets */
     644          23 :     if (!rm->nb_packets && (flags & 4))
     645           0 :         rm->nb_packets = 3600 * 25;
     646          23 :     avio_rb32(pb); /* next data header */
     647             : 
     648          23 :     if (!data_off)
     649           0 :         data_off = avio_tell(pb) - 18;
     650          33 :     if (indx_off && (pb->seekable & AVIO_SEEKABLE_NORMAL) &&
     651          20 :         !(s->flags & AVFMT_FLAG_IGNIDX) &&
     652          10 :         avio_seek(pb, indx_off, SEEK_SET) >= 0) {
     653          10 :         rm_read_index(s);
     654          10 :         avio_seek(pb, data_off + 18, SEEK_SET);
     655             :     }
     656             : 
     657          23 :     return 0;
     658             : 
     659           0 : fail:
     660           0 :     rm_read_close(s);
     661           0 :     return ret;
     662             : }
     663             : 
     664        7322 : static int get_num(AVIOContext *pb, int *len)
     665             : {
     666             :     int n, n1;
     667             : 
     668        7322 :     n = avio_rb16(pb);
     669        7322 :     (*len)-=2;
     670        7322 :     n &= 0x7FFF;
     671        7322 :     if (n >= 0x4000) {
     672        6571 :         return n - 0x4000;
     673             :     } else {
     674         751 :         n1 = avio_rb16(pb);
     675         751 :         (*len)-=2;
     676         751 :         return (n << 16) | n1;
     677             :     }
     678             : }
     679             : 
     680             : /* multiple of 20 bytes for ra144 (ugly) */
     681             : #define RAW_PACKET_SIZE 1000
     682             : 
     683        7177 : static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
     684        7177 :     RMDemuxContext *rm = s->priv_data;
     685        7177 :     AVIOContext *pb = s->pb;
     686             :     AVStream *st;
     687        7177 :     uint32_t state=0xFFFFFFFF;
     688             : 
     689     1371632 :     while(!avio_feof(pb)){
     690             :         int len, num, i;
     691             :         int mlti_id;
     692     1364339 :         *pos= avio_tell(pb) - 3;
     693     1364339 :         if(rm->remaining_len > 0){
     694         557 :             num= rm->current_stream;
     695         557 :             mlti_id = 0;
     696         557 :             len= rm->remaining_len;
     697         557 :             *timestamp = AV_NOPTS_VALUE;
     698         557 :             *flags= 0;
     699             :         }else{
     700     1363782 :             state= (state<<8) + avio_r8(pb);
     701             : 
     702     1363782 :             if(state == MKBETAG('I', 'N', 'D', 'X')){
     703             :                 int n_pkts, expected_len;
     704           4 :                 len = avio_rb32(pb);
     705           4 :                 avio_skip(pb, 2);
     706           4 :                 n_pkts = avio_rb32(pb);
     707           4 :                 expected_len = 20 + n_pkts * 14;
     708           4 :                 if (len == 20)
     709             :                     /* some files don't add index entries to chunk size... */
     710           2 :                     len = expected_len;
     711           2 :                 else if (len != expected_len)
     712           0 :                     av_log(s, AV_LOG_WARNING,
     713             :                            "Index size %d (%d pkts) is wrong, should be %d.\n",
     714             :                            len, n_pkts, expected_len);
     715           4 :                 len -= 14; // we already read part of the index header
     716           4 :                 if(len<0)
     717           0 :                     continue;
     718           4 :                 goto skip;
     719     1363778 :             } else if (state == MKBETAG('D','A','T','A')) {
     720           0 :                 av_log(s, AV_LOG_WARNING,
     721             :                        "DATA tag in middle of chunk, file may be broken.\n");
     722             :             }
     723             : 
     724     1363778 :             if(state > (unsigned)0xFFFF || state <= 12)
     725     1357208 :                 continue;
     726        6570 :             len=state - 12;
     727        6570 :             state= 0xFFFFFFFF;
     728             : 
     729        6570 :             num = avio_rb16(pb);
     730        6570 :             *timestamp = avio_rb32(pb);
     731        6570 :             mlti_id = (avio_r8(pb)>>1)-1<<16;
     732        6570 :             mlti_id = FFMAX(mlti_id, 0);
     733        6570 :             *flags = avio_r8(pb); /* flags */
     734             :         }
     735       10140 :         for(i=0;i<s->nb_streams;i++) {
     736       10074 :             st = s->streams[i];
     737       10074 :             if (mlti_id + num == st->id)
     738        7061 :                 break;
     739             :         }
     740        7127 :         if (i == s->nb_streams) {
     741          66 : skip:
     742             :             /* skip packet if unknown number */
     743          70 :             avio_skip(pb, len);
     744          70 :             rm->remaining_len = 0;
     745          70 :             continue;
     746             :         }
     747        7061 :         *stream_index= i;
     748             : 
     749        7061 :         return len;
     750             :     }
     751         116 :     return -1;
     752             : }
     753             : 
     754        3665 : static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
     755             :                                    RMDemuxContext *rm, RMStream *vst,
     756             :                                    AVPacket *pkt, int len, int *pseq,
     757             :                                    int64_t *timestamp)
     758             : {
     759             :     int hdr;
     760        3665 :     int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silence compiler warning
     761             :     int type;
     762             :     int ret;
     763             : 
     764        3665 :     hdr = avio_r8(pb); len--;
     765        3665 :     type = hdr >> 6;
     766             : 
     767        3665 :     if(type != 3){  // not frame as a part of packet
     768        2426 :         seq = avio_r8(pb); len--;
     769             :     }
     770        3665 :     if(type != 1){  // not whole frame
     771        3661 :         len2 = get_num(pb, &len);
     772        3661 :         pos  = get_num(pb, &len);
     773        3661 :         pic_num = avio_r8(pb); len--;
     774             :     }
     775        3665 :     if(len<0) {
     776           0 :         av_log(s, AV_LOG_ERROR, "Insufficient data\n");
     777           0 :         return -1;
     778             :     }
     779        3665 :     rm->remaining_len = len;
     780        3665 :     if(type&1){     // frame, not slice
     781        1243 :         if(type == 3){  // frame as a part of packet
     782        1239 :             len= len2;
     783        1239 :             *timestamp = pos;
     784             :         }
     785        1243 :         if(rm->remaining_len < len) {
     786           0 :             av_log(s, AV_LOG_ERROR, "Insufficient remaining len\n");
     787           0 :             return -1;
     788             :         }
     789        1243 :         rm->remaining_len -= len;
     790        1243 :         if(av_new_packet(pkt, len + 9) < 0)
     791           0 :             return AVERROR(EIO);
     792        1243 :         pkt->data[0] = 0;
     793        1243 :         AV_WL32(pkt->data + 1, 1);
     794        1243 :         AV_WL32(pkt->data + 5, 0);
     795        1243 :         if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
     796           2 :             av_packet_unref(pkt);
     797           2 :             av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
     798           2 :             return ret < 0 ? ret : AVERROR(EIO);
     799             :         }
     800        1241 :         return 0;
     801             :     }
     802             :     //now we have to deal with single slice
     803             : 
     804        2422 :     *pseq = seq;
     805        2422 :     if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
     806         967 :         if (len2 > ffio_limit(pb, len2)) {
     807           0 :             av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
     808           0 :             return AVERROR_INVALIDDATA;
     809             :         }
     810         967 :         vst->slices = ((hdr & 0x3F) << 1) + 1;
     811         967 :         vst->videobufsize = len2 + 8*vst->slices + 1;
     812         967 :         av_packet_unref(&vst->pkt); //FIXME this should be output.
     813         967 :         if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
     814           0 :             return AVERROR(ENOMEM);
     815         967 :         memset(vst->pkt.data, 0, vst->pkt.size);
     816         967 :         vst->videobufpos = 8*vst->slices + 1;
     817         967 :         vst->cur_slice = 0;
     818         967 :         vst->curpic_num = pic_num;
     819         967 :         vst->pktpos = avio_tell(pb);
     820             :     }
     821        2422 :     if(type == 2)
     822         957 :         len = FFMIN(len, pos);
     823             : 
     824        2422 :     if(++vst->cur_slice > vst->slices) {
     825           0 :         av_log(s, AV_LOG_ERROR, "cur slice %d, too large\n", vst->cur_slice);
     826           0 :         return 1;
     827             :     }
     828        2422 :     if(!vst->pkt.data)
     829           0 :         return AVERROR(ENOMEM);
     830        2422 :     AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
     831        2422 :     AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
     832        2422 :     if(vst->videobufpos + len > vst->videobufsize) {
     833           0 :         av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
     834           0 :         return 1;
     835             :     }
     836        2422 :     if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
     837           0 :         return AVERROR(EIO);
     838        2422 :     vst->videobufpos += len;
     839        2422 :     rm->remaining_len-= len;
     840             : 
     841        2422 :     if (type == 2 || vst->videobufpos == vst->videobufsize) {
     842         967 :         vst->pkt.data[0] = vst->cur_slice-1;
     843         967 :         *pkt= vst->pkt;
     844         967 :         vst->pkt.data= NULL;
     845         967 :         vst->pkt.size= 0;
     846         967 :         vst->pkt.buf = NULL;
     847         967 :         if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
     848         767 :             memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
     849         767 :                 vst->videobufpos - 1 - 8*vst->slices);
     850         967 :         pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
     851         967 :         pkt->pts = AV_NOPTS_VALUE;
     852         967 :         pkt->pos = vst->pktpos;
     853         967 :         vst->slices = 0;
     854         967 :         return 0;
     855             :     }
     856             : 
     857        1455 :     return 1;
     858             : }
     859             : 
     860             : static inline void
     861         955 : rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
     862             : {
     863             :     uint8_t *ptr;
     864             :     int j;
     865             : 
     866         955 :     if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
     867           9 :         ptr = pkt->data;
     868        1260 :         for (j=0;j<pkt->size;j+=2) {
     869        1251 :             FFSWAP(int, ptr[0], ptr[1]);
     870        1251 :             ptr += 2;
     871             :         }
     872             :     }
     873         955 : }
     874             : 
     875        4161 : static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n) {
     876        4161 :     int ret = avio_read(pb, dst, n);
     877        4161 :     if (ret != n) {
     878           7 :         if (ret >= 0) memset(dst + ret, 0, n - ret);
     879           5 :         else          memset(dst      , 0, n);
     880           7 :         av_log(s, AV_LOG_ERROR, "Failed to fully read block\n");
     881             :     }
     882        4161 :     return ret;
     883             : }
     884             : 
     885             : int
     886        5809 : ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
     887             :                     AVStream *st, RMStream *ast, int len, AVPacket *pkt,
     888             :                     int *seq, int flags, int64_t timestamp)
     889             : {
     890        5809 :     RMDemuxContext *rm = s->priv_data;
     891             :     int ret;
     892             : 
     893        5809 :     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
     894        3665 :         rm->current_stream= st->id;
     895        3665 :         ret = rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp);
     896        3665 :         if(ret)
     897        1457 :             return ret < 0 ? ret : -1; //got partial frame or error
     898        2144 :     } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
     899        4043 :         if ((ast->deint_id == DEINT_ID_GENR) ||
     900        3383 :             (ast->deint_id == DEINT_ID_INT4) ||
     901        1619 :             (ast->deint_id == DEINT_ID_SIPR)) {
     902             :             int x;
     903        1188 :             int sps = ast->sub_packet_size;
     904        1188 :             int cfs = ast->coded_framesize;
     905        1188 :             int h = ast->sub_packet_h;
     906        1188 :             int y = ast->sub_packet_cnt;
     907        1188 :             int w = ast->audio_framesize;
     908             : 
     909        1188 :             if (flags & 2)
     910         141 :                 y = ast->sub_packet_cnt = 0;
     911        1188 :             if (!y)
     912         141 :                 ast->audiotimestamp = timestamp;
     913             : 
     914        1188 :             switch (ast->deint_id) {
     915         415 :                 case DEINT_ID_INT4:
     916        2905 :                     for (x = 0; x < h/2; x++)
     917        2490 :                         readfull(s, pb, ast->pkt.data+x*2*w+y*cfs, cfs);
     918         415 :                     break;
     919         245 :                 case DEINT_ID_GENR:
     920        1388 :                     for (x = 0; x < w/sps; x++)
     921        1143 :                         readfull(s, pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
     922         245 :                     break;
     923         528 :                 case DEINT_ID_SIPR:
     924         528 :                     readfull(s, pb, ast->pkt.data + y * w, w);
     925         528 :                     break;
     926             :             }
     927             : 
     928        1188 :             if (++(ast->sub_packet_cnt) < h)
     929        1053 :                 return -1;
     930         135 :             if (ast->deint_id == DEINT_ID_SIPR)
     931          88 :                 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
     932             : 
     933         135 :              ast->sub_packet_cnt = 0;
     934         135 :              rm->audio_stream_num = st->index;
     935         135 :             if (st->codecpar->block_align <= 0) {
     936           0 :                 av_log(s, AV_LOG_ERROR, "Invalid block alignment %d\n", st->codecpar->block_align);
     937           0 :                 return AVERROR_INVALIDDATA;
     938             :             }
     939         135 :              rm->audio_pkt_cnt = h * w / st->codecpar->block_align;
     940        1912 :         } else if ((ast->deint_id == DEINT_ID_VBRF) ||
     941         956 :                    (ast->deint_id == DEINT_ID_VBRS)) {
     942             :             int x;
     943           0 :             rm->audio_stream_num = st->index;
     944           0 :             ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
     945           0 :             if (ast->sub_packet_cnt) {
     946           0 :                 for (x = 0; x < ast->sub_packet_cnt; x++)
     947           0 :                     ast->sub_packet_lengths[x] = avio_rb16(pb);
     948           0 :                 rm->audio_pkt_cnt = ast->sub_packet_cnt;
     949           0 :                 ast->audiotimestamp = timestamp;
     950             :             } else
     951           0 :                 return -1;
     952             :         } else {
     953         956 :             ret = av_get_packet(pb, pkt, len);
     954         956 :             if (ret < 0)
     955           1 :                 return ret;
     956         955 :             rm_ac3_swap_bytes(st, pkt);
     957             :         }
     958             :     } else {
     959           0 :         ret = av_get_packet(pb, pkt, len);
     960           0 :         if (ret < 0)
     961           0 :             return ret;
     962             :     }
     963             : 
     964        3298 :     pkt->stream_index = st->index;
     965             : 
     966        3298 :     pkt->pts = timestamp;
     967        3298 :     if (flags & 2)
     968         685 :         pkt->flags |= AV_PKT_FLAG_KEY;
     969             : 
     970        3298 :     return st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
     971             : }
     972             : 
     973             : int
     974        9906 : ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
     975             :                       AVStream *st, RMStream *ast, AVPacket *pkt)
     976             : {
     977        9906 :     RMDemuxContext *rm = s->priv_data;
     978             :     int ret;
     979             : 
     980        9906 :     av_assert0 (rm->audio_pkt_cnt > 0);
     981             : 
     982       19812 :     if (ast->deint_id == DEINT_ID_VBRF ||
     983        9906 :         ast->deint_id == DEINT_ID_VBRS) {
     984           0 :         ret = av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
     985           0 :         if (ret < 0)
     986           0 :             return ret;
     987             :     } else {
     988        9906 :         ret = av_new_packet(pkt, st->codecpar->block_align);
     989        9906 :         if (ret < 0)
     990           0 :             return ret;
     991       19812 :         memcpy(pkt->data, ast->pkt.data + st->codecpar->block_align * //FIXME avoid this
     992        9906 :                (ast->sub_packet_h * ast->audio_framesize / st->codecpar->block_align - rm->audio_pkt_cnt),
     993        9906 :                st->codecpar->block_align);
     994             :     }
     995        9906 :     rm->audio_pkt_cnt--;
     996        9906 :     if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
     997         135 :         ast->audiotimestamp = AV_NOPTS_VALUE;
     998         135 :         pkt->flags = AV_PKT_FLAG_KEY;
     999             :     } else
    1000        9771 :         pkt->flags = 0;
    1001        9906 :     pkt->stream_index = st->index;
    1002             : 
    1003        9906 :     return rm->audio_pkt_cnt;
    1004             : }
    1005             : 
    1006       11449 : static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
    1007             : {
    1008       11449 :     RMDemuxContext *rm = s->priv_data;
    1009       11449 :     AVStream *st = NULL; // init to silence compiler warning
    1010       11449 :     int i, len, res, seq = 1;
    1011             :     int64_t timestamp, pos;
    1012             :     int flags;
    1013             : 
    1014             :     for (;;) {
    1015       20017 :         if (rm->audio_pkt_cnt) {
    1016             :             // If there are queued audio packet return them first
    1017        9906 :             st = s->streams[rm->audio_stream_num];
    1018        9906 :             res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
    1019        9906 :             if(res < 0)
    1020           0 :                 return res;
    1021        9906 :             flags = 0;
    1022             :         } else {
    1023        5827 :             if (rm->old_format) {
    1024             :                 RMStream *ast;
    1025             : 
    1026           3 :                 st = s->streams[0];
    1027           3 :                 ast = st->priv_data;
    1028           3 :                 timestamp = AV_NOPTS_VALUE;
    1029           3 :                 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
    1030           0 :                     ast->coded_framesize * ast->sub_packet_h / 2;
    1031           3 :                 flags = (seq++ == 1) ? 2 : 0;
    1032           3 :                 pos = avio_tell(s->pb);
    1033             :             } else {
    1034        5824 :                 len = rm_sync(s, &timestamp, &flags, &i, &pos);
    1035        5824 :                 if (len > 0)
    1036        5809 :                     st = s->streams[i];
    1037             :             }
    1038             : 
    1039        5827 :             if (avio_feof(s->pb))
    1040          18 :                 return AVERROR_EOF;
    1041        5809 :             if (len <= 0)
    1042           0 :                 return AVERROR(EIO);
    1043             : 
    1044        5809 :             res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
    1045             :                                       &seq, flags, timestamp);
    1046        5809 :             if (res < -1)
    1047           3 :                 return res;
    1048        5806 :             if((flags&2) && (seq&0x7F) == 1)
    1049         790 :                 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
    1050        5806 :             if (res)
    1051        2643 :                 continue;
    1052             :         }
    1053             : 
    1054       13069 :         if(  (st->discard >= AVDISCARD_NONKEY && !(flags&2))
    1055       11450 :            || st->discard >= AVDISCARD_ALL){
    1056        1641 :             av_packet_unref(pkt);
    1057             :         } else
    1058             :             break;
    1059             :     }
    1060             : 
    1061       11428 :     return 0;
    1062             : }
    1063             : 
    1064          24 : static int rm_read_close(AVFormatContext *s)
    1065             : {
    1066             :     int i;
    1067             : 
    1068          57 :     for (i=0;i<s->nb_streams;i++)
    1069          33 :         ff_rm_free_rmstream(s->streams[i]->priv_data);
    1070             : 
    1071          24 :     return 0;
    1072             : }
    1073             : 
    1074        6130 : static int rm_probe(AVProbeData *p)
    1075             : {
    1076             :     /* check file header */
    1077        6153 :     if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
    1078          69 :          p->buf[2] == 'M' && p->buf[3] == 'F' &&
    1079        6153 :          p->buf[4] == 0 && p->buf[5] == 0) ||
    1080        6119 :         (p->buf[0] == '.' && p->buf[1] == 'r' &&
    1081           2 :          p->buf[2] == 'a' && p->buf[3] == 0xfd))
    1082          24 :         return AVPROBE_SCORE_MAX;
    1083             :     else
    1084        6106 :         return 0;
    1085             : }
    1086             : 
    1087         248 : static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
    1088             :                                int64_t *ppos, int64_t pos_limit)
    1089             : {
    1090         248 :     RMDemuxContext *rm = s->priv_data;
    1091             :     int64_t pos, dts;
    1092             :     int stream_index2, flags, len, h;
    1093             : 
    1094         248 :     pos = *ppos;
    1095             : 
    1096         248 :     if(rm->old_format)
    1097           0 :         return AV_NOPTS_VALUE;
    1098             : 
    1099         248 :     if (avio_seek(s->pb, pos, SEEK_SET) < 0)
    1100           0 :         return AV_NOPTS_VALUE;
    1101             : 
    1102         248 :     rm->remaining_len=0;
    1103        1105 :     for(;;){
    1104        1353 :         int seq=1;
    1105             :         AVStream *st;
    1106             : 
    1107        1353 :         len = rm_sync(s, &dts, &flags, &stream_index2, &pos);
    1108        1353 :         if(len<0)
    1109         101 :             return AV_NOPTS_VALUE;
    1110             : 
    1111        1252 :         st = s->streams[stream_index2];
    1112        1252 :         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
    1113        1034 :             h= avio_r8(s->pb); len--;
    1114        1034 :             if(!(h & 0x40)){
    1115        1034 :                 seq = avio_r8(s->pb); len--;
    1116             :             }
    1117             :         }
    1118             : 
    1119        1252 :         if((flags&2) && (seq&0x7F) == 1){
    1120         355 :             av_log(s, AV_LOG_TRACE, "%d %d-%d %"PRId64" %d\n",
    1121             :                     flags, stream_index2, stream_index, dts, seq);
    1122         355 :             av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
    1123         355 :             if(stream_index2 == stream_index)
    1124         147 :                 break;
    1125             :         }
    1126             : 
    1127        1105 :         avio_skip(s->pb, len);
    1128             :     }
    1129         147 :     *ppos = pos;
    1130         147 :     return dts;
    1131             : }
    1132             : 
    1133          78 : static int rm_read_seek(AVFormatContext *s, int stream_index,
    1134             :                         int64_t pts, int flags)
    1135             : {
    1136          78 :     RMDemuxContext *rm = s->priv_data;
    1137             : 
    1138          78 :     if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
    1139           1 :         return -1;
    1140          77 :     rm->audio_pkt_cnt = 0;
    1141          77 :     return 0;
    1142             : }
    1143             : 
    1144             : 
    1145             : AVInputFormat ff_rm_demuxer = {
    1146             :     .name           = "rm",
    1147             :     .long_name      = NULL_IF_CONFIG_SMALL("RealMedia"),
    1148             :     .priv_data_size = sizeof(RMDemuxContext),
    1149             :     .read_probe     = rm_probe,
    1150             :     .read_header    = rm_read_header,
    1151             :     .read_packet    = rm_read_packet,
    1152             :     .read_close     = rm_read_close,
    1153             :     .read_timestamp = rm_read_dts,
    1154             :     .read_seek      = rm_read_seek,
    1155             : };
    1156             : 
    1157             : AVInputFormat ff_rdt_demuxer = {
    1158             :     .name           = "rdt",
    1159             :     .long_name      = NULL_IF_CONFIG_SMALL("RDT demuxer"),
    1160             :     .priv_data_size = sizeof(RMDemuxContext),
    1161             :     .read_close     = rm_read_close,
    1162             :     .flags          = AVFMT_NOFILE,
    1163             : };
    1164             : 
    1165        6130 : static int ivr_probe(AVProbeData *p)
    1166             : {
    1167       12260 :     if (memcmp(p->buf, ".R1M\x0\x1\x1", 7) &&
    1168        6130 :         memcmp(p->buf, ".REC", 4))
    1169        6130 :         return 0;
    1170             : 
    1171           0 :     return AVPROBE_SCORE_MAX;
    1172             : }
    1173             : 
    1174           0 : static int ivr_read_header(AVFormatContext *s)
    1175             : {
    1176             :     unsigned tag, type, len, tlen, value;
    1177           0 :     int i, j, n, count, nb_streams = 0, ret;
    1178             :     uint8_t key[256], val[256];
    1179           0 :     AVIOContext *pb = s->pb;
    1180             :     AVStream *st;
    1181             :     int64_t pos, offset, temp;
    1182             : 
    1183           0 :     pos = avio_tell(pb);
    1184           0 :     tag = avio_rl32(pb);
    1185           0 :     if (tag == MKTAG('.','R','1','M')) {
    1186           0 :         if (avio_rb16(pb) != 1)
    1187           0 :             return AVERROR_INVALIDDATA;
    1188           0 :         if (avio_r8(pb) != 1)
    1189           0 :             return AVERROR_INVALIDDATA;
    1190           0 :         len = avio_rb32(pb);
    1191           0 :         avio_skip(pb, len);
    1192           0 :         avio_skip(pb, 5);
    1193           0 :         temp = avio_rb64(pb);
    1194           0 :         while (!avio_feof(pb) && temp) {
    1195           0 :             offset = temp;
    1196           0 :             temp = avio_rb64(pb);
    1197             :         }
    1198           0 :         avio_skip(pb, offset - avio_tell(pb));
    1199           0 :         if (avio_r8(pb) != 1)
    1200           0 :             return AVERROR_INVALIDDATA;
    1201           0 :         len = avio_rb32(pb);
    1202           0 :         avio_skip(pb, len);
    1203           0 :         if (avio_r8(pb) != 2)
    1204           0 :             return AVERROR_INVALIDDATA;
    1205           0 :         avio_skip(pb, 16);
    1206           0 :         pos = avio_tell(pb);
    1207           0 :         tag = avio_rl32(pb);
    1208             :     }
    1209             : 
    1210           0 :     if (tag != MKTAG('.','R','E','C'))
    1211           0 :         return AVERROR_INVALIDDATA;
    1212             : 
    1213           0 :     if (avio_r8(pb) != 0)
    1214           0 :         return AVERROR_INVALIDDATA;
    1215           0 :     count = avio_rb32(pb);
    1216           0 :     for (i = 0; i < count; i++) {
    1217           0 :         if (avio_feof(pb))
    1218           0 :             return AVERROR_INVALIDDATA;
    1219             : 
    1220           0 :         type = avio_r8(pb);
    1221           0 :         tlen = avio_rb32(pb);
    1222           0 :         avio_get_str(pb, tlen, key, sizeof(key));
    1223           0 :         len = avio_rb32(pb);
    1224           0 :         if (type == 5) {
    1225           0 :             avio_get_str(pb, len, val, sizeof(val));
    1226           0 :             av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
    1227           0 :         } else if (type == 4) {
    1228           0 :             av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
    1229           0 :             for (j = 0; j < len; j++) {
    1230           0 :                 if (avio_feof(pb))
    1231           0 :                     return AVERROR_INVALIDDATA;
    1232           0 :                 av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
    1233             :             }
    1234           0 :             av_log(s, AV_LOG_DEBUG, "'\n");
    1235           0 :         } else if (len == 4 && type == 3 && !strncmp(key, "StreamCount", tlen)) {
    1236           0 :             nb_streams = value = avio_rb32(pb);
    1237           0 :         } else if (len == 4 && type == 3) {
    1238           0 :             value = avio_rb32(pb);
    1239           0 :             av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
    1240             :         } else {
    1241           0 :             av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
    1242           0 :             avio_skip(pb, len);
    1243             :         }
    1244             :     }
    1245             : 
    1246           0 :     for (n = 0; n < nb_streams; n++) {
    1247           0 :         st = avformat_new_stream(s, NULL);
    1248           0 :         if (!st)
    1249           0 :             return AVERROR(ENOMEM);
    1250           0 :         st->priv_data = ff_rm_alloc_rmstream();
    1251           0 :         if (!st->priv_data)
    1252           0 :             return AVERROR(ENOMEM);
    1253             : 
    1254           0 :         if (avio_r8(pb) != 1)
    1255           0 :             return AVERROR_INVALIDDATA;
    1256             : 
    1257           0 :         count = avio_rb32(pb);
    1258           0 :         for (i = 0; i < count; i++) {
    1259           0 :             if (avio_feof(pb))
    1260           0 :                 return AVERROR_INVALIDDATA;
    1261             : 
    1262           0 :             type = avio_r8(pb);
    1263           0 :             tlen  = avio_rb32(pb);
    1264           0 :             avio_get_str(pb, tlen, key, sizeof(key));
    1265           0 :             len  = avio_rb32(pb);
    1266           0 :             if (type == 5) {
    1267           0 :                 avio_get_str(pb, len, val, sizeof(val));
    1268           0 :                 av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
    1269           0 :             } else if (type == 4 && !strncmp(key, "OpaqueData", tlen)) {
    1270           0 :                 ret = ffio_ensure_seekback(pb, 4);
    1271           0 :                 if (ret < 0)
    1272           0 :                     return ret;
    1273           0 :                 if (avio_rb32(pb) == MKBETAG('M', 'L', 'T', 'I')) {
    1274           0 :                     ret = rm_read_multi(s, pb, st, NULL);
    1275             :                 } else {
    1276           0 :                     avio_seek(pb, -4, SEEK_CUR);
    1277           0 :                     ret = ff_rm_read_mdpr_codecdata(s, pb, st, st->priv_data, len, NULL);
    1278             :                 }
    1279             : 
    1280           0 :                 if (ret < 0)
    1281           0 :                     return ret;
    1282           0 :             } else if (type == 4) {
    1283             :                 int j;
    1284             : 
    1285           0 :                 av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
    1286           0 :                 for (j = 0; j < len; j++)
    1287           0 :                     av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
    1288           0 :                 av_log(s, AV_LOG_DEBUG, "'\n");
    1289           0 :             } else if (len == 4 && type == 3 && !strncmp(key, "Duration", tlen)) {
    1290           0 :                 st->duration = avio_rb32(pb);
    1291           0 :             } else if (len == 4 && type == 3) {
    1292           0 :                 value = avio_rb32(pb);
    1293           0 :                 av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
    1294             :             } else {
    1295           0 :                 av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
    1296           0 :                 avio_skip(pb, len);
    1297             :             }
    1298             :         }
    1299             :     }
    1300             : 
    1301           0 :     if (avio_r8(pb) != 6)
    1302           0 :         return AVERROR_INVALIDDATA;
    1303           0 :     avio_skip(pb, 12);
    1304           0 :     avio_skip(pb, avio_rb64(pb) + pos - avio_tell(s->pb));
    1305           0 :     if (avio_r8(pb) != 8)
    1306           0 :         return AVERROR_INVALIDDATA;
    1307           0 :     avio_skip(pb, 8);
    1308             : 
    1309           0 :     return 0;
    1310             : }
    1311             : 
    1312           0 : static int ivr_read_packet(AVFormatContext *s, AVPacket *pkt)
    1313             : {
    1314           0 :     RMDemuxContext *rm = s->priv_data;
    1315           0 :     int ret = AVERROR_EOF, opcode;
    1316           0 :     AVIOContext *pb = s->pb;
    1317             :     unsigned size, index;
    1318             :     int64_t pos, pts;
    1319             : 
    1320           0 :     if (avio_feof(pb) || rm->data_end)
    1321           0 :         return AVERROR_EOF;
    1322             : 
    1323           0 :     pos = avio_tell(pb);
    1324             : 
    1325             :     for (;;) {
    1326           0 :         if (rm->audio_pkt_cnt) {
    1327             :             // If there are queued audio packet return them first
    1328             :             AVStream *st;
    1329             : 
    1330           0 :             st = s->streams[rm->audio_stream_num];
    1331           0 :             ret = ff_rm_retrieve_cache(s, pb, st, st->priv_data, pkt);
    1332           0 :             if (ret < 0) {
    1333           0 :                 return ret;
    1334             :             }
    1335             :         } else {
    1336           0 :             if (rm->remaining_len) {
    1337           0 :                 avio_skip(pb, rm->remaining_len);
    1338           0 :                 rm->remaining_len = 0;
    1339             :             }
    1340             : 
    1341           0 :             if (avio_feof(pb))
    1342           0 :                 return AVERROR_EOF;
    1343             : 
    1344           0 :             opcode = avio_r8(pb);
    1345           0 :             if (opcode == 2) {
    1346             :                 AVStream *st;
    1347           0 :                 int seq = 1;
    1348             : 
    1349           0 :                 pts = avio_rb32(pb);
    1350           0 :                 index = avio_rb16(pb);
    1351           0 :                 if (index >= s->nb_streams)
    1352           0 :                     return AVERROR_INVALIDDATA;
    1353             : 
    1354           0 :                 avio_skip(pb, 4);
    1355           0 :                 size = avio_rb32(pb);
    1356           0 :                 avio_skip(pb, 4);
    1357             : 
    1358           0 :                 if (size < 1 || size > INT_MAX/4) {
    1359           0 :                     av_log(s, AV_LOG_ERROR, "size %u is invalid\n", size);
    1360           0 :                     return AVERROR_INVALIDDATA;
    1361             :                 }
    1362             : 
    1363           0 :                 st = s->streams[index];
    1364           0 :                 ret = ff_rm_parse_packet(s, pb, st, st->priv_data, size, pkt,
    1365             :                                          &seq, 0, pts);
    1366           0 :                 if (ret < -1) {
    1367           0 :                     return ret;
    1368           0 :                 } else if (ret) {
    1369           0 :                     continue;
    1370             :                 }
    1371             : 
    1372           0 :                 pkt->pos = pos;
    1373           0 :                 pkt->pts = pts;
    1374           0 :                 pkt->stream_index = index;
    1375           0 :             } else if (opcode == 7) {
    1376           0 :                 pos = avio_rb64(pb);
    1377           0 :                 if (!pos) {
    1378           0 :                     rm->data_end = 1;
    1379           0 :                     return AVERROR_EOF;
    1380             :                 }
    1381             :             } else {
    1382           0 :                 av_log(s, AV_LOG_ERROR, "Unsupported opcode=%d at %"PRIX64"\n", opcode, avio_tell(pb) - 1);
    1383           0 :                 return AVERROR(EIO);
    1384             :             }
    1385             :         }
    1386             : 
    1387           0 :         break;
    1388             :     }
    1389             : 
    1390           0 :     return ret;
    1391             : }
    1392             : 
    1393             : AVInputFormat ff_ivr_demuxer = {
    1394             :     .name           = "ivr",
    1395             :     .long_name      = NULL_IF_CONFIG_SMALL("IVR (Internet Video Recording)"),
    1396             :     .priv_data_size = sizeof(RMDemuxContext),
    1397             :     .read_probe     = ivr_probe,
    1398             :     .read_header    = ivr_read_header,
    1399             :     .read_packet    = ivr_read_packet,
    1400             :     .read_close     = rm_read_close,
    1401             :     .extensions     = "ivr",
    1402             : };

Generated by: LCOV version 1.13