comparison mxfenc.c @ 4504:c0803ee07a93 libavformat

merge mxf_write_d10_packet into mxf_write_packet
author bcoudurier
date Mon, 16 Feb 2009 10:44:38 +0000
parents cc4b6d42dcd5
children da04428a94db
comparison
equal deleted inserted replaced
4503:c3d058b6ddde 4504:c0803ee07a93
1629 for (; i < 8; i++) 1629 for (; i < 8; i++)
1630 put_le32(pb, 0); 1630 put_le32(pb, 0);
1631 } 1631 }
1632 } 1632 }
1633 1633
1634 static int mxf_write_d10_packet(AVFormatContext *s, AVPacket *pkt) 1634 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1635 { 1635 {
1636 MXFContext *mxf = s->priv_data; 1636 MXFContext *mxf = s->priv_data;
1637 ByteIOContext *pb = s->pb; 1637 ByteIOContext *pb = s->pb;
1638 AVStream *st = s->streams[pkt->stream_index]; 1638 AVStream *st = s->streams[pkt->stream_index];
1639 MXFStreamContext *sc = st->priv_data; 1639 MXFStreamContext *sc = st->priv_data;
1640 int flags = 0; 1640 int flags = 0;
1641 1641
1642 if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) { 1642 if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1643 if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
1644 av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
1645 return -1;
1646 }
1647 }
1648
1649 if (!mxf->header_written) {
1650 mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
1651 mxf->header_written = 1;
1652 mxf_write_klv_fill(s);
1653 mxf_write_index_table_segment(s);
1654 }
1655
1656 if (st->index == 0) {
1657 mxf_write_klv_fill(s);
1658 mxf_write_system_item(s);
1659
1660 mxf->edit_units_count++;
1661 }
1662
1663 mxf_write_klv_fill(s);
1664 put_buffer(pb, sc->track_essence_element_key, 16); // write key
1665 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1666 mxf_write_d10_video_packet(s, st, pkt);
1667 else
1668 mxf_write_d10_audio_packet(s, st, pkt);
1669
1670 put_flush_packet(pb);
1671
1672 return 0;
1673 }
1674
1675 static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
1676 {
1677 MXFContext *mxf = s->priv_data;
1678 ByteIOContext *pb = s->pb;
1679 AVStream *st = s->streams[pkt->stream_index];
1680 MXFStreamContext *sc = st->priv_data;
1681 int flags = 0;
1682
1683 if (!(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
1684 mxf->index_entries = av_realloc(mxf->index_entries, 1643 mxf->index_entries = av_realloc(mxf->index_entries,
1685 (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries)); 1644 (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries));
1686 if (!mxf->index_entries) { 1645 if (!mxf->index_entries) {
1687 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n"); 1646 av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
1688 return -1; 1647 return -1;
1695 return -1; 1654 return -1;
1696 } 1655 }
1697 } 1656 }
1698 1657
1699 if (!mxf->header_written) { 1658 if (!mxf->header_written) {
1700 mxf_write_partition(s, 0, 0, header_open_partition_key, 1); 1659 if (mxf->edit_unit_byte_count) {
1660 mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
1661 mxf_write_klv_fill(s);
1662 mxf_write_index_table_segment(s);
1663 } else {
1664 mxf_write_partition(s, 0, 0, header_open_partition_key, 1);
1665 }
1701 mxf->header_written = 1; 1666 mxf->header_written = 1;
1702 } 1667 }
1703 1668
1704 if (st->index == 0) { 1669 if (st->index == 0) {
1705 if ((!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) && 1670 if (!mxf->edit_unit_byte_count &&
1671 (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
1706 !(flags & 0x33)) { // I frame, Gop start 1672 !(flags & 0x33)) { // I frame, Gop start
1707 mxf_write_klv_fill(s); 1673 mxf_write_klv_fill(s);
1708 mxf_write_partition(s, 1, 2, body_partition_key, 0); 1674 mxf_write_partition(s, 1, 2, body_partition_key, 0);
1709 1675
1710 mxf_write_klv_fill(s); 1676 mxf_write_klv_fill(s);
1711 mxf_write_index_table_segment(s); 1677 mxf_write_index_table_segment(s);
1712 } 1678 }
1713 1679
1714 mxf_write_klv_fill(s); 1680 mxf_write_klv_fill(s);
1715 mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
1716 mxf->index_entries[mxf->edit_units_count].flags = flags;
1717 mxf_write_system_item(s); 1681 mxf_write_system_item(s);
1718 1682
1719 mxf->body_offset += KAG_SIZE; // size of system element 1683 if (!mxf->edit_unit_byte_count) {
1720 1684 mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
1685 mxf->index_entries[mxf->edit_units_count].flags = flags;
1686 mxf->body_offset += KAG_SIZE; // size of system element
1687 }
1721 mxf->edit_units_count++; 1688 mxf->edit_units_count++;
1722 } else if (st->index == 1) { 1689 } else if (!mxf->edit_unit_byte_count && st->index == 1) {
1723 mxf->index_entries[mxf->edit_units_count-1].slice_offset = 1690 mxf->index_entries[mxf->edit_units_count-1].slice_offset =
1724 mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset; 1691 mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
1725 } 1692 }
1726 1693
1727 mxf_write_klv_fill(s); 1694 mxf_write_klv_fill(s);
1728 put_buffer(pb, sc->track_essence_element_key, 16); // write key 1695 put_buffer(pb, sc->track_essence_element_key, 16); // write key
1729 klv_encode_ber4_length(pb, pkt->size); // write length 1696 if (s->oformat == &mxf_d10_muxer) {
1730 put_buffer(pb, pkt->data, pkt->size); // write value 1697 if (st->codec->codec_type == CODEC_TYPE_VIDEO)
1698 mxf_write_d10_video_packet(s, st, pkt);
1699 else
1700 mxf_write_d10_audio_packet(s, st, pkt);
1701 } else {
1702 klv_encode_ber4_length(pb, pkt->size); // write length
1703 put_buffer(pb, pkt->data, pkt->size);
1704 mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
1705 }
1731 1706
1732 put_flush_packet(pb); 1707 put_flush_packet(pb);
1733
1734 mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
1735 1708
1736 return 0; 1709 return 0;
1737 } 1710 }
1738 1711
1739 static void mxf_write_random_index_pack(AVFormatContext *s) 1712 static void mxf_write_random_index_pack(AVFormatContext *s)
1780 1753
1781 mxf_write_klv_fill(s); 1754 mxf_write_klv_fill(s);
1782 mxf_write_random_index_pack(s); 1755 mxf_write_random_index_pack(s);
1783 1756
1784 if (!url_is_streamed(s->pb)) { 1757 if (!url_is_streamed(s->pb)) {
1785 int index;
1786 url_fseek(pb, 0, SEEK_SET); 1758 url_fseek(pb, 0, SEEK_SET);
1787 if (s->oformat == &mxf_d10_muxer) { 1759 if (mxf->edit_unit_byte_count) {
1788 mxf_write_partition(s, 1, 2, header_closed_partition_key, 1); 1760 mxf_write_partition(s, 1, 2, header_closed_partition_key, 1);
1789 index = 1; 1761 mxf_write_klv_fill(s);
1790 } else if (mxf->edit_unit_byte_count) { 1762 mxf_write_index_table_segment(s);
1791 mxf_write_partition(s, 0, 2, header_closed_partition_key, 1);
1792 index = 1;
1793 } else { 1763 } else {
1794 mxf_write_partition(s, 0, 0, header_closed_partition_key, 1); 1764 mxf_write_partition(s, 0, 0, header_closed_partition_key, 1);
1795 index = 0;
1796 }
1797 if (index) {
1798 mxf_write_klv_fill(s);
1799 mxf_write_index_table_segment(s);
1800 } 1765 }
1801 } 1766 }
1802 1767
1803 ff_audio_interleave_close(s); 1768 ff_audio_interleave_close(s);
1804 1769
1906 NULL, 1871 NULL,
1907 sizeof(MXFContext), 1872 sizeof(MXFContext),
1908 CODEC_ID_PCM_S16LE, 1873 CODEC_ID_PCM_S16LE,
1909 CODEC_ID_MPEG2VIDEO, 1874 CODEC_ID_MPEG2VIDEO,
1910 mxf_write_header, 1875 mxf_write_header,
1911 mxf_write_d10_packet, 1876 mxf_write_packet,
1912 mxf_write_footer, 1877 mxf_write_footer,
1913 AVFMT_NOTIMESTAMPS, 1878 AVFMT_NOTIMESTAMPS,
1914 NULL, 1879 NULL,
1915 mxf_interleave, 1880 mxf_interleave,
1916 }; 1881 };