00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/dict.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include <stdlib.h>
00027
00028 #define RPL_SIGNATURE "ARMovie\x0A"
00029 #define RPL_SIGNATURE_SIZE 8
00030
00032 #define RPL_LINE_LENGTH 256
00033
00034 static int rpl_probe(AVProbeData *p)
00035 {
00036 if (memcmp(p->buf, RPL_SIGNATURE, RPL_SIGNATURE_SIZE))
00037 return 0;
00038
00039 return AVPROBE_SCORE_MAX;
00040 }
00041
00042 typedef struct RPLContext {
00043
00044 int32_t frames_per_chunk;
00045
00046
00047 uint32_t chunk_number;
00048 uint32_t chunk_part;
00049 uint32_t frame_in_part;
00050 } RPLContext;
00051
00052 static int read_line(AVIOContext * pb, char* line, int bufsize)
00053 {
00054 int i;
00055 for (i = 0; i < bufsize - 1; i++) {
00056 int b = avio_r8(pb);
00057 if (b == 0)
00058 break;
00059 if (b == '\n') {
00060 line[i] = '\0';
00061 return url_feof(pb) ? -1 : 0;
00062 }
00063 line[i] = b;
00064 }
00065 line[i] = '\0';
00066 return -1;
00067 }
00068
00069 static int32_t read_int(const char* line, const char** endptr, int* error)
00070 {
00071 unsigned long result = 0;
00072 for (; *line>='0' && *line<='9'; line++) {
00073 if (result > (0x7FFFFFFF - 9) / 10)
00074 *error = -1;
00075 result = 10 * result + *line - '0';
00076 }
00077 *endptr = line;
00078 return result;
00079 }
00080
00081 static int32_t read_line_and_int(AVIOContext * pb, int* error)
00082 {
00083 char line[RPL_LINE_LENGTH];
00084 const char *endptr;
00085 *error |= read_line(pb, line, sizeof(line));
00086 return read_int(line, &endptr, error);
00087 }
00088
00093 static AVRational read_fps(const char* line, int* error)
00094 {
00095 int64_t num, den = 1;
00096 AVRational result;
00097 num = read_int(line, &line, error);
00098 if (*line == '.')
00099 line++;
00100 for (; *line>='0' && *line<='9'; line++) {
00101
00102 if (num > (INT64_MAX - 9) / 10 || den > INT64_MAX / 10)
00103 break;
00104 num = 10 * num + *line - '0';
00105 den *= 10;
00106 }
00107 if (!num)
00108 *error = -1;
00109 av_reduce(&result.num, &result.den, num, den, 0x7FFFFFFF);
00110 return result;
00111 }
00112
00113 static int rpl_read_header(AVFormatContext *s)
00114 {
00115 AVIOContext *pb = s->pb;
00116 RPLContext *rpl = s->priv_data;
00117 AVStream *vst = NULL, *ast = NULL;
00118 int total_audio_size;
00119 int error = 0;
00120
00121 uint32_t i;
00122
00123 int32_t audio_format, chunk_catalog_offset, number_of_chunks;
00124 AVRational fps;
00125
00126 char line[RPL_LINE_LENGTH];
00127
00128
00129
00130
00131
00132
00133
00134 error |= read_line(pb, line, sizeof(line));
00135 error |= read_line(pb, line, sizeof(line));
00136 av_dict_set(&s->metadata, "title" , line, 0);
00137 error |= read_line(pb, line, sizeof(line));
00138 av_dict_set(&s->metadata, "copyright", line, 0);
00139 error |= read_line(pb, line, sizeof(line));
00140 av_dict_set(&s->metadata, "author" , line, 0);
00141
00142
00143 vst = avformat_new_stream(s, NULL);
00144 if (!vst)
00145 return AVERROR(ENOMEM);
00146 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00147 vst->codec->codec_tag = read_line_and_int(pb, &error);
00148 vst->codec->width = read_line_and_int(pb, &error);
00149 vst->codec->height = read_line_and_int(pb, &error);
00150 vst->codec->bits_per_coded_sample = read_line_and_int(pb, &error);
00151 error |= read_line(pb, line, sizeof(line));
00152 fps = read_fps(line, &error);
00153 avpriv_set_pts_info(vst, 32, fps.den, fps.num);
00154
00155
00156 switch (vst->codec->codec_tag) {
00157 #if 0
00158 case 122:
00159 vst->codec->codec_id = AV_CODEC_ID_ESCAPE122;
00160 break;
00161 #endif
00162 case 124:
00163 vst->codec->codec_id = AV_CODEC_ID_ESCAPE124;
00164
00165 vst->codec->bits_per_coded_sample = 16;
00166 break;
00167 case 130:
00168 vst->codec->codec_id = AV_CODEC_ID_ESCAPE130;
00169 break;
00170 default:
00171 av_log(s, AV_LOG_WARNING,
00172 "RPL video format %i not supported yet!\n",
00173 vst->codec->codec_tag);
00174 vst->codec->codec_id = AV_CODEC_ID_NONE;
00175 }
00176
00177
00178
00179
00180
00181 audio_format = read_line_and_int(pb, &error);
00182 if (audio_format) {
00183 ast = avformat_new_stream(s, NULL);
00184 if (!ast)
00185 return AVERROR(ENOMEM);
00186 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00187 ast->codec->codec_tag = audio_format;
00188 ast->codec->sample_rate = read_line_and_int(pb, &error);
00189 ast->codec->channels = read_line_and_int(pb, &error);
00190 ast->codec->bits_per_coded_sample = read_line_and_int(pb, &error);
00191
00192
00193 if (ast->codec->bits_per_coded_sample == 0)
00194 ast->codec->bits_per_coded_sample = 4;
00195
00196 ast->codec->bit_rate = ast->codec->sample_rate *
00197 ast->codec->bits_per_coded_sample *
00198 ast->codec->channels;
00199
00200 ast->codec->codec_id = AV_CODEC_ID_NONE;
00201 switch (audio_format) {
00202 case 1:
00203 if (ast->codec->bits_per_coded_sample == 16) {
00204
00205 ast->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
00206 break;
00207 }
00208
00209
00210 break;
00211 case 101:
00212 if (ast->codec->bits_per_coded_sample == 8) {
00213
00214
00215 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
00216 break;
00217 } else if (ast->codec->bits_per_coded_sample == 4) {
00218 ast->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
00219 break;
00220 }
00221 break;
00222 }
00223 if (ast->codec->codec_id == AV_CODEC_ID_NONE) {
00224 av_log(s, AV_LOG_WARNING,
00225 "RPL audio format %i not supported yet!\n",
00226 audio_format);
00227 }
00228 avpriv_set_pts_info(ast, 32, 1, ast->codec->bit_rate);
00229 } else {
00230 for (i = 0; i < 3; i++)
00231 error |= read_line(pb, line, sizeof(line));
00232 }
00233
00234 rpl->frames_per_chunk = read_line_and_int(pb, &error);
00235 if (rpl->frames_per_chunk > 1 && vst->codec->codec_tag != 124)
00236 av_log(s, AV_LOG_WARNING,
00237 "Don't know how to split frames for video format %i. "
00238 "Video stream will be broken!\n", vst->codec->codec_tag);
00239
00240 number_of_chunks = read_line_and_int(pb, &error);
00241
00242 number_of_chunks++;
00243
00244 error |= read_line(pb, line, sizeof(line));
00245 error |= read_line(pb, line, sizeof(line));
00246 chunk_catalog_offset =
00247 read_line_and_int(pb, &error);
00248 error |= read_line(pb, line, sizeof(line));
00249 error |= read_line(pb, line, sizeof(line));
00250 error |= read_line(pb, line, sizeof(line));
00251
00252
00253 avio_seek(pb, chunk_catalog_offset, SEEK_SET);
00254 total_audio_size = 0;
00255 for (i = 0; !error && i < number_of_chunks; i++) {
00256 int64_t offset, video_size, audio_size;
00257 error |= read_line(pb, line, sizeof(line));
00258 if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
00259 &offset, &video_size, &audio_size))
00260 error = -1;
00261 av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
00262 video_size, rpl->frames_per_chunk, 0);
00263 if (ast)
00264 av_add_index_entry(ast, offset + video_size, total_audio_size,
00265 audio_size, audio_size * 8, 0);
00266 total_audio_size += audio_size * 8;
00267 }
00268
00269 if (error) return AVERROR(EIO);
00270
00271 return 0;
00272 }
00273
00274 static int rpl_read_packet(AVFormatContext *s, AVPacket *pkt)
00275 {
00276 RPLContext *rpl = s->priv_data;
00277 AVIOContext *pb = s->pb;
00278 AVStream* stream;
00279 AVIndexEntry* index_entry;
00280 uint32_t ret;
00281
00282 if (rpl->chunk_part == s->nb_streams) {
00283 rpl->chunk_number++;
00284 rpl->chunk_part = 0;
00285 }
00286
00287 stream = s->streams[rpl->chunk_part];
00288
00289 if (rpl->chunk_number >= stream->nb_index_entries)
00290 return AVERROR_EOF;
00291
00292 index_entry = &stream->index_entries[rpl->chunk_number];
00293
00294 if (rpl->frame_in_part == 0)
00295 if (avio_seek(pb, index_entry->pos, SEEK_SET) < 0)
00296 return AVERROR(EIO);
00297
00298 if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
00299 stream->codec->codec_tag == 124) {
00300
00301
00302 uint32_t frame_size;
00303
00304 avio_skip(pb, 4);
00305 frame_size = avio_rl32(pb);
00306 if (avio_seek(pb, -8, SEEK_CUR) < 0)
00307 return AVERROR(EIO);
00308
00309 ret = av_get_packet(pb, pkt, frame_size);
00310 if (ret != frame_size) {
00311 av_free_packet(pkt);
00312 return AVERROR(EIO);
00313 }
00314 pkt->duration = 1;
00315 pkt->pts = index_entry->timestamp + rpl->frame_in_part;
00316 pkt->stream_index = rpl->chunk_part;
00317
00318 rpl->frame_in_part++;
00319 if (rpl->frame_in_part == rpl->frames_per_chunk) {
00320 rpl->frame_in_part = 0;
00321 rpl->chunk_part++;
00322 }
00323 } else {
00324 ret = av_get_packet(pb, pkt, index_entry->size);
00325 if (ret != index_entry->size) {
00326 av_free_packet(pkt);
00327 return AVERROR(EIO);
00328 }
00329
00330 if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00331
00332
00333 pkt->duration = rpl->frames_per_chunk;
00334 } else {
00335
00336
00337 pkt->duration = ret * 8;
00338 }
00339 pkt->pts = index_entry->timestamp;
00340 pkt->stream_index = rpl->chunk_part;
00341 rpl->chunk_part++;
00342 }
00343
00344
00345
00346 if (rpl->chunk_number == 0 && rpl->frame_in_part == 0)
00347 pkt->flags |= AV_PKT_FLAG_KEY;
00348
00349 return ret;
00350 }
00351
00352 AVInputFormat ff_rpl_demuxer = {
00353 .name = "rpl",
00354 .long_name = NULL_IF_CONFIG_SMALL("RPL / ARMovie"),
00355 .priv_data_size = sizeof(RPLContext),
00356 .read_probe = rpl_probe,
00357 .read_header = rpl_read_header,
00358 .read_packet = rpl_read_packet,
00359 };