3 * Copyright (c) 2009 Tobias Bindhammer
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavcodec/avcodec.h"
23 #include "libavcodec/a64enc.h"
24 #include "libavcodec/bytestream.h"
27 typedef struct A64MuxerContext {
33 static int a64_write_header(struct AVFormatContext *s)
35 AVCodecContext *avctx = s->streams[0]->codec;
36 A64MuxerContext *c = s->priv_data;
41 0x00, //charset_lifetime (multi only)
45 switch (avctx->codec->id) {
46 case CODEC_ID_A64_MULTI:
48 header[3] = AV_RB32(avctx->extradata+0);
51 case CODEC_ID_A64_MULTI5:
53 header[3] = AV_RB32(avctx->extradata+0);
57 return AVERROR(EINVAL);
60 put_buffer(s->pb, header, 2);
62 c->prev_frame_count = 0;
66 static int a64_write_packet(struct AVFormatContext *s, AVPacket *pkt)
68 AVCodecContext *avctx = s->streams[0]->codec;
69 A64MuxerContext *c = s->priv_data;
78 /* fetch values from extradata */
79 switch (avctx->codec->id) {
80 case CODEC_ID_A64_MULTI:
81 case CODEC_ID_A64_MULTI5:
83 /* Write interleaved, means we insert chunks of the future charset before each current frame.
84 * Reason: if we load 1 charset + corresponding frames in one block on c64, we need to store
85 * them first and then display frame by frame to keep in sync. Thus we would read and write
86 * the data for colram from/to ram first and waste too much time. If we interleave and send the
87 * charset beforehand, we assemble a new charset chunk by chunk, write current screen data to
88 * screen-ram to be displayed and decode the colram directly to colram-location $d800 during
89 * the overscan, while reading directly from source.
90 * This is the only way so far, to achieve 25fps on c64 */
91 if(avctx->extradata) {
92 /* fetch values from extradata */
93 lifetime = AV_RB32(avctx->extradata + 0);
94 frame_count = AV_RB32(avctx->extradata + 4);
95 charset_size = AV_RB32(avctx->extradata + 8);
96 frame_size = AV_RB32(avctx->extradata + 12);
98 /* TODO: sanity checks? */
100 av_log(avctx, AV_LOG_ERROR, "extradata not set\n");
101 return AVERROR(EINVAL);
104 ch_chunksize=charset_size/lifetime;
105 /* TODO: check if charset/size is % lifetime, but maybe check in codec */
107 if(pkt->data) num_frames = lifetime;
108 else num_frames = c->prev_frame_count;
110 for(i = 0; i < num_frames; i++) {
112 /* if available, put newest charset chunk into buffer */
113 put_buffer(s->pb, pkt->data + ch_chunksize * i, ch_chunksize);
115 /* a bit ugly, but is there an alternative to put many zeros? */
116 for(j = 0; j < ch_chunksize; j++) put_byte(s->pb, 0);
119 if(c->prev_pkt.data) {
120 /* put frame (screen + colram) from last packet into buffer */
121 put_buffer(s->pb, c->prev_pkt.data + charset_size + frame_size * i, frame_size);
123 /* a bit ugly, but is there an alternative to put many zeros? */
124 for(j = 0; j < frame_size; j++) put_byte(s->pb, 0);
128 /* backup current packet for next turn */
130 /* no backup packet yet? create one! */
131 if(!c->prev_pkt.data) av_new_packet(&c->prev_pkt, pkt->size);
132 /* we have a packet and data is big enough, reuse it */
133 if(c->prev_pkt.data && c->prev_pkt.size >= pkt->size) {
134 memcpy(c->prev_pkt.data, pkt->data, pkt->size);
135 c->prev_pkt.size = pkt->size;
137 av_log(avctx, AV_LOG_ERROR, "Too less memory for prev_pkt.\n");
138 return AVERROR(ENOMEM);
142 c->prev_frame_count = frame_count;
146 /* Write things as is. Nice for self-contained frames from non-multicolor modes or if played
147 * directly from ram and not from a streaming device (rrnet/mmc) */
148 if(pkt) put_buffer(s->pb, pkt->data, pkt->size);
152 put_flush_packet(s->pb);
156 static int a64_write_trailer(struct AVFormatContext *s)
158 A64MuxerContext *c = s->priv_data;
160 /* need to flush last packet? */
161 if(c->interleaved) a64_write_packet(s, &pkt);
162 /* discard backed up packet */
163 if(c->prev_pkt.data) av_destruct_packet(&c->prev_pkt);
167 AVOutputFormat a64_muxer = {
169 .long_name = NULL_IF_CONFIG_SMALL("a64 - video for Commodore 64"),
171 .extensions = "a64, A64",
172 .priv_data_size = sizeof (A64Context),
173 .video_codec = CODEC_ID_A64_MULTI,