static int libschroedinger_encode_frame(AVCodecContext *avccontext, AVPacket *pkt, const AVFrame *frame, int *got_packet) { int enc_size = 0; SchroEncoderParams *p_schro_params = avccontext->priv_data; SchroEncoder *encoder = p_schro_params->encoder; struct FFSchroEncodedFrame *p_frame_output = NULL; int go = 1; SchroBuffer *enc_buf; int presentation_frame; int parse_code; int last_frame_in_sequence = 0; int pkt_size, ret; if (!frame) { /* Push end of sequence if not already signalled. */ if (!p_schro_params->eos_signalled) { schro_encoder_end_of_stream(encoder); p_schro_params->eos_signalled = 1; } } else { /* Allocate frame data to schro input buffer. */ SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext, frame); /* Load next frame. */ schro_encoder_push_frame(encoder, in_frame); } if (p_schro_params->eos_pulled) go = 0; /* Now check to see if we have any output from the encoder. */ while (go) { SchroStateEnum state; state = schro_encoder_wait(encoder); switch (state) { case SCHRO_STATE_HAVE_BUFFER: case SCHRO_STATE_END_OF_STREAM: enc_buf = schro_encoder_pull(encoder, &presentation_frame); assert(enc_buf->length > 0); assert(enc_buf->length <= buf_size); parse_code = enc_buf->data[4]; /* All non-frame data is prepended to actual frame data to * be able to set the pts correctly. So we don't write data * to the frame output queue until we actually have a frame */ p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf, p_schro_params->enc_buf_size + enc_buf->length); memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size, enc_buf->data, enc_buf->length); p_schro_params->enc_buf_size += enc_buf->length; if (state == SCHRO_STATE_END_OF_STREAM) { p_schro_params->eos_pulled = 1; go = 0; } if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) { schro_buffer_unref(enc_buf); break; } /* Create output frame. */ p_frame_output = av_mallocz(sizeof(FFSchroEncodedFrame)); /* Set output data. */ p_frame_output->size = p_schro_params->enc_buf_size; p_frame_output->p_encbuf = p_schro_params->enc_buf; if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) && SCHRO_PARSE_CODE_IS_REFERENCE(parse_code)) p_frame_output->key_frame = 1; /* Parse the coded frame number from the bitstream. Bytes 14 * through 17 represesent the frame number. */ p_frame_output->frame_num = AV_RB32(enc_buf->data + 13); ff_schro_queue_push_back(&p_schro_params->enc_frame_queue, p_frame_output); p_schro_params->enc_buf_size = 0; p_schro_params->enc_buf = NULL; schro_buffer_unref(enc_buf); break; case SCHRO_STATE_NEED_FRAME: go = 0; break; case SCHRO_STATE_AGAIN: break; default: av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n"); return -1; } } /* Copy 'next' frame in queue. */ if (p_schro_params->enc_frame_queue.size == 1 && p_schro_params->eos_pulled) last_frame_in_sequence = 1; p_frame_output = ff_schro_queue_pop(&p_schro_params->enc_frame_queue); if (!p_frame_output) return 0; pkt_size = p_frame_output->size; if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) pkt_size += p_schro_params->enc_buf_size; if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) { av_log(avccontext, AV_LOG_ERROR, "Error getting output packet of size %d.\n", pkt_size); goto error; } memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size); avccontext->coded_frame->key_frame = p_frame_output->key_frame; /* Use the frame number of the encoded frame as the pts. It is OK to * do so since Dirac is a constant frame rate codec. It expects input * to be of constant frame rate. */ pkt->pts = avccontext->coded_frame->pts = p_frame_output->frame_num; pkt->dts = p_schro_params->dts++; enc_size = p_frame_output->size; /* Append the end of sequence information to the last frame in the * sequence. */ if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) { memcpy(pkt->data + enc_size, p_schro_params->enc_buf, p_schro_params->enc_buf_size); enc_size += p_schro_params->enc_buf_size; av_freep(&p_schro_params->enc_buf); p_schro_params->enc_buf_size = 0; } if (p_frame_output->key_frame) pkt->flags |= AV_PKT_FLAG_KEY; *got_packet = 1; error: /* free frame */ libschroedinger_free_frame(p_frame_output); return ret; }
static void handle_packet (unsigned char *data, int size) { SchroUnpack unpack; const char *parse_code; int next; int prev; if (memcmp (data, "BBCD", 4) != 0) { printf("non-Dirac packet\n"); dump_hex (data, MIN(size, 100), " "); return; } switch (data[4]) { case SCHRO_PARSE_CODE_SEQUENCE_HEADER: parse_code = "access unit header"; break; case SCHRO_PARSE_CODE_AUXILIARY_DATA: parse_code = "auxiliary data"; break; case SCHRO_PARSE_CODE_INTRA_REF: parse_code = "intra ref"; break; case SCHRO_PARSE_CODE_INTRA_NON_REF: parse_code = "intra non-ref"; break; case SCHRO_PARSE_CODE_INTER_REF_1: parse_code = "inter ref 1"; break; case SCHRO_PARSE_CODE_INTER_REF_2: parse_code = "inter ref 2"; break; case SCHRO_PARSE_CODE_INTER_NON_REF_1: parse_code = "inter non-ref 1"; break; case SCHRO_PARSE_CODE_INTER_NON_REF_2: parse_code = "inter non-ref 2"; break; case SCHRO_PARSE_CODE_END_OF_SEQUENCE: parse_code = "end of sequence"; break; case SCHRO_PARSE_CODE_LD_INTRA_REF: parse_code = "low-delay intra ref"; break; case SCHRO_PARSE_CODE_LD_INTRA_NON_REF: parse_code = "low-delay intra non-ref"; break; case SCHRO_PARSE_CODE_INTRA_REF_NOARITH: parse_code = "intra ref noarith"; break; case SCHRO_PARSE_CODE_INTRA_NON_REF_NOARITH: parse_code = "intra non-ref noarith"; break; case SCHRO_PARSE_CODE_INTER_REF_1_NOARITH: parse_code = "inter ref 1 noarith"; break; case SCHRO_PARSE_CODE_INTER_REF_2_NOARITH: parse_code = "inter ref 2 noarith"; break; case SCHRO_PARSE_CODE_INTER_NON_REF_1_NOARITH: parse_code = "inter non-ref 1 noarith"; break; case SCHRO_PARSE_CODE_INTER_NON_REF_2_NOARITH: parse_code = "inter non-ref 2 noarith"; break; default: parse_code = "unknown"; break; } schro_unpack_init_with_data (&unpack, data + 5, size - 5, 1); next = schro_unpack_decode_bits (&unpack, 32); prev = schro_unpack_decode_bits (&unpack, 32); if (data[4] == SCHRO_PARSE_CODE_SEQUENCE_HEADER) { printf("AU\n"); printf("pictur: "); printf(" "); printf(" "); printf(" ref1 "); printf(" ref2 "); printf("retire "); printf(" size \n"); } else if (SCHRO_PARSE_CODE_IS_PICTURE(data[4])) { int num_refs = SCHRO_PARSE_CODE_NUM_REFS(data[4]); int pic_num; char ref_chars[3] = { 'I', 'P', 'B' }; schro_unpack_byte_sync(&unpack); pic_num = schro_unpack_decode_bits(&unpack, 32); printf("%6d: ", pic_num); if (SCHRO_PARSE_CODE_IS_REFERENCE(data[4])) { printf("ref "); } else { printf(" "); } printf("%c ", ref_chars[num_refs]); if (num_refs > 0) { printf("%6d ", pic_num + schro_unpack_decode_sint(&unpack)); } else { printf(" "); } if (num_refs > 1) { printf("%6d ", pic_num + schro_unpack_decode_sint(&unpack)); } else { printf(" "); } if (SCHRO_PARSE_CODE_IS_REFERENCE(data[4])) { int r = schro_unpack_decode_sint(&unpack); if (r == 0) { printf(" none "); } else { printf("%6d ", pic_num + r); } } else { printf(" "); } printf(" %8d\n", next); } else if (data[4] == SCHRO_PARSE_CODE_AUXILIARY_DATA) { } schro_unpack_byte_sync (&unpack); }