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

Generated by: LCOV version 1.13